MySQL keepalived安装配置(二)
一、keepalived安装配置
1.1、主备库安装keepalived服务:
yum install -y keepalived yum install -y python MySQL-python
1.2、将文件部署在/opt/目录下
[root@hzyg-ip-bd32 scripts]# pwd /opt/keepalived/scripts [root@hzyg-ip-bd32 scripts]# ll total 336 -rwxr-xr-x 1 root root 20620 Dec 10 2019 dtagentconf.py -rw-r--r-- 1 root root 19282 Mar 8 16:54 dtagentconf.pyc -rwxr-xr-x 1 root root 8830 Aug 22 2016 dtutil.py -rw-r--r-- 1 root root 11711 Mar 9 11:24 dtutil.pyc -rwxr-xr-x 1 root root 8591 Aug 22 2016 keepalived_conf_scanner.py -rw-r--r-- 1 root root 6235 Mar 9 11:24 keepalived_conf_scanner.pyc -rwxr-xr-x 1 root root 16931 Dec 10 2019 mysqlha_check_alive.py -rwxr-xr-x 1 root root 7089 Dec 10 2019 mysqlha_setup_master.py -rwxr-xr-x 1 root root 1002 Aug 22 2016 mysqlha_setup_slave.py -rw-r--r-- 1 root root 218018 May 29 2016 MySQL-python-1.2.5.tar.gz [root@hzyg-ip-bd32 scripts]# cd ../conf [root@hzyg-ip-bd32 conf]# ll total 8 -rw-r--r-- 1 root root 53 Dec 10 2019 agent.yaml -rw-r--r-- 1 root root 849 Mar 9 11:40 keepalived.conf
1.3、授权脚本执行权限:
[root@hzyg-ip-bd32 scripts]# chmod 777 *.py [root@hzyg-ip-bd32 scripts]# ll total 336 -rwxr-xr-x 1 root root 20620 Dec 10 2019 dtagentconf.py -rw-r--r-- 1 root root 19282 Mar 8 16:54 dtagentconf.pyc -rwxr-xr-x 1 root root 8830 Aug 22 2016 dtutil.py -rw-r--r-- 1 root root 11711 Mar 9 11:24 dtutil.pyc -rwxr-xr-x 1 root root 8591 Aug 22 2016 keepalived_conf_scanner.py -rw-r--r-- 1 root root 6235 Mar 9 11:24 keepalived_conf_scanner.pyc -rwxr-xr-x 1 root root 16931 Dec 10 2019 mysqlha_check_alive.py -rwxr-xr-x 1 root root 7089 Dec 10 2019 mysqlha_setup_master.py -rwxr-xr-x 1 root root 1002 Aug 22 2016 mysqlha_setup_slave.py -rw-r--r-- 1 root root 218018 May 29 2016 MySQL-python-1.2.5.tar.gz [root@hzyg-ip-bd32 scripts]#
1.4、编辑配置文件:
注:
3306 替换为真实端口号
状态:主库为 MASTER,从库为 BACKUP
ens32 替换为网络设备号
virtual_ipaddress vip
地址根据实际情况规划 auth_pass 要保持主备库一致
下面标记为红色字体的地方需要特别关注修改
配置文件样例如下
主配置文件:
/opt/keepalived/conf/keepalived.conf
vrrp_script chk_port_3306 { script "/opt/keepalived/scripts/mysqlha_check_alive.py 3306" interval 11 # check every 2 seconds fall 3 # require 2 failures for KO rise 3 # require 2 successes for OK timeout 11 } vrrp_instance mysql_3306 { state MASTER interface bond0 garp_master_delay 5 virtual_router_id 201 priority 200 advert_int 1 unicast_src_ip 10.129.16.32 unicast_peer { 10.129.16.33 } nopreempt authentication { auth_type PASS auth_pass PASS3306 } virtual_ipaddress { 10.129.16.44/24 dev bond0 } track_script { chk_port_3306 } #debug notify_master "/opt/keepalived/scripts/mysqlha_setup_master.py 3306" notify_backup "/opt/keepalived/scripts/mysqlha_setup_slave.py 3306" notify_fault "/opt/keepalived/scripts/keepalived_conf_scanner.py" }
从配置文件:
/opt/keepalived/conf/keepalived.conf
vrrp_script chk_port_3306 { script "/opt/keepalived/scripts/mysqlha_check_alive.py 3306" interval 11 # check every 2 seconds fall 3 # require 2 failures for KO rise 3 # require 2 successes for OK timeout 11 } vrrp_instance mysql_3306 { state BACKUP interface bond0 garp_master_delay 5 virtual_router_id 201 priority 100 advert_int 1 unicast_src_ip 10.129.16.33 unicast_peer { 10.129.16.32 } nopreempt authentication { auth_type PASS auth_pass PASS3306 } virtual_ipaddress { 10.129.16.44/24 dev bond0 } track_script { chk_port_3306 } #debug notify_master "/opt/keepalived/scripts/mysqlha_setup_master.py 3306" notify_backup "/opt/keepalived/scripts/mysqlha_setup_slave.py 3306" notify_fault "/opt/keepalived/scripts/keepalived_conf_scanner.py" }
1.5、修改默认配置文件指向/opt/keepalived/conf/keepalived.conf:
因为keepalived默认会到 /etc/keepalived/ 读取配置文件,如果想要生效我们需要修改运行脚本,当然也可以直接将配置文件拷贝到指定目录,下面介绍修改启动脚本配置。
将
ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS
修改为:
ExecStart=/usr/sbin/keepalived --use-file=/opt/keepalived/conf/keepalived.conf
1.6、测试环境依赖
启动keepalived前建议最好先测试一下 py 脚本是否有依赖缺失
此时我们发现Python有yaml模块缺失,下面是离线安装方法
tar xf PyYAML-5.4.1.tar.gz cd PyYAML-5.4.1 python setup.py install
如果提示:
ImportError: No module named setuptools
先安装setuptools再重新安装yaml
yum install python-setuptools
安装完成后测试没问问题后可进入下一步。
1.7、主库启动keepalived
[root@test32 conf]# systemctl start keepalived
2.8、备库启动keepalived
[root@test33 conf]# systemctl start keepalived
2.9、确认监控数据
如果查不到监控数据,那么说明keepalived部署有问题,需要检查前面的步骤。