【2017/8/12】Linux7 keepalived+nginx配置

参考:http://blog.csdn.net/u012453843/article/details/69668663?locationNum=3&fps=1
参考:http://blog.csdn.net/dingsai88/article/details/51015681


初始环境

nginx快速搭建:http://blog.csdn.net/ssrmygod/article/details/77103559
VIP:192.168.158.110
nginx111:192.168.158.111
nginx112:192.168.158.112


将nginx111设置为主节点,nginx112设置为备份节点。一般情况下,访问VIP的时候会自动访问主节点,当主节点故障便自动访问备份节点。




安装keepalived

tar -zxvf keepalived-1.2.18.tar.gz
cd keepalived-1.2.18/
./configure --prefix=/usr/local/keepalived
make && make install


设置开机启动

mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/




ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
若存在
[root@111 sbin]# cd /sbin
[root@111 sbin]# rm -rf keepalived 
[root@111 sbin]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/
[root@111 sbin]# 




chkconfig keepalived on






启动keepalive

[root@111 ~]# systemctl restart keepalived.service 
[root@111 ~]# ps -ef | grep keepalived-1.2.18
root      49513   4123  0 14:30 pts/3    00:00:00 grep --color=auto keepalived-1.2.18
[root@111 ~]# ps -ef | grep keepalived
root      49501      1  0 14:30 ?        00:00:00 keepalived -D
root      49502  49501  0 14:30 ?        00:00:00 keepalived -D
root      49504  49501  0 14:30 ?        00:00:00 keepalived -D
root      49516   4123  0 14:30 pts/3    00:00:00 grep --color=auto keepalived
[root@111 ~]# 


主节点配置keepalived

vi /etc/keepalived/keepalived.conf 




! Configuration File for keepalived  
  
global_defs {  
   router_id 111  #local hostname
}  
  
vrrp_script chk_nginx {  
   script "/etc/keepalived/nginx_check.sh"  
   interval 2  
   weight -20  
}  
#每隔2秒中去执行/etc/keepalived/nginx_check.sh脚本一次,这项检查从开始便一直进行,interval表示间隔时间,weight -20的意思是,脚本执行成功后把这个节点的优先级降低20。  


vrrp_instance VI_1 {  
    state MASTER  
    interface eno16777736  #ifconfig查看一下
    virtual_router_id 11   #两个节点的这项配置的值必须一样
    mcast_src_ip 192.168.158.111  #local IP
    priority 100  #MASTER节点要比BACKUP节点高
    nopreempt  
    advert_int 1  
    authentication {  
        auth_type PASS  
        auth_pass 1111  #两个节点要一样,表明它们属于一个组,keepalived会同一组中去做检查并保持高可用。
    }  
    track_script {  
       chk_nginx  
    }  
    virtual_ipaddress {  
        192.168.158.110  
    }  
}  






主节点监控脚本

vi /etc/keepalived/nginx_check.sh


#!/bin/sh  
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




备份节点配置keepalived

vi /etc/keepalived/keepalived.conf




! Configuration File for keepalived  
  
global_defs {  
   router_id 112  
}  
  
vrrp_script chk_nginx {  
   script "/etc/keepalived/nginx_check.sh"  
   interval 2  
   weight -20  
}  
  
vrrp_instance VI_1 {  
    state BACKUP  
    interface eno16777728  
    virtual_router_id 11  
    mcast_src_ip 192.168.158.112  
    priority 90  
    advert_int 1  
    authentication {  
        auth_type PASS  
        auth_pass 1111  
    }  
    track_script {  
       chk_nginx  
    }  
    virtual_ipaddress {  
        192.168.158.110  
    }  
}  




备份节点监控脚本

vi /etc/keepalived/nginx_check.sh


#!/bin/sh  
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




测试



先关闭防火墙,防止影响结果
systemctl stop firewalld.service


[root@111 ~]# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:61:50:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.158.111/24 brd 192.168.158.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet 192.168.158.110/32 scope global eno16777736     #只有Master角色的节点上有虚拟IP
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe61:5007/64 scope link 
       valid_lft forever preferred_lft forever


[root@112 keepalived]# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777728: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:3e:dd:df brd ff:ff:ff:ff:ff:ff
    inet 192.168.158.112/24 brd 192.168.158.255 scope global eno16777728
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe3e:dddf/64 scope link 
       valid_lft forever preferred_lft forever




此时访问VIP地址 curl 192.168.158.110
[root@112 keepalived]# curl 192.168.158.110


Thank you for using 158.111.






若Master角色的节点keepalived停止工作


[root@111 ~]# systemctl stop keepalived.service 


[root@111 ~]# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:61:50:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.158.111/24 brd 192.168.158.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe61:5007/64 scope link 
       valid_lft forever preferred_lft forever
[root@111 ~]# 


[root@112 keepalived]# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777728: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:3e:dd:df brd ff:ff:ff:ff:ff:ff
    inet 192.168.158.112/24 brd 192.168.158.255 scope global eno16777728
       valid_lft forever preferred_lft forever
    inet 192.168.158.110/32 scope global eno16777728    ##Master节点挂掉后,BACKUP节点上会有虚拟IP
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe3e:dddf/64 scope link 
       valid_lft forever preferred_lft forever
[root@112 keepalived]# 


此时访问VIP地址 curl 192.168.158.110
[root@111 ~]# curl 192.168.158.110


Thank you for using 158.112.




[root@111 ~]# 









你可能感兴趣的:(linux,linux,installations)