欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

nginx+keepalived配置

程序员文章站 2024-02-23 16:05:46
...

转:http://www.yangzb.com/article/5

nginx安装见:http://www.yangzb.com/article/6
keepalived安装见:http://www.yangzb.com/article/16
NGINX High Availability 官方文档:https://docs.nginx.com/nginx/admin-guide/high-availability/ha-keepalived-nodes/
nginx+keepalived配置

1. 添加脚本

[aaa@qq.com bin]# vi /etc/keepalived/check_nginx.sh 
#!/bin/bash
# 如果进程中没有nginx则将keepalived进程kill掉
status=0
A=`ps -C nginx --no-header |wc -l`      ## 查看是否有 nginx进程 把值赋给变量A 
if [ $A -eq 0 ];then                    ## 如果没有进程值得为 零
    service nginx start ## 则结束 重启nginx
	status=1
fi
exit $status

添加可执行权限
[aaa@qq.com subsys]# chmod 755 /etc/keepalived/check_nginx.sh 

2. 修改keepalived配置

#注意一定要在 vrrp_instance之前,不然不执行
vrrp_script check_nginx
{
    script "/etc/keepalived/check_nginx.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    nopreempt
    advert_int 1
    #检测添加脚本
    track_script {
        check_nginx
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.160 brd 192.168.10.255 dev eth0 label eth0:vip
    }
}
virtual_server 192.168.10.160 9008 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

#
    real_server 192.168.10.15 9008 {
        weight 1
        HTTP_GET {
        	url {
	            path /test/index.html #定义要监控的URL;
	            status_code 200 #判断上述检测机制为健康状态的响应码;
	            #digest <STRING> 判断上述检测机制为健康状态的响应的内容的校验码;
        	}
            connect_timeout 3 #连接请求的超时时长;
            retry 3 #重试次数3次
            delay_before_retry 3 #重试之前的延迟时长;
        }
    }
#
    real_server 192.168.10.16 9008 {
        weight 1
        HTTP_GET {
        	url {
	            path /test/index.html #定义要监控的URL;
	            status_code 200 #判断上述检测机制为健康状态的响应码;
	            #digest <STRING> 判断上述检测机制为健康状态的响应的内容的校验码;
        	}
            connect_timeout 3 #连接请求的超时时长;
            retry 3 #重试次数3次
            delay_before_retry 3 #重试之前的延迟时长;
        }
    }
}
相关标签: 高可用