• Linux安装Keepalived


    一、卸载Keepalived

    1. # 查看安装版本
    2. rpm -qi keepalived
    3. # yum卸载
    4. yum remove keepalived
    5. # 找出残留的配置文件
    6. find / -name keepalived
    7. /etc/selinux/targeted/tmp/modules/100/keepalived
    8. /etc/selinux/targeted/active/modules/100/keepalived
    9. # 删除配置文件
    10. rm -rf /etc/selinux/targeted/tmp/modules/100/keepalived
    11. rm -rf /etc/selinux/targeted/active/modules/100/keepalived

    二、安装Keepalived

    1. yum -y install keepalived
    2. Last metadata expiration check: 0:41:09 ago on Wed 09 Nov 2022 02:13:14 PM CST.
    3. Dependencies resolved.
    4. =================================================================================================================================================================================================================================================
    5. Package Architecture Version Repository Size
    6. =================================================================================================================================================================================================================================================
    7. Installing:
    8. keepalived x86_64 2.1.5-8.el8 ol8_appstream 536 k
    9. Installing dependencies:
    10. mariadb-connector-c x86_64 3.1.11-2.el8_3 ol8_appstream 200 k
    11. mariadb-connector-c-config noarch 3.1.11-2.el8_3 ol8_appstream 15 k
    12. net-snmp-agent-libs x86_64 1:5.8-25.el8 ol8_appstream 748 k
    13. net-snmp-libs x86_64 1:5.8-25.el8 ol8_baseos_latest 827 k
    14. Transaction Summary
    15. =================================================================================================================================================================================================================================================
    16. Install 5 Packages
    17. Total download size: 2.3 M
    18. Installed size: 7.3 M
    19. Downloading Packages:
    20. (1/5): mariadb-connector-c-3.1.11-2.el8_3.x86_64.rpm 16 kB/s | 200 kB 00:12
    21. (2/5): mariadb-connector-c-config-3.1.11-2.el8_3.noarch.rpm 17 kB/s | 15 kB 00:00
    22. (3/5): net-snmp-agent-libs-5.8-25.el8.x86_64.rpm 91 kB/s | 748 kB 00:08
    23. (4/5): keepalived-2.1.5-8.el8.x86_64.rpm 20 kB/s | 536 kB 00:26
    24. (5/5): net-snmp-libs-5.8-25.el8.x86_64.rpm 30 kB/s | 827 kB 00:27
    25. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    26. Total 84 kB/s | 2.3 MB 00:27
    27. Running transaction check
    28. Transaction check succeeded.
    29. Running transaction test
    30. Transaction test succeeded.
    31. Running transaction
    32. Preparing : 1/1
    33. Installing : net-snmp-libs-1:5.8-25.el8.x86_64 1/5
    34. Installing : mariadb-connector-c-config-3.1.11-2.el8_3.noarch 2/5
    35. Installing : mariadb-connector-c-3.1.11-2.el8_3.x86_64 3/5
    36. Installing : net-snmp-agent-libs-1:5.8-25.el8.x86_64 4/5
    37. Installing : keepalived-2.1.5-8.el8.x86_64 5/5
    38. Running scriptlet: keepalived-2.1.5-8.el8.x86_64 5/5
    39. Verifying : net-snmp-libs-1:5.8-25.el8.x86_64 1/5
    40. Verifying : keepalived-2.1.5-8.el8.x86_64 2/5
    41. Verifying : mariadb-connector-c-3.1.11-2.el8_3.x86_64 3/5
    42. Verifying : mariadb-connector-c-config-3.1.11-2.el8_3.noarch 4/5
    43. Verifying : net-snmp-agent-libs-1:5.8-25.el8.x86_64 5/5
    44. Installed:
    45. 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
    46. Complete!

    Keepalived基础命令:

    1. # 启动 keepalived
    2. systemctl start keepalived
    3. # 加入开机启动 keepalived
    4. systemctl enable keepalived
    5. # 重新启动 keepalived
    6. systemctl restart keepalived
    7. # 查看 keepalived 状态
    8. systemctl status keepalived

    三、配置Haproxy验证脚本

    1. cat check_haproxy.sh
    2. #!/usr/bin/env bash
    3. if [ $(pgrep haproxy|wc -l) -eq 0 ];then
    4. sudo systemctl start haproxy
    5. sleep 3
    6. if [ $(pgrep haproxy|wc -l) -eq 0 ];then
    7. sudo systemctl stop keepalived
    8. fi
    9. fi

    四、配置Keepalived

    1、MASTER

    1. global_defs {
    2. router_id haproxy
    3. script_user root
    4. enable_script_security
    5. }
    6. vrrp_script chk_haproxy {
    7. script "/etc/keepalived/check_haproxy.sh"
    8. interval 2
    9. weight -20
    10. }
    11. vrrp_instance VI_1 {
    12. state MASTER
    13. interface ens32
    14. virtual_router_id 55
    15. mcast_src_ip 10.95.34.38
    16. nopreempt
    17. priority 89
    18. advert_int 1
    19. authentication {
    20. auth_type PASS
    21. auth_pass 1111
    22. }
    23. virtual_ipaddress {
    24. 10.95.34.65
    25. }
    26. track_script {
    27. chk_haproxy
    28. }
    29. }

    2、BACKUP

    1. global_defs {
    2. router_id haproxy
    3. script_user root
    4. enable_script_security
    5. }
    6. vrrp_script chk_haproxy {
    7. script "/etc/keepalived/check_haproxy.sh"
    8. interval 2
    9. weight -20
    10. }
    11. vrrp_instance VI_1 {
    12. state BACKUP
    13. interface ens32
    14. virtual_router_id 55
    15. nopreempt
    16. mcast_src_ip 10.95.34.37
    17. priority 100
    18. advert_int 1
    19. authentication {
    20. auth_type PASS
    21. auth_pass 1111
    22. }
    23. virtual_ipaddress {
    24. 10.95.34.65
    25. }
    26. track_script {
    27. chk_haproxy
    28. }
    29. }

    3、启动Keepalived

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

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

    五、验证

    ​​​​​​​

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

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

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

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

  • 相关阅读:
    从入门到精通,收下这 22 个 Python 学习网站
    [计算机动画]Games103-作业1-刚体动画
    Apache Hive概述,模拟实现Hive功能,Hive基础架构
    FME读写cass数据的方案及操作流程
    怎么从0到1实现一个PHP框架?
    [附源码]计算机毕业设计springboot交通事故档案管理系统
    如何在2023年学习React
    亚信安全入选中国数据安全市场图谱
    python_argparse模块的使用
    flutter 记录学习不一样的动画(二)
  • 原文地址:https://blog.csdn.net/summer_fish/article/details/127769908