前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Centos7 keepalived安装并监控mysql实现自动切换

Centos7 keepalived安装并监控mysql实现自动切换

作者头像
肖哥哥
发布2020-08-06 19:30:59
9530
发布2020-08-06 19:30:59
举报
文章被收录于专栏:后台及大数据开发
代码语言:javascript
复制
安装

下载(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
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-08-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档