keepalived 结合mysql 自动切换

  • 启动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 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/zhaoyangjian724/article/details/51926868?utm_source=copy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Keepalived is a software that provides high availability and load balancing for Linux systems. It works by monitoring the health of servers and automatically redirecting traffic to healthy servers in case of failure. MySQL is an open-source relational database management system that is widely used in web applications. When combined, Keepalived and MySQL can provide a highly available and scalable database solution. To set up Keepalived with MySQL, you need to have at least two MySQL servers running on different machines. Keepalived should be installed on both machines. The first step is to configure Keepalived to monitor the health of the MySQL servers. This can be done by setting up a health check script that checks the status of the MySQL service. Once the health check is configured, Keepalived can be set up to redirect traffic to the healthy MySQL server. This can be done by configuring virtual IP addresses (VIPs) that are managed by Keepalived. The VIPs are assigned to the active MySQL server, and if the active server fails, the VIP is moved to the standby server. To ensure data consistency between the MySQL servers, you can use MySQL replication. This involves setting up a master-slave configuration where one MySQL server acts as the master and the other as the slave. The master server replicates its data to the slave server in real-time, ensuring that both servers have the same data. Overall, Keepalived and MySQL can provide a highly available and scalable database solution for web applications. By using Keepalived to monitor the health of MySQL servers and redirect traffic to healthy servers, and using MySQL replication to ensure data consistency, you can ensure that your database is always available and can handle high traffic loads.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值