Centos7 keepalived安装并监控mysql实现自动切换
安装 下载(https://www.keepalived.org/download.html) wget https://www.keepalived.org/software/keepalived-2.1.5.tar.gz tar -xvf keepalived-2.1.5.tar.gz -C /usr/local/src cd /usr/local/src/keepalived-2.1.5 ./configure --prefix=/ #安装一般不能一帆风顺,一般会缺少OpenSSL #1.在线安装:yum install -y openssl openssl-devel #2.离线安装:参考离线安装openssl.md make && make install 修改配置 /etc/keepalived/keepalived.conf #node117 ! Configuration File for keepalived global_defs { router_id lb01 } vrrp_script chk_msql { script "/etc/keepalived/check_mysql.sh" interval 3 timeout 9 fall 2 rise 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface ens192 virtual_router_id 125 mcast_src_ip 192.168.6.117 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_msql } virtual_ipaddress { 192.168.6.125 } } #node118 ! Configuration File for keepalived global_defs { router_id lb01 } vrrp_script chk_msql { script "/etc/keepalived/check_mysql.sh" interval 3 timeout 9 fall 2 rise 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface ens192 virtual_router_id 125 mcast_src_ip 192.168.6.118 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_msql } virtual_ipaddress { 192.168.6.125 } } #check_mysql.sh #!/bin/bash #MYSQL=mysql #MYSQL_HOST=localhost #MYSQL_USER=root MYSQL_PASSWORD=xiaoWEI0923! #$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" >/dev/null 2>&1 mysql -uroot -p$MYSQL_PASSWORD -e "show status;" >/dev/null 2>&1 #$mysqlclient --host=$host --port=$port --user=$user --password=$password -e "show databases;" > /dev/null 2>&1 if [ $? == 0 ] then echo " $host mysql login successfully " exit 0 else #echo " $host mysql login faild" #/etc/init.d/keepalived stop exit 2 fi #check_nginx.sh #!/bin/bash A=`ps -C nginx --no-header | wc -l` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf sleep 2 if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then #systemctl stop keepalived echo "nginx exit" exit 2 fi fi echo "nginx running..." exit 0 启动 systemctl enable keepalived.service systemctl start keepalived.service # 可通过status查看相关错误 systemctl status keepalived.service 测试 1.ip addr查看虚拟ip绑定到了哪台机器(假如为A机器) 2.停止A机器keepalived,确认ip绑定到了另一台机器(B机器) 3.启动A机器keealived,停止B机器上keepavlived,ip绑定到A机器 4.同样方式测试mysql,停止A机器mysql,ip绑定到了B 4.恢复A机器mysql,停止B机器mysql,ip绑定到了A 另一种配置方法 #通过端口区分不同业务 ! Configuration File for keepalived global_defs { router_id 001 } vrrp_instance VI_1 { state MASTER interface ens192 virtual_router_id 125 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.6.125 } track_script { nginx_check } } vrrp_script nginx_check { script "/etc/keepalived/scripts/check_n.sh" interval 1 weight -20 } virtual_server 192.168.6.125 80 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.6.117 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.6.118 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }