KeepAlived 搭建高可用的HAProxy集群

KeepAlived 搭建高可用的HAProxy集群

  1. KeepAlived 简介

    Keepalived,它是一个高性能的服务器高可用或热备解决方案,Keepalived主要来防止服务器单点故障的发生问题,可以通过其与Nginx、Haproxy等反向代理的负载均衡服务器配合实现web服务端的高可用。Keepalived以VRRP协议为实现基础,用VRRP协议来实现高可用性(HA).VRRP(Virtual Router Redundancy Protocol)协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成一个设备,对外提供虚拟路由器IP(一个或多个)。

  2. KeepAlived 的安装

    #安装所需软件包
    yum install -y openssl openssl-devel
    #下载安装包
    wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz
    #解压、编译、安装
    tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/
    cd /usr/local/keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived
    make && make install
    # 将keepalived安装成Linux系统服务,因为没有使用keepalived的默认安装路径(默认路径:/usr/local),安装完成之后,需要做一些修改工作
    #首先创建文件夹,将keepalived配置文件进行复制:
    mkdir /etc/keepalived
    cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
    #然后复制keepalived脚本文件:
    cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
    cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
    # 设置的过程中如果出现已经存在则删除原有的文件,重新创建
    ln -s -f /usr/local/sbin/keepalived /usr/sbin/
    ln -s -f /usr/local/keepalived/sbin/keepalived /sbin/
    #可以设置开机启动:chkconfig keepalived on,到此我们安装完毕!
    chkconfig keepalived on
    
  3. KeepAlived 的配置

    [root@nacos keepalived]# cat keepalived.conf
    ! Configuration File for keepalived
    global_defs {
       router_id other  ##标识节点的字符串,通常为本机hostname
    }
    vrrp_script chk_haproxy {
        script "/etc/keepalived/haproxy_check.sh"  ##执行脚本位置
        interval 2  ##检测时间间隔
        weight -20  ##如果条件成立则权重减20
    }
    vrrp_instance VI_1 {
        state BACKUP  ## 主节点为MASTER,备份节点为BACKUP-该配置非常重要
        interface ens192 ## 绑定虚拟IP的网络接口(网卡可以使用ifconfig查看)
        virtual_router_id 110  ## 虚拟路由ID号(主备节点一定要相同)-该配置非常重要
        mcast_src_ip 192.168.1.118 ## 本机ip地址
        priority 150  ##优先级配置(0-254的值),一般主节点的权重大于备份节点
        nopreempt
        advert_int 1  ## 组播信息发送间隔,俩个节点必须配置一致,默认1s
    authentication {  ## 认证匹配
            auth_type PASS
            auth_pass dalianpai
        }
        track_script {
            chk_haproxy
        }
        virtual_ipaddress {
            192.168.1.199  ## 虚拟ip,可以指定多个,以后连接mq就使用该虚拟ip进行连接
        }
    }
    [root@nacos keepalived]#
    
    

4.编写执行脚本(一定要赋权否则不能执行)

[root@nacos keepalived]# cat haproxy_check.sh
#!/bin/bash
COUNT=`ps -C haproxy --no-header |wc -l`
if [ $COUNT -eq 0 ];then
    /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    sleep 2
    if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi
[root@nacos keepalived]#
  1. 启动

    [root@other keepalived]# chmod +x /etc/keepalived/haproxy_check.sh
    [root@other keepalived]# service keepalived start
    Starting keepalived (via systemctl):                       [  OK  ]
    [root@other keepalived]# ps -ef | grep haproxy
    nobody    2143     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2144     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2145     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2146     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2147     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2148     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2149     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2150     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2151     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2152     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2153     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2154     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2155     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2156     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2157     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2158     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2159     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2160     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2161     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    nobody    2162     1  0 Jul08 ?        00:00:02 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
    root      3713  1754  0 02:45 pts/0    00:00:00 grep --color=auto haproxy
    [root@other keepalived]# ps -ef | grep keepalived
    root      3669     1  0 02:44 ?        00:00:00 keepalived -D
    root      3670  3669  0 02:44 ?        00:00:00 keepalived -D
    root      3671  3669  0 02:44 ?        00:00:00 keepalived -D
    root      3718  1754  0 02:45 pts/0    00:00:00 grep --color=auto keepalived
    [root@other keepalived]# ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> 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: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether 00:50:56:b4:2f:fe brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.121/24 brd 192.168.1.255 scope global noprefixroute ens192
           valid_lft forever preferred_lft forever
        inet 192.168.1.199/32 scope global ens192
           valid_lft forever preferred_lft forever
        inet6 fe80::c1bd:70bd:56f9:d8f5/64 scope link tentative noprefixroute dadfailed
           valid_lft forever preferred_lft forever
        inet6 fe80::2e50:2dd3:3d4d:2c2e/64 scope link tentative noprefixroute dadfailed
           valid_lft forever preferred_lft forever
        inet6 fe80::68b:4e0e:e486:8dd8/64 scope link tentative noprefixroute dadfailed
           valid_lft forever preferred_lft forever
    3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
        link/ether 02:42:2a:41:5e:74 brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
           valid_lft forever preferred_lft forever
    [root@other keepalived]# service keepalived stop
    Stopping keepalived (via systemctl):                       [  OK  ]
    [root@other keepalived]# systemctl start keepalived
    [root@other keepalived]# systemctl status keepalived
    ● keepalived.service - SYSV: Start and stop Keepalived
       Loaded: loaded (/etc/rc.d/init.d/keepalived; bad; vendor preset: disabled)
       Active: active (running) since Thu 2020-07-09 02:54:17 EDT; 6s ago
         Docs: man:systemd-sysv-generator(8)
      Process: 3869 ExecStop=/etc/rc.d/init.d/keepalived stop (code=exited, status=0/SUCCESS)
      Process: 3888 ExecStart=/etc/rc.d/init.d/keepalived start (code=exited, status=0/SUCCESS)
     Main PID: 3895 (keepalived)
        Tasks: 3
       Memory: 904.0K
       CGroup: /system.slice/keepalived.service
               ├─3895 keepalived -D
               ├─3896 keepalived -D
               └─3897 keepalived -D
    
    Jul 09 02:54:17 other Keepalived_vrrp[3897]: Opening file '/etc/keepalived/keepalived.conf'.
    Jul 09 02:54:17 other Keepalived_vrrp[3897]: Truncating auth_pass to 8 characters
    Jul 09 02:54:17 other Keepalived_vrrp[3897]: Configuration is using : 67374 Bytes
    Jul 09 02:54:17 other Keepalived_vrrp[3897]: Using LinkWatch kernel netlink reflector...
    Jul 09 02:54:17 other Keepalived_vrrp[3897]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
    Jul 09 02:54:17 other Keepalived_healthcheckers[3896]: Using LinkWatch kernel netlink reflector...
    Jul 09 02:54:17 other Keepalived_vrrp[3897]: VRRP_Script(chk_haproxy) succeeded
    Jul 09 02:54:18 other Keepalived_vrrp[3897]: VRRP_Instance(VI_1) Transition to MASTER STATE
    Jul 09 02:54:18 other Keepalived_vrrp[3897]: VRRP_Instance(VI_1) Received higher prio advert
    Jul 09 02:54:18 other Keepalived_vrrp[3897]: VRRP_Instance(VI_1) Entering BACKUP STATE
    [root@other keepalived]#
    
    

    停止主节点的keepalived查看我们的虚拟ip是否漂移到了从节点

posted @ 2020-07-09 14:58  天宇轩-王  阅读(321)  评论(0编辑  收藏  举报