[关闭]
@ruoli 2017-11-22T06:46:51.000000Z 字数 2472 阅读 2256

Linux下安装Keepalived实现双机热备

其他开源框架


1、准备工作

Linux主机:10.122.1.35
Linux主机:10.122.1.36
虚拟IP:10.122.1.30

2、安装Keepalived

两台Linux主机都需要安装Keepalived

1、下载Keepalived

下载至目录:/home/zz/user/keepalived

wget http://keepalived.org/software/keepalived-1.3.8.tar.gz

2、解压Keepalived

tar -zxvf keepalived-1.3.8.tar.gz

3、安装Keepalived

./configure

此步骤如果出现错误:libnfnetlink 库找不到,则需要执行:

cp -R usr/ /

然后在当前目录

make
make install

4、配置防火墙

firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 \
--in-interface enp0s31f6 --destination 224.0.0.18 --protocol vrrp -j ACCEPT


firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 \
--out-interface enp0s31f6 --destination 224.0.0.18 --protocol vrrp -j ACCEPT


firewall-cmd --reload
注意:【enp0s31f6】为网卡名称。

5、配置服务启动

cp /home/zz/user/keepalived/keepalived-1.3.8/keepalived/etc/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

6、配置Keepalived

配置文件路径为:

/etc/keepalived/keepalived.conf

修改配置如下:

  1. ! Configuration File for keepalived
  2. global_defs {
  3. notification_email {
  4. # acassen@firewall.loc
  5. # failover@firewall.loc
  6. # sysadmin@firewall.loc
  7. }
  8. notification_email_from Alexandre.Cassen@firewall.loc
  9. # smtp_server 192.168.200.1
  10. # smtp_connect_timeout 30
  11. router_id SERVER_1
  12. vrrp_skip_check_adv_addr
  13. vrrp_strict
  14. vrrp_garp_interval 0
  15. vrrp_gna_interval 0
  16. }
  17. vrrp_instance VI_1 {
  18. state MASTER
  19. interface enp0s25
  20. virtual_router_id 51
  21. priority 100
  22. protocol TCP
  23. advert_int 1
  24. authentication {
  25. auth_type PASS
  26. auth_pass 1111
  27. }
  28. virtual_ipaddress {
  29. 10.122.1.30/24
  30. }
  31. }
  32. virtual_server 10.122.1.30 3306 {
  33. delay_loop 2
  34. lb_algo wrr
  35. lb_kind DR
  36. nat_mask 255.255.255.0
  37. persistence_timeout 0
  38. protocol TCP
  39. real_server 10.122.1.35 3306 {
  40. weight 1
  41. MISC_CHECK {
  42. connect_timeout 60
  43. nb_get_retry 3
  44. delay_before_retry 3
  45. connect_port 3306
  46. misc_path "/home/zz/user/status_check/check_mysql.sh 10.122.1.35"
  47. misc_timeout 60
  48. misc_dynamic
  49. }
  50. }
  51. real_server 10.122.1.36 3306 {
  52. weight 1
  53. MISC_CHECK {
  54. connect_timeout 60
  55. nb_get_retry 3
  56. delay_before_retry 3
  57. connect_port 3306
  58. misc_path "/home/zz/user/status_check/check_mysql.sh 10.122.1.36"
  59. misc_timeout 60
  60. misc_dynamic
  61. }
  62. }
  63. }

从机配置与主机配置大致一致,主要不同的配置项如下

router_id SERVER_2
state BACKUP
priority 50

7、编写心跳检查脚本

check_mysql.sh

#!/bin/bash
MYSQL_PING=`/usr/local/mysql/bin/mysqladmin -u root -h $1 ping`
MYSQL_OK="mysqld is alive"
if [[ ${MYSQL_PING} == ${MYSQL_OK} ]]; then
echo "yes"
exit 0
else
echo "no"
exit 1
fi

8、启动Keepalived

  1. service keepalived start

查看日志:

tail -f /var/log/messages

9、判断是否脑裂

在主从服务器上都执行虚拟IP查看,如果虚拟IP都在自己身上,则发生了脑裂

ip addr|grep 10.122.1.30
发生脑裂主要原因为两台服务器之间无法进行心跳通讯,可检查防火墙或者进行串口通讯。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注