时间:2021-07-01 10:21:17 帮助过:25人阅读
(这里只展现了脚本中判断的一部分)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
def existMySQL(self):
cmd= "ps -ef | egrep -i \"mysqld\" | grep %s | egrep -iv \"mysqld_safe\" | grep -v grep | wc -l" % self.dbport
mysqldNum = os.popen(cmd). read ()
cmd = "netstat -tunlp | grep \":%s\" | wc -l" % self.dbport
mysqlPortNum= os.popen(cmd). read ()
#print mysqldNum, mysqlPortNum
if ( int(mysqldNum) <= 0):
print "error"
return 1
if ( int(mysqldNum) > 0 and mysqlPortNum <= 0):
return 1
return 0
def connect(self):
# print "in db conn"
# print "host : %s, port: %d, user: %s, password: %s" % (self.dbhost, self.dbport, self.dbuser, self.dbpassword)
try:
self.conn=MySQLdb.connect(host= "%s" %self.dbhost, port=self.dbport,user= "%s" %dbuser, passwd = "%s" %self.dbpassword)
except Exception, e:
# print " Error"
print e
return 1
return 0
def disconnect(self):
if (self.conn):
self.conn.close()
self.conn = None
|
在两台机器上修改keepalived的配置文件:
192.168.56.100上面
1 |
vim /etc/keepalived/keepalived .conf
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
vrrp_instance VI_100 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 100
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
vs_mysql_100
}
virtual_ipaddress {
192.168.56.111
}
}
|
192.168.56.101上面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
vrrp_instance VI_101 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 100
priority 90
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
vs_mysql_101
}
virtual_ipaddress {
192.168.56.111
}
}
|
总结:可以看到两台机器的state都是backup并且都是非抢占模式nopreempt,通过优先级的高低来决定谁是主库。(这里192.168.56.100是主)还有注意virtual_router_id(虚拟路由id)要保持一致。
接下来可以启动两台机器的keepalived进程:
192.168.56.100:
1 2 |
[root@node1 keepalived] # /etc/init.d/keepalived start
Starting keepalived: [ OK ]
|
观察日志中信息的变化:
cat /var/log/messages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Aug 3 10:52:17 node1 Keepalived_vrrp[6760]: Opening file ‘/etc/keepalived/keepalived.conf‘ .
Aug 3 10:52:17 node1 Keepalived_vrrp[6760]: Configuration is using : 62711 Bytes
Aug 3 10:52:17 node1 Keepalived_vrrp[6760]: Using LinkWatch kernel netlink reflector...
Aug 3 10:52:17 node1 Keepalived_vrrp[6760]: VRRP_Instance(VI_100) Entering BACKUP STATE
Aug 3 10:52:17 node1 Keepalived_vrrp[6760]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)]
Aug 3 10:52:17 node1 Keepalived_healthcheckers[6759]: Opening file ‘/etc/keepalived/keepalived.conf‘ .
Aug 3 10:52:17 node1 Keepalived_healthcheckers[6759]: Configuration is using : 4431 Bytes
Aug 3 10:52:17 node1 Keepalived_healthcheckers[6759]: Using LinkWatch kernel netlink reflector...
Aug 3 10:52:18 node1 Keepalived_vrrp[6760]: VRRP_Script(vs_mysql_100) succeeded
Aug 3 10:52:33 node1 Keepalived_vrrp[6760]: VRRP_Instance(VI_100) Transition to MASTER STATE
Aug 3 10:52:38 node1 Keepalived_vrrp[6760]: VRRP_Instance(VI_100) Entering MASTER STATE
Aug 3 10:52:38 node1 Keepalived_vrrp[6760]: VRRP_Instance(VI_100) setting protocol VIPs.
Aug 3 10:52:38 node1 Keepalived_vrrp[6760]: VRRP_Instance(VI_100) Sending gratuitous ARPs on eth0 for 192.168.56.111
Aug 3 10:52:38 node1 Keepalived_healthcheckers[6759]: Netlink reflector reports IP 192.168.56.111 added
Aug 3 10:52:43 node1 Keepalived_vrrp[6760]: VRRP_Instance(VI_100) Sending gratuitous ARPs on eth0 for 192.168.56.111
|
可以看到它从backup状态切换到master的状态了,并且发送了一个广播协议,证明192.168.56.111已经在本台机器上面,其他机器不要再使用了。
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@node1 keepalived] # ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link /loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1 /8 scope host lo
inet6 ::1 /128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link /ether 08:00:27:f1:fd:08 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.100 /24 brd 192.168.56.255 scope global eth0
inet 192.168.56.111 /32 scope global eth0
inet6 fe80::a00:27ff:fef1:fd08 /64 scope link
valid_lft forever preferred_lft forever
|
192.168.56.101:
1 2 |
[root@node2 ~] # /etc/init.d/keepalived start
Starting keepalived: [ OK ]
|
cat /var/log/messages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Aug 3 10:53:28 node2 Keepalived_healthcheckers[8111]: Interface queue is empty
Aug 3 10:53:28 node2 Keepalived_healthcheckers[8111]: Netlink reflector reports IP 192.168.56.101 added
Aug 3 10:53:28 node2 Keepalived_healthcheckers[8111]: Netlink reflector reports IP fe80::a00:27ff:fe83:b39d added
Aug 3 10:53:28 node2 Keepalived_healthcheckers[8111]: Registering Kernel netlink reflector
Aug 3 10:53:28 node2 Keepalived_healthcheckers[8111]: Registering Kernel netlink command channel
Aug 3 10:53:28 node2 Keepalived_healthcheckers[8111]: Opening file ‘/etc/keepalived/keepalived.conf‘ .
Aug 3 10:53:28 node2 Keepalived_healthcheckers[8111]: Configuration is using : 4561 Bytes
Aug 3 10:53:28 node2 Keepalived_healthcheckers[8111]: Using LinkWatch kernel netlink reflector...
Aug 3 10:53:28 node2 Keepalived_vrrp[8112]: Opening file ‘/etc/keepalived/keepalived.conf‘ .
Aug 3 10:53:28 node2 Keepalived_vrrp[8112]: Configuration is using : 62577 Bytes
Aug 3 10:53:28 node2 Keepalived_vrrp[8112]: Using LinkWatch kernel netlink reflector...
Aug 3 10:53:28 node2 Keepalived_vrrp[8112]: VRRP_Instance(VI_101) Entering BACKUP STATE
Aug 3 10:53:28 node2 Keepalived_vrrp[8112]: VRRP sockpool: [ifindex(2), proto(112), fd(12,13)]
Aug 3 10:53:28 node2 Keepalived_vrrp[8112]: VRRP_Script(vs_mysql_101) succeeded
|
这台服务器就是正常的backup状态,时刻准备着接管主库的服务。
模拟一下主库宕机的故障切换:
主库192.168.56.100上面执行关闭MySQL服务操作:
1 |
mysqladmin -uroot -proot123 shutdown
|
这时再查看一下keepalived日志的情况:
1 2 3 4 5 6 |
cat /var/log/messages
Aug 3 11:04:17 node1 Keepalived_vrrp[6760]: VRRP_Script(vs_mysql_100) failed
Aug 3 11:04:18 node1 Keepalived_vrrp[6760]: VRRP_Instance(VI_100) Entering FAULT STATE
Aug 3 11:04:18 node1 Keepalived_vrrp[6760]: VRRP_Instance(VI_100) removing protocol VIPs.
Aug 3 11:04:18 node1 Keepalived_healthcheckers[6759]: Netlink reflector reports IP 192.168.56.111 removed
Aug 3 11:04:18 node1 Keepalived_vrrp[6760]: VRRP_Instance(VI_100) Now in FAULT state
|
已经把vip removed了,并且keepalived的状态变成了fault
在备库192.168.56.101上面查看日志:
1 2 3 4 5 6 7 |
cat /var/log/messages
Aug 3 11:04:29 node2 Keepalived_vrrp[8112]: VRRP_Instance(VI_101) Transition to MASTER STATE
Aug 3 11:04:34 node2 Keepalived_vrrp[8112]: VRRP_Instance(VI_101) Entering MASTER STATE
Aug 3 11:04:34 node2 Keepalived_vrrp[8112]: VRRP_Instance(VI_101) setting protocol VIPs.
Aug 3 11:04:34 node2 Keepalived_vrrp[8112]: VRRP_Instance(VI_101) Sending gratuitous ARPs on eth0 for 192.168.56.111
Aug 3 11:04:34 node2 Keepalived_healthcheckers[8111]: Netlink reflector reports IP 192.168.56.111 added
Aug 3 11:04:39 node2 Keepalived_vrrp[8112]: VRRP_Instance(VI_101) Sending gratuitous ARPs on eth0 for 192.168.56.111
|
备库已经从backup状态切换成master状态了。并且VIP(192.168.56.111)已经切换过来。
查看ip地址:
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@node2 ~] # ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link /loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1 /8 scope host lo
inet6 ::1 /128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link /ether 08:00:27:83:b3:9d brd ff:ff:ff:ff:ff:ff
inet 192.168.56.101 /24 brd 192.168.56.255 scope global eth0
inet 192.168.56.111 /32 scope global eth0
inet6 fe80::a00:27ff:fe83:b39d /64 scope link
valid_lft forever preferred_lft forever
|
主备库切换成功。
实战演练过程结束,希望对大家学习MySQL高可用集群有帮助。
今后我们可能还会遇到其他的MySQL高可用架构,学习它的时候,先不要忙于搭建,要先弄清原理,整理好实验过程的思路,遇到报错,一步步地去排查。自己的水平也会在这个历练的过程中,得到提升。
今后我们可以一起讨论,在写博的过程中难免可能会有一些笔误,或是想不周全的地方,希望大家谅解。有不对的地方欢迎各位老铁指定。
企业中MySQL高可用集群架构三部曲之MM+keepalived
标签:判断脚本 popen 定时 summary function read backup dbus t_sql