-
启动keepalived:/usr/local/sbin/keepalived -D -d -S 0
-
master ip:192.168.32.6
-
master:/root/sbin# cat /etc/keepalived/keepalived.conf
-
global_defs {
-
router_id MySQL-ha
-
}
-
vrrp_instance VI_1{
-
# 在初始化状态下定义为主设备
-
state BACKUP
-
# 注意网卡接口
-
interface eth1
-
virtual_router_id 71
-
# 优先级,另一台改为90
-
priority 100
-
advert_int 1
-
# 不主动抢占资源
-
nopreempt
-
authentication {
-
# 认证方式,可以是PASS或AH两种认证方式
-
auth_type PASS
-
# 认证密码
-
auth_pass 1111
-
}
-
virtual_ipaddress {
-
# 虚拟IP地址,随着state的变化而增加删除
-
192.168.32.66
-
}
-
}
-
virtual_server 192.168.32.66 3306 {
-
# 每个2秒检查一次real_server状态
-
delay_loop 2
-
# LVS算法
-
lb_algo wrr
-
# LVS模式
-
lb_kind DR
-
# 会话保持时间
-
persistence_timeout 60
-
protocol TCP
-
real_server 192.168.32.6 3306 {
-
# 权重
-
weight 3
-
# 检测到服务down后执行的脚本
-
notify_down /etc/keepalived/keepalived.sh
-
TCP_CHECK {
-
# 连接超时时间
-
connect_timeout 10
-
# 重连次数
-
nb_get_retry 3
-
# 重连间隔时间
-
delay_before_retry 3
-
# 健康检查端口
-
connect_port 3306
-
}
-
}
-
}
-
slave ip: 192.168.32.116
-
slave:/root/sbin# cat /etc/keepalived/keepalived.conf
-
global_defs {
-
router_id MySQL-ha
-
}
-
vrrp_instance VI_1{
-
# 在初始化状态下定义为主设备
-
state BACKUP
-
# 注意网卡接口
-
interface eth1
-
virtual_router_id 71
-
# 优先级,另一台改为90
-
priority 90
-
advert_int 1
-
# 不主动抢占资源
-
nopreempt
-
authentication {
-
# 认证方式,可以是PASS或AH两种认证方式
-
auth_type PASS
-
# 认证密码
-
auth_pass 1111
-
}
-
virtual_ipaddress {
-
# 虚拟IP地址,随着state的变化而增加删除
-
192.168.32.66
-
}
-
}
-
virtual_server 192.168.32.66 3306 {
-
# 每个2秒检查一次real_server状态
-
delay_loop 2
-
# LVS算法
-
lb_algo wrr
-
# LVS模式
-
lb_kind DR
-
# 会话保持时间
-
persistence_timeout 60
-
protocol TCP
-
real_server 192.168.32.116 3306 {
-
# 权重
-
weight 3
-
# 检测到服务down后执行的脚本
-
notify_down /etc/keepalived/keepalived.sh
-
TCP_CHECK {
-
# 连接超时时间
-
connect_timeout 10
-
# 重连次数
-
nb_get_retry 3
-
# 重连间隔时间
-
delay_before_retry 3
-
# 健康检查端口
-
connect_port 3306
-
}
-
}
-
}
-
mysql 主从切换;
-
master:
-
test:/root/sbin# ps -ef | grep mysql
-
root 29199 1 0 22:31 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data01/mysql --pid-file=/data01/mysql/test.pid
-
mysql 29531 29199 0 22:31 pts/0 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data01/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysql/mysqld.log --pid-file=/data01/mysql/test.pid --socket=/data01/mysql/mysql.sock
-
root 29597 27487 0 22:34 pts/0 00:00:00 grep mysql
-
test:/root/sbin# ps -ef | grep keep
-
root 29573 1 0 22:31 ? 00:00:00 /usr/local/sbin/keepalived -D -d -S 0
-
root 29574 29573 0 22:31 ? 00:00:00 /usr/local/sbin/keepalived -D -d -S 0
-
root 29575 29573 0 22:31 ? 00:00:00 /usr/local/sbin/keepalived -D -d -S 0
-
root 29599 27487 0 22:34 pts/0 00:00:00 grep keep
-
test:/root/sbin# ip add | grep inet
-
inet 127.0.0.1/8 scope host lo
-
inet6 ::1/128 scope host
-
inet 192.168.32.6/24 brd 192.168.32.255 scope global eth1
-
inet 192.168.32.66/32 scope global eth1
-
inet6 8001::20c:29ff:febf:dce8/64 scope global dynamic
-
inet6 fe80::20c:29ff:febf:dce8/64 scope link
-
slave:
-
slave:/root# ps -ef | grep mysql
-
root 9083 1 0 22:34 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data01/mysql --pid-file=/var/run/mysqld/mysqld.pid
-
mysql 9451 9083 4 22:34 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data01/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysql/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/data01/mysql/mysql.sock
-
root 9493 5676 0 22:34 pts/1 00:00:00 grep mysql
-
slave:/root# ps -ef | grep keep
-
root 7928 7907 0 22:23 pts/0 00:00:00 tail -f /var/log/keepalived/keepalived.log
-
root 9485 1 0 22:34 ? 00:00:00 /usr/local/sbin/keepalived -D -d -S 0
-
root 9486 9485 0 22:34 ? 00:00:00 /usr/local/sbin/keepalived -D -d -S 0
-
root 9487 9485 0 22:34 ? 00:00:00 /usr/local/sbin/keepalived -D -d -S 0
-
root 9514 5676 0 22:35 pts/1 00:00:00 grep keep
-
slave:/root# ip add | grep inet
-
inet 127.0.0.1/8 scope host lo
-
inet6 ::1/128 scope host
-
inet 192.168.32.116/24 brd 192.168.32.255 scope global eth1
-
inet6 8001::20c:29ff:fec2:2682/64 scope global dynamic
-
inet6 fe80::20c:29ff:fec2:2682/64 scope link
-
master 停掉数据库后:
-
test:/root/sbin# service mysql stop
-
Shutting down MySQL... [ OK ]
-
You have mail in /var/spool/mail/root
-
test:/root/sbin# ip add | grep inet
-
inet 127.0.0.1/8 scope host lo
-
inet6 ::1/128 scope host
-
inet 192.168.32.6/24 brd 192.168.32.255 scope global eth1
-
inet6 8001::20c:29ff:febf:dce8/64 scope global dynamic
-
inet6 fe80::20c:29ff:febf:dce8/64 scope link
-
test:/root/sbin# ps -ef | grep mysql
-
root 29676 27487 0 22:36 pts/0 00:00:00 grep mysql
-
test:/root/sbin# ps -ef | grep keep
-
root 29678 27487 0 22:36 pts/0 00:00:00 grep keep
-
slave上:
-
slave:/root# ip add | grep inet
-
inet 127.0.0.1/8 scope host lo
-
inet6 ::1/128 scope host
-
inet 192.168.32.116/24 brd 192.168.32.255 scope global eth1
-
inet 192.168.32.66/32 scope global eth1
-
inet6 8001::20c:29ff:fec2:2682/64 scope global dynamic
-
inet6 fe80::20c:29ff:fec2:2682/64 scope link
-
此时地址过来了:
-
当监测到端口down后执行的脚本;
-
slave:/root/sbin# cat /etc/keepalived/keepalived.sh
-
. ~/.bash_profile
-
pkill keepalived
-
perl /root/sbin/smail.pl
-
keepalived 监测到3306 mysql服务后;
-
Jul 16 22:02:12 slave Keepalived_healthcheckers[5816]: TCP connection to [192.168.32.116]:3306 success.
-
Jul 16 22:02:12 slave Keepalived_healthcheckers[5816]: Adding service [192.168.32.116]:3306 to VS [192.168.32.66]:3306
-
Jul 16 22:02:12 slave Keepalived_healthcheckers[5816]: Gained quorum 1+0=1 <= 3 for VS [192.168.32.66]:3306
-
关闭Mysql:
-
Jul 16 22:03:00 slave Keepalived_healthcheckers[5816]: TCP connection to [192.168.32.116]:3306 failed !!!
-
Jul 16 22:03:00 slave Keepalived_healthcheckers[5816]: Removing service [192.168.32.116]:3306 from VS [192.168.32.66]:3306
-
Jul 16 22:03:00 slave Keepalived_healthcheckers[5816]: Executing [/etc/keepalived/keepalived.sh] for service [192.168.32.116]:3306 in VS [192.168.32.66]:3306
-
Jul 16 22:03:00 slave Keepalived_healthcheckers[5816]: Lost quorum 1-0=1 > 0 for VS [192.168.32.66]:3
- --------------------- 本文来自 zhaoyangjian724 的优快云 博客 ,全文地址请点击:https://blog.youkuaiyun.com/zhaoyangjian724/article/details/51926868?utm_source=copy