参考链接: https://blog.csdn.net/l1028386804/article/details/52577875 注:使用keepalived+nginx,配置nginx代理主从备份 代理服务器 主:192.168.0.203 VIP:192.168.0.244 从:192.168.0.204 VIP:192.168.0.244 web服务器 web1:192.168.0.210 web2:192.168.0.211 代理服务均需要安装 keepalived 和 nginx,web服务器安装 apach nginx tomcat 等任选一即可 代理: # yum -y install keepalived # wget https://nginx.org/download/nginx-1.14.2.tar.gz # tar -xvf nginx-1.14.2.tar.gz # yum -y install make gcc gcc-c++ autoconf automake pcre zlib openssl zlib-devel openssl-devel libtool # useradd -s /sbin/nologin nginx # cd nginx-1.14.2 # ./configure --with-http_stub_status_module --with-http_ssl_module --prefix=/usr/local/nginx --user=nginx --group=nginx # make && make install web: 如上安装 nginx 即可 代理服务器配置如下: 主要代理如下: # vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { # notification_email { # admin@test.com # } # notification_email_from admin@test.com # smtp_server 192.168.0.1 # smtp_connect_timeout 30 router_id proxy_nginx1 # 标识本节点的字条串 } # keepalived 会定时执行脚本并对脚本执行的结果进行分析,动态调整 vrrp_instance 的优先级,如果脚本执行结果为 0,并且 weight 配置的值大于 0,则优先级相应的增加,如果脚本执行结果非 0,并且 weight配置的值小于 0,则优先级相应的减少,其他情况,维持原本配置的优先级,即配置文件中 priority 对应的值; vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 # 检测时间间隔 weight 2 # 条件成立,权重+2(若值为-2,则条件成立时权重-2) } vrrp_instance VI_1 { # 定义虚拟路由, VI_1 为虚拟路由的标示符,自己定义名称: state MASTER # 主节点为 MASTER, 对应的备份节点为 BACKUP interface eth0 # 绑定虚拟 IP 的网络接口,与本机 IP 地址所在的网络接口相同, 我的是 eth0 virtual_router_id 45 # 虚拟路由的 ID 号, 两个节点设置必须一样,相同的 VRID 为一个组,他将决定多播的 MAC 地址 mcast_src_ip 192.168.0.203 # 本机 IP 地址 priority 100 # 节点优先级, 值范围 0-254, MASTER 要比 BACKUP 高 nopreempt # 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题 advert_int 1 # 组播信息发送间隔,两个节点设置必须一样, 默认 1s authentication { # 设置验证信息,两个节点必须一致 auth_type PASS auth_pass 1111 # 真实生产,按需求更改 } track_script { # 将 track_script 块加入 instance 配置块 chk_nginx # 执行 Nginx 监控的服务 } virtual_ipaddress { # 虚拟 IP 池, 两个节点设置必须一样 192.168.0.244 # 虚拟 ip,可以定义多个 } } # cat /usr/local/nginx/conf/nginx.conf #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream webserver { server 192.168.0.210:80 max_fails=1; server 192.168.0.211:80 max_fails=1; } server { listen 80; server_name localhost,192.168.0.244; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_next_upstream error timeout http_500 http_502 http_504; proxy_read_timeout 10s; #这里表示程序返回的时间,请参考php.ini的max_exe_time来设置, proxy_pass http://webserver; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } 备份代理配置 # cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { # notification_email { # admin@test.com # } # notification_email_from admin@test.com # smtp_server 192.168.0.1 # smtp_connect_timeout 30 router_id proxy_nginx2 } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 45 mcast_src_ip 192.168.0.204 priority 90 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.0.244 } } [root@nginx2 nginx-1.14.2]# cat /usr/local/nginx/conf/nginx.conf #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream webserver { server 192.168.0.210:80 max_fails=1; server 192.168.0.211:80 max_fails=1; } server { listen 80; server_name localhost,192.168.0.244; #charset koi8-r; location / { proxy_next_upstream error timeout http_500 http_502 http_504; proxy_read_timeout 10s; #这里表示程序返回的时间,请参考php.ini的max_exe_time来设置, proxy_pass http://webserver; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } 编写 nginx 状态检测脚本 (如果nginx停止运行,尝试启动,如果无法启动则杀死本机的 keepalived 进程,keepalied将虚拟 ip 绑定到BACKUP机器上) # vi /etc/keepalived/nginx_check.sh #!/bin/bash A=`ps -C nginx –no-header |wc -l` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx sleep 2 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then killall keepalived fi fi 保存并添加执行权限 #chmod +x /etc/keepalived/nginx_check.sh web服务器配置如下(nginx): web1: echo "web1" > /usr/local/nginx/html/index.html web2: echo "web2" > /usr/local/nginx/html/index.html 设置完成后,代理服务器启动 keepalived 和 ngixn #systemctl start keepalived #/usr/local/nginx/sbin/nginx 检查 keepalived 配置是否正确 #ip a s eth0 2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:fb:51:68 brd ff:ff:ff:ff:ff:ff inet 192.168.0.203/24 brd 192.168.0.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet 192.168.0.244/32 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fefb:5168/64 scope link valid_lft forever preferred_lft forever !!注!! 两台同时启动,优先级较高的服务器上会查询到VIP 启动web服务器,并进行测试 # /usr/local/nginx/sbin/nginx # curl 192.168.0.244 关闭其中一台代理进行测试,查看结果是否会有变化