云服务器

使用云服务器五分钟部署轻量化工具keepalived

2020-04-28 11:35:48 88

这期来讲一下在云服务器上部署keepalived+haproxy软件的实战,翻看往期有一篇关于轻量化实现web高可用的简单介绍,读者朋友有兴趣的话也可以阅读下。

 

软件安装

安装keepalived

使用yum安装或者源码装都可以,这里我们使用源码安装为例

    cd /opt
    wget https://www.keepalived.org/software/keepalived-2.0.17.tar.gz
    tar -zxvf keepalived-1.2.24.tar.gz
    cd keepalived-1.2.24
    ./configure --prefix=/opt/keepalived
     make && make install

 

keepalived配置

安装了keepalived的云服务器,可以设置为主备模式,以参数state来加以区别,通常master表示主角色,slave表示备角色,以下是一个实例的配置文件

 

主服务器

配置文件位置: /etc/keepalived/keepalived.conf

    ! Configuration File for keepalived
    global_defs {
       notification_email {
         xxxx@qq.com     //通知邮箱
       }
       notification_email_from xxx@qq.com    //通知邮箱
       smtp_connect_timeout 3
       smtp_server 127.0.0.1
       router_id master
    }
    vrrp_script chk_haproxy {
        script "/etc/keepalived/check_haproxy.sh"    //监测脚本
        interval 3
        weight 2
    }
    vrrp_instance VI_5 {
        state MASTER        //设置为master
        interface eth1
        virtual_router_id 145
        priority 105    //设置为优先级参数为100
        garp_master_delay 1
        authentication {
            auth_type PASS
            auth_pass passwd!Q@W#E        //设置vrrp密码
        }
        track_interface {
           eth1
        }
       virtual_ipaddress {
        121.121.121.121/24 dev eth1        //设置一个“虚拟ip”
    }
       track_script {
         chk_haproxy
    }
      notify_master "/etc/keepalived/notify.sh master 121.121.121.121"
      notify_backup "/etc/keepalived/notify.sh backup 121.121.121.121"
      notify_fault "/etc/keepalived/notify.sh fault 121.121.121.121"
    }

 

备服务器

配置文件位置: /etc/keepalived/keepalived.conf

    ! Configuration File for keepalived
    global_defs {
       notification_email {
         xxxx@qq.com     //通知邮箱
       }
       notification_email_from xxx@qq.com    //通知邮箱
       smtp_connect_timeout 3
       smtp_server 127.0.0.1
       router_id master
    }
    vrrp_script chk_haproxy {
        script "/etc/keepalived/check_haproxy.sh"    //监测脚本
        interval 3
        weight 2
    }
    vrrp_instance VI_5 {
        state SLVAE        //设置为slave
        interface eth1
        virtual_router_id 145
        priority 90    //设置为优先级参数为100
        garp_master_delay 1
        authentication {
            auth_type PASS
            auth_pass passwd!Q@W#E        //设置vrrp密码
        }
        track_interface {
           eth1
        }
       virtual_ipaddress {
        121.121.121.121/24 dev eth1        //设置一个“虚拟ip”
    }
       track_script {
         chk_haproxy
    }
      notify_master "/etc/keepalived/notify.sh master 121.121.121.121"
      notify_backup "/etc/keepalived/notify.sh backup 121.121.121.121"
      notify_fault "/etc/keepalived/notify.sh fault 121.121.121.121"
    }

 

启动keeplived服务

keepalived -D -f /opt/keepalived/keepalived.conf

 

查看打印的日志文件

看到下面打印的日志,已经知道“虚拟IP”已经成功启动,此时使用ifconfig就可以看到“虚拟IP”地址已经配置到了主服务器上。
[root@srv4 ~]# tail -f /var/log/messages
Sep 20 01:45:29 srv4 Keepalived_vrrp: Configuration is using : 34546 Bytes
Sep 20 01:45:29 srv4 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)]
Sep 20 01:45:30 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.100
Sep 20 01:45:31 srv4 Keepalived_vrrp: Netlink reflector reports IP 121.121.121.121 added
Sep 20 01:45:31 srv4 Keepalived_healthcheckers: Netlink reflector reports IP 121.121.121.121 added
Sep 20 01:45:31 srv4 avahi-daemon[4029]: Registering new address record for 121.121.121.121 on eth1.
Sep 20 01:45:36 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 121.121.121.121

 

以下附上笔者一个keepalived监控脚本

keepalived有相关的程序监控脚本,keepalived会定时在主备服务器上都执行该脚本文件,并根据执行之后的结果来判定主备服务器的状态,从而进行VIP的漂移选择。

    vim /etc/keepalived/notify.sh 
    # vi /etc/keepalived/notify.sh
    #!/bin/bash
    # Author: jinghong <x@xx.cc>
    # description: An example of notify script
    #
    contact='xxx@xx.cc'
    notify() {
      mailsubject=" `date '+%F %H:%M:%S'`: 研发项目:绿色出行项目角色变更通知,"
      mailbody="
    ####项目角色变更通知######\n
       echo "$mailbody" | mail -s "$mailsubject" $contact
    }
    case "$1" in
      master)
        notify master $2
        killall haproxy
        /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
        exit 0
      ;;
      backup)
        notify backup $2
        killall haproxy 
        exit 0
      ;;
      fault)
        notify fault $2
        killall haproxy 
        exit 0
      ;;
      *)
        echo 'Usage: `basename $0` {master|backup|fault}'
        exit 1
      ;;
    esac

 

实际使用过程中可能遇到的问题

不同的云厂家,可能对vrrp包有做限制,笔者就在测试过阿X云的过程中,遇到双方收不到VRRP包的情况,较大概率是交换机层面对此进行了限制,因此在实际的使用过程中,也使用了单播IP的方式,也解决了该问题。

睿江云官网链接:www.eflycloud.com

上一篇: 无

微信关注

获取更多技术咨询