Linux安装Keepalived

一、卸载Keepalived

# 查看安装版本
rpm -qi keepalived

# yum卸载
yum remove keepalived 

# 找出残留的配置文件
find / -name keepalived
/etc/selinux/targeted/tmp/modules/100/keepalived
/etc/selinux/targeted/active/modules/100/keepalived

# 删除配置文件
rm -rf /etc/selinux/targeted/tmp/modules/100/keepalived
rm -rf /etc/selinux/targeted/active/modules/100/keepalived

二、安装Keepalived

yum -y install keepalived

Last metadata expiration check: 0:41:09 ago on Wed 09 Nov 2022 02:13:14 PM CST.
Dependencies resolved.
=================================================================================================================================================================================================================================================
 Package                                                             Architecture                                    Version                                                    Repository                                                  Size
=================================================================================================================================================================================================================================================
Installing:
 keepalived                                                          x86_64                                          2.1.5-8.el8                                                ol8_appstream                                              536 k
Installing dependencies:
 mariadb-connector-c                                                 x86_64                                          3.1.11-2.el8_3                                             ol8_appstream                                              200 k
 mariadb-connector-c-config                                          noarch                                          3.1.11-2.el8_3                                             ol8_appstream                                               15 k
 net-snmp-agent-libs                                                 x86_64                                          1:5.8-25.el8                                               ol8_appstream                                              748 k
 net-snmp-libs                                                       x86_64                                          1:5.8-25.el8                                               ol8_baseos_latest                                          827 k

Transaction Summary
=================================================================================================================================================================================================================================================
Install  5 Packages

Total download size: 2.3 M
Installed size: 7.3 M
Downloading Packages:
(1/5): mariadb-connector-c-3.1.11-2.el8_3.x86_64.rpm                                                                                                                                                              16 kB/s | 200 kB     00:12    
(2/5): mariadb-connector-c-config-3.1.11-2.el8_3.noarch.rpm                                                                                                                                                       17 kB/s |  15 kB     00:00    
(3/5): net-snmp-agent-libs-5.8-25.el8.x86_64.rpm                                                                                                                                                                  91 kB/s | 748 kB     00:08    
(4/5): keepalived-2.1.5-8.el8.x86_64.rpm                                                                                                                                                                          20 kB/s | 536 kB     00:26    
(5/5): net-snmp-libs-5.8-25.el8.x86_64.rpm                                                                                                                                                                        30 kB/s | 827 kB     00:27    
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                             84 kB/s | 2.3 MB     00:27     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                                                         1/1 
  Installing       : net-snmp-libs-1:5.8-25.el8.x86_64                                                                                                                                                                                       1/5 
  Installing       : mariadb-connector-c-config-3.1.11-2.el8_3.noarch                                                                                                                                                                        2/5 
  Installing       : mariadb-connector-c-3.1.11-2.el8_3.x86_64                                                                                                                                                                               3/5 
  Installing       : net-snmp-agent-libs-1:5.8-25.el8.x86_64                                                                                                                                                                                 4/5 
  Installing       : keepalived-2.1.5-8.el8.x86_64                                                                                                                                                                                           5/5 
  Running scriptlet: keepalived-2.1.5-8.el8.x86_64                                                                                                                                                                                           5/5 
  Verifying        : net-snmp-libs-1:5.8-25.el8.x86_64                                                                                                                                                                                       1/5 
  Verifying        : keepalived-2.1.5-8.el8.x86_64                                                                                                                                                                                           2/5 
  Verifying        : mariadb-connector-c-3.1.11-2.el8_3.x86_64                                                                                                                                                                               3/5 
  Verifying        : mariadb-connector-c-config-3.1.11-2.el8_3.noarch                                                                                                                                                                        4/5 
  Verifying        : net-snmp-agent-libs-1:5.8-25.el8.x86_64                                                                                                                                                                                 5/5 

Installed:
  keepalived-2.1.5-8.el8.x86_64          mariadb-connector-c-3.1.11-2.el8_3.x86_64          mariadb-connector-c-config-3.1.11-2.el8_3.noarch          net-snmp-agent-libs-1:5.8-25.el8.x86_64          net-snmp-libs-1:5.8-25.el8.x86_64         

Complete!

Keepalived基础命令:


# 启动 keepalived
systemctl start keepalived  

# 加入开机启动 keepalived
systemctl enable keepalived 

# 重新启动 keepalived
systemctl restart keepalived  

# 查看 keepalived 状态
systemctl status keepalived   

三、配置Haproxy验证脚本

cat check_haproxy.sh 

#!/usr/bin/env bash
if [ $(pgrep haproxy|wc -l) -eq 0 ];then
    sudo systemctl start haproxy
    sleep 3
    if [ $(pgrep haproxy|wc -l) -eq 0 ];then
       sudo systemctl stop keepalived
    fi
fi

四、配置Keepalived

1、MASTER

global_defs {
   router_id haproxy
   script_user root
   enable_script_security
}
vrrp_script chk_haproxy {
    script "/etc/keepalived/check_haproxy.sh"
    interval 2
    weight -20
}
vrrp_instance VI_1 {
    state MASTER
    interface ens32
    virtual_router_id 55
    mcast_src_ip 10.95.34.38
    nopreempt
    priority 89
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.95.34.65
    }
    track_script {
        chk_haproxy
    }
}

2、BACKUP

global_defs {
   router_id haproxy
   script_user root
   enable_script_security
}
vrrp_script chk_haproxy {
    script "/etc/keepalived/check_haproxy.sh"
    interval 2
    weight -20
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens32
    virtual_router_id 55
    nopreempt
    mcast_src_ip 10.95.34.37
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.95.34.65
    }
    track_script {
        chk_haproxy
    }
}

3、启动Keepalived

ip addr
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens32:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:be:d4:61 brd ff:ff:ff:ff:ff:ff
    inet 10.95.34.37/24 brd 10.95.34.255 scope global noprefixroute ens32
       valid_lft forever preferred_lft forever
    inet 10.95.34.65/32 scope global ens32
       valid_lft forever preferred_lft forever

可以看到,多出来一条虚拟ip的记录。当node1的节点宕机或者keepalived挂了的时候,就会自动切换到节点2上面,可以在节点2看到这一条记录信息。

五、验证

​​​​​​​Linux安装Keepalived_第1张图片

六、安装异常情况(暂未完全实践)

1、keepalived执行后日志狂刷,2.脚本并没有执行(down.sh),具体情况就是haproxy挂掉后,keeplived并没有自杀,导致vip漂移失败,但是手动关闭keeplived后会实现漂移,原因就在于down脚本没有得到执行。

很纠结,经过排查,原因是ip_vs模块系统默认没有自动加载,执行命令:

查看
lsmod | grep ip_vs
(如果没有任何输出则表示ip_vs模块并没有被内核加载)
 
手动加载
modprobe ip_vs
 
modprobe ip_vs_wrr

你可能感兴趣的:(云原生,中间件,分布式,linux,服务器,运维)