ubuntu keepalived notes

surroundings:

master:Ubuntu 12.04(192.168.31.186)
backup: Ubuntu 12.04(192.168.31.110)
VIP: 192.168.31.100

keepalived principle can be so simple to understand:

keepalived installed on two physical servers, and monitor each other whether in normal operation.
When the master of normal time: keepalived will be binding on the master vip (192.168.8.100)

tail -f /var/log/syslog

Feb 18 05:41:41 keepalived1 Keepalived_vrrp[2561]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.31.100
Feb 18 05:41:41 keepalived1 Keepalived_healthcheckers[2560]: Netlink reflector reports IP 192.168.31.100 added
Feb 18 05:41:46 keepalived1 Keepalived_vrrp[2561]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.31.100

When the master failed, keepalived the backup will be detected, and the binding vip (192.168.8.100)
or virtual IP to drift to the backup
when the master vip recovery and re-bound

installation

apt-get install keepalived

keepalived profile

vim /etc/keepalived/keepalived.conf
global_defs {
    router_id L1
}
vrrp_instance VI_1 {
    state MASTER //设置服务器模式,需要大写,备用机可以写 state BACKUP
    interface eth0
    virtual_router_id 51 //VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
    priority 100 //主节点的优先级(1-254之间),备用节点必须比主节点优先级低
    advert_int 1  
    authentication { //设置验证信息,两个节点必须一致
        auth_type PASS
        auth_pass 123456
}
virtual_ipaddress {  //指定虚拟IP, 两个节点设置必须一样
   192.168.31.100
}

Mastr and backup is the difference between state and priority

start up

service keepalived start

test

Nginx mounted on both machines, the installation procedure will be omitted
install nginx modified after the completion of the default page for different content
and access http://192.168.31.100

The default should display the contents of 192.168.31.186
and then stop or restart 192.168.31.186 192.168.31.186 keepalived service of
this refresh the page
displays the contents of 192.168.31.110
If 192.168.31.186 vip will switch back to normal on 192.168.31.186

Http monitoring service

Modify the configuration file

vim /etc/keepalived/keepalived.conf 


global_defs { router_id master }
vrrp_script chk_nginx_port { script "/check_nginx.sh" interval 2 weight 2 }



vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 }
   virtual_ipaddress { 192.168.31.100 }
   track_script { chk_nginx_port }
}
vim /check_nginx.sh


#!/bin/bash 
V_NUM=`eval "netstat -nlpt | grep '80' | wc -l"`

if [ $V_NUM -lt 1 ];then
    /etc/init.d/keepalived stop
    exit
fi

Check if port 80 is not listening, stop keepalived service, vip passes to 192.168.31.110 on

Then you need to repair http human services, and then restart the service keepalived

Reproduced in: https: //my.oschina.net/neohlj/blog/624784

Guess you like

Origin blog.csdn.net/weixin_33796205/article/details/91835283