Install keepalived
# ./configure --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-358.el6.x86_64/ && make && make install#ln -s /usr/local/sbin/keepalived /sbin/# chkconfig keepalived on
Modify Nic Configuration
# vim /etc/sysconfig/network-scripts/ifcfg-lo:0DEVICE=lo:0IPADDR=192.168.1.2NETMASK=255.255.255.255# If you‘re having problems with gated making 127.0.0.0/8 a martian,# you can change this to something else (255.255.255.255, for example)BROADCAST=192.168.1.2ONBOOT=yes
Modify the Kernel File Configuration
# vim /etc/sysctl.confnet.ipv4.conf.all.arp_announce = 2net.ipv4.conf.lo.arp_announce = 2net.ipv4.conf.all.arp_ignore = 1net.ipv4.conf.lo.arp_ignore = 1# sysctl -p
Modify the keepalived configuration file
# Vim/etc/keepalived. conf! Configuration file for keepalived vrrp_script failed {script "/var/Shell/login" interval 2 weight 2} global_defs {icationication_email {[email protected]} login [email protected] smtp_server 127.0.0.1 route 30 router_id lvs_devel} vrrp_instance vi_1 {state master (Backup) interface eth0 virtual_router_id 51 priority 100 (backup must have a higher priority than Master) advert_int 1 authentication {auth_type pass auth_pass 1111} track_script {token} virtual_ipaddress {192.168.1.2} virtual_server 192.168.1.2 80 {delay_loop 6 destination RR Dr destination 255.255.255.0 255.50 protocol TCP real_server 192.168.1.1 80 {weight 3 tcp_check {connect_timeout 3 nb_get_retry 3 delay_before_retry 3} real_server 192.168.1.3 80 {weight 3 tcp_check {connect_timeout 3 nb_get_retry 3 delay_before_retry 3 }}}
Write scripts (Monitor MySQL, stop the nginx service when MySQL cannot be restarted, The keepalived service will remove the problematic web server from the cluster)
# vim mysql_check.sh#!/bin/bashMYSQL_SLAVE=`ps -C mysqld | grep -v PID | wc -l`if [ $MYSQL_SLAVE -eq 0 ];then /etc/init.d/mysqld restart sleep 3 if [ `ps -C mysqld | grep -v PID | wc -l` -eq 0 ];then pkill -9 nginx fifi
Keepalived dual-host for high availability and load balancing