keepalived高可用反向代理的nginx
环境准备
VIP(Virtual IP)为192.168.1.225,用户只需要访问这个IP地址即可获得网页服务
负载均衡主机为192.168.1.221(master) ----》keepalived+nginx
备机为 192.168.1.222(backup) ----》keepalived+nginx
Web服务器A为192.168.1.223(web01) ----》realserver + nginx
192.168.1.221(master)主机配置
root@localhost ~]# systemctl stop firewalld [root@localhost ~]# setenforce 0 [root@localhost ~]# yum -y install keepalived
[root@localhost ~]# cd /etc/keepalived/ [root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak [root@localhost keepalived]# echo "" > keepalived.conf
[root@localhost src]# cd /etc/keepalived/ [root@localhost keepalived]# cat keepalived.conf global_defs { notification_email { 1350748936@qq.com } notification_email_from sns-lvs@gmail.com smtp_server smtp.hysec.com smtp_connection_timeout 30 router_id nginx_master # 设置nginx master的id,在一个网络应该是唯一的 } vrrp_script chk_http_port { script "/usr/local/src/check_nginx_pid.sh" #最后手动执行下此脚本,以确保此脚本能够正常执行 interval 2 #(检测脚本执行的间隔,单位是秒) weight 2 } vrrp_instance VI_1 { state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备 interface ens33 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) virtual_router_id 66 # 虚拟路由编号,主从要一直 priority 100 # 优先级,数值越大,获取处理请求的优先级越高 advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数) authentication { auth_type PASS auth_pass 1111 } track_script { chk_http_port #(调用检测脚本) } virtual_ipaddress { 192.168.1.225 # 定义虚拟ip(VIP),可多设,每行一个 } }
nginx安装
[root@localhost keepalived]# tar xf nginx-1.12.2.tar.gz [root@localhost keepalived]# cd nginx-1.12.2 [root@localhost nginx-1.12.2]# yum -y install gcc* pcre-devel zlib-devel [root@localhost nginx-1.12.2]# useradd -r -s /sbin/nologin nginx [root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install [root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [root@localhost nginx-1.12.2]# nginx
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
#location / {
# root html;
# index index.html index.htm;
#}
#注释掉
location / {
proxy_pass http://192.168.1.223;
}
192.168.1.222(slave)备机配置
[root@localhost ~]# cd /etc/keepalived/ [root@localhost keepalived]# vi keepalived.conf global_defs { notification_email { 1350748936@qq.com } notification_email_from sns-lvs@gmail.com smtp_server smtp.hysec.com smtp_connection_timeout 30 router_id nginx_master # 设置nginx master的id,在一个网络应该是唯一的 } vrrp_script chk_http_port { script "/usr/local/src/check_nginx_pid.sh" #最后手动执行下此脚本,以确保此脚本能够正常执行 interval 2 #(检测脚本执行的间隔,单位是秒) weight 2 } vrrp_instance VI_1 { state BACKUP # 指定keepalived的角色,MASTER为主,BACKUP为备 interface ens33 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) virtual_router_id 66 # 虚拟路由编号,主从要一直 priority 90 # 优先级,数值越大,获取处理请求的优先级越高 advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数) authentication { auth_type PASS auth_pass 1111 } track_script { chk_http_port #(调用检测脚本) } virtual_ipaddress { 192.168.1.225 # 定义虚拟ip(VIP),可多设,每行一个 } }
[root@localhost keepalived]# cat /usr/local/src/check_nginx_pid.sh 主备机上都有。 #!/bin/bash A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx #重启nginx if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx重启失败 exit 1 else exit 0 fi else exit 0 fi
[root@localhost ~]# chmod 775 /usr/local/src/check_nginx_pid.sh
nginx安装
[root@localhost keepalived]# tar xf nginx-1.12.2.tar.gz [root@localhost keepalived]# cd nginx-1.12.2 [root@localhost nginx-1.12.2]# yum -y install gcc* pcre-devel zlib-devel [root@localhost nginx-1.12.2]# useradd -r -s /sbin/nologin nginx [root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install [root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [root@localhost nginx-1.12.2]# nginx
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
#location / {
# root html;
# index index.html index.htm;
#}
#注释掉
location / {
proxy_pass http://192.168.1.223;
}
192.168.1.222这装nginx
[root@localhost ~]# tar xf nginx-1.12.2.tar.gz [root@localhost ~]# cd nginx-1.12.2 [root@localhost nginx-1.12.2]# yum -y install gcc* pcre-devel zlib-devel [root@localhost nginx-1.12.2]# useradd -r -s /sbin/nologin nginx [root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install [root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [root@localhost nginx-1.12.2]# nginx [root@localhost nginx-1.12.2]# cd /usr/local/nginx/ [root@localhost nginx]# cd html/ [root@localhost html]# echo "This is test." > index.html [root@localhost html]# curl localhost This is test.
测试
[root@localhost ~]# curl 192.168.1.225 This is test. [root@localhost ~]# curl 192.168.1.225 This is test. [root@localhost ~]# curl 192.168.1.225 This is test. [root@localhost ~]# curl 192.168.1.225 This is test. [root@localhost ~]# curl 192.168.1.225 This is test. [root@localhost ~]# curl 192.168.1.225 This is test.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?