分别在服务器Nginx 01和Nginx 02安装Nginx和Keepalived。
# 安装Nginx [root@Docker-OA-98 ~]# yum -y install nginx # 安装Keepalived [root@Docker-OA-98 ~]# yum -y install keepalived 3.2 配置Nginx负载均衡分别在服务器Nginx 01和Nginx02上配置Nginx的应用负载均衡。
[root@Docker-OA-98 ~]# vi /etc/nginx/nginx.conf upstream application01 { ip_hash; server 192.168.202.91:8080; server 192.168.202.92:8080; server 192.168.202.93:8080; } server { listen 80; listen [::]:80; server_name service.test01.com; #root /usr/share/nginx/html; location / { proxy_pass ; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; access_log /var/log/nginx/access.ecology.log main; client_max_body_size 100m; client_body_buffer_size 1024k; proxy_connect_timeout 5000; proxy_read_timeout 5000; proxy_send_timeout 6000; proxy_buffer_size 1024k; proxy_buffers 4 512k; proxy_busy_buffers_size 1024k; proxy_temp_file_write_size 1024k; } } 4. 高可用方案 -- 主主模式主主模式
Nginx 01服务器Keepalived脚本配置
[root@Nginx-A-98 src]# vim /etc/keepalived/keepalived.conf vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" # 检测nginx状态的脚本路径 interval 2 # 检测时间间隔2s weight -20 # 如果脚本的条件成立,权重-20 } vrrp_instance VI_1 { # VRRP实例名;多实例的情况下,名称必须不一致 state MASTER # 服务状态;MASTER(工作状态)BACKUP(备用状态) interface eth0 # VIP绑定网卡 virtual_router_id 51 # 虚拟路由ID,主、备节点必须一致;多实例的情况下,ID必须不一致 mcast_src_ip 192.168.1.98 # 本机IP nopreempt # 优先级高的设置,解决异常回复后再次抢占的问题 priority 100 # 优先级;取值范围:0~254;MASTER > BACKUP advert_int 1 # 组播信息发送间隔,主、备节点必须一致,默认1s authentication { # 验证信息;主、备节点必须一致 auth_type PASS # VRRP验证类型,PASS、AH两种 auth_pass 1111 # VRRP验证密码,在同一个vrrp_instance下,主、从必须使用相同的密码才能正常通信 } track_script { # 将track_script块加入instance配置块 chk_nginx # 执行Nginx监控的服务 } virtual_ipaddress { # 虚拟IP池,主、备节点必须一致,可以定义多个VIP 192.168.1.100 # 虚拟IP } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 52 mcast_src_ip 192.168.1.98 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.1.101 } }Nginx 02服务器Keepalived配置
[root@Nginx-B-99 src]# vim /etc/keepalived/keepalived.conf vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 mcast_src_ip 192.168.1.99 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.1.100 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 52 mcast_src_ip 192.168.1.99 nopreempt priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.1.101 } }Nginx状态监测脚本
分别在服务器Nginx 01和Nginx 02上添加Nginx状态监测脚本。
[root@Nginx-A-98 keepalived]# vim /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我们也可以根据自己的业务需求,总结出在什么情形下关闭keepalived,如 curl 主页连续2个5s没有响应则切换:
#!/bin/bash count = 0 for (( k=0; k<2; k++ )) do check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\\n" -o /dev/null ) if [ "$check_code" != "200" ]; then count = count +1 continue else count = 0 break fi done if [ "$count" != "0" ]; then # /etc/init.d/keepalived stop exit 1 else exit 0 fi 5. 高可用方案 -- 主从模式主从模式