Installation keepalive
Installation depends plugin:
yum install -y gcc openssl-devel popt-devel
wget -q http://www.keepalived.org/software/keepalived-1.2.13.tar.gz tar -zxvf keepalived-1.2.13.tar.gz cd keepalived-1.2.13 ./configure && make && make install cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/sbin/keepalived /usr/sbin/ chkconfig --add keepalived chkconfig --level 345 keepalived on
Configured on the master host
CAT / etc / keepalived / keepalived.conf vrrp_script chk_mysql_port {# detect mysql service is running. There are many ways, such as process, script testing, etc. Script " / opt / chk_mysql.sh" # script here by monitoring interval The 2 # script execution interval, tested once every 2s weight - 5 # result script priority change detection failure (non-script returns 0), the priority - . 5 Fall 2 # 2 successive detection failure is considered to determine the true failure. Priority will be reduced by weight ( 1 - between 255) Rise 1 # 1 Detection successful even if successful. But does not modify the priority } vrrp_instance VI_1 { State the MASTER interface ens33 # ip of the specified virtual network interface, not necessarily according to ifconfig eth0 determined virtual_router_id 51 is # router identification, the MASTER and BACKUP must be consistent priority 100 # define the priority, the larger the number, the higher the priority, the same next vrrp_instance, MASTER priority must be higher than the priority of BACKUP. MASTER recovery after such a failure, you can once again win back the VIP resource advert_int 1 authentication { AUTH_TYPE PASS AUTH_PASS 123456 } virtual_ipaddress { 192.168 . 11.25 } track_script { chk_mysql_port } }
Configured on the slave
cat /etc/keepalived/keepalived.conf vrrp_script chk_mysql_port { script "/opt/chk_mysql.sh" interval 2 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.11.25 } track_script { chk_mysql_port } }
Detection script configuration
cat /opt/chk_mysql.sh #!/bin/bash counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l) if [ "${counter}" -eq 0 ] then ## /etc/init.d/keepalived stop
service keepalived stop else echo "running..." >> /opt/keepalived-running-info.log sleep 5000 fi
# Executed on the master and Slave sudo / etc / init.d / keepalived Start
service keepalived start //keepalived启动
service keepalived stop //keepalive 停止