Keepalived+HAProxy高可用搭建

2015-01-06 阅读: HAProxy

Keepalived实现了一套VRRP协议(虚拟路由协议),这个协议允许服务器可以动态的设置IP路由。 使用Keepalived可以加强负载均衡软件的可用性。

环境信息

192.168.61.3 ha1 - MASTER
192.168.61.4 ha2 - BACKUP

Keepalived编译安装

yum -y install openssl-devel

wget http://www.keepalived.org/software/keepalived-<version>.tar.gz
tar -zxvf keepalived-<version>.tar.gz
cd keepalived-<version>
./configure --prefix=/usr/local/keepalived

make
make install

安装psmisc

安装psmisc,可使用 killall -0 haproxy检查haproxy实例是否存活,性能高于ps。

yum install -y psmisc

配置

编辑配置文件/usr/local/keepalived/etc/keepalived/keepalived.conf:

! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL
}

vrrp_script chk_haproxy {
    script "/usr/bin/killall -0 haproxy"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    virtual_ipaddress {
        192.168.61.2
    }
    track_script {
      chk_haproxy
    }
}

配置文件关键配置如下:

  • vrrp_script chk_haproxy Haproxy健康检查
  • script "/usr/bin/killall -0 haproxy" 检查haproxy是否存活
  • interval 2 脚本运行周期
  • weight 2 每次检查加权权重
  • vrrp_instance VI_1 虚拟路由配置
  • state MASTER 本机实例状态,备机配置文件中填写BACKUP
  • interface eth0 本机网卡名称
  • virtual_router_id 虚拟路由编号,主机和备机要保持一致
  • priority 100 本机初始权重,备机填写小雨主机的权重值
  • advert_int 争抢虚拟地址的周期,单位秒
  • virtual_ipaddress 虚拟IP地址,主备机保持一致
  • track_script 对应的健康检查

启动keepalived

注册为系统服务/usr/lib/systemd/system/keepalived.service:

[Unit]
Description=LVS and VRRP High Availability Monitor
After=syslog.target network-online.target

[Service]
Type=simple
PIDFile=/var/run/keepalived.pid
KillMode=process
EnvironmentFile=-/usr/local/keepalived/etc/sysconfig/keepalived
ExecStart=/usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived/keepalived.conf $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

接下来启动keepalived:

systemctl enable keepalived
systemctl start keepalived

测试

在ha1上查看网卡eth0

ip addr sh eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:68:cc:c1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.61.3/24 brd 192.168.61.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet 192.168.61.2/32 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe68:ccc1/64 scope link
       valid_lft forever preferred_lft forever

可以看到目前虚IP被ha1持有。

在ha2上查看:

ip addr sh eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:68:cc:c1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.61.4/24 brd 192.168.61.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe68:ccc1/64 scope link tentative dadfailed
       valid_lft forever preferred_lft forever

ha2没有持有虚IP。

通过虚IP测试haproxy服务:

curl 192.168.61.2
Hello

停止ha1上的haproxy,一会虚IP会漂移到ha2上。

标题:Keepalived+HAProxy高可用搭建
本文链接:https://blog.frognew.com/2015/01/ha-using-keepalived-and-haproxy.html
转载请注明出处。

目录