Keepalived

0、准备工作

准备三台主机,比如如下

A主机:192.168.1.10
B主机:192.168.1.11 
C主机:192.168.1.12

准备一个vip,注意不要被其他主机占用,而且要与上面三个IP在同一子网内,比如为

192.168.1.20

1、安装

在三台主机上都安装keepalived,执行以下命令即可安装

sudo yum -y install keepalived

2、配置文件

keepalived有两个配置文件,一个是/etc/sysconfig/keepalived,一个是/etc/keepalived/keepalived.conf

  • /etc/sysconfig/keepalived

该文件中的有效内容只有一行,三个节点上这个文件的内容一样。为

KEEPALIVED_OPTIONS="-D"
  • /etc/keepalived/keepalived.conf

这个文件在三台主机上的配置会不一样

A主机

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER        # B C 主机设为BACKUP
    interface eth0      # IP对应的网卡
    virtual_router_id 20    # 三台主机这个id要一致,如果局域网内还有其他的vip,注意这个值要与其他vip不同
    priority 100    # MASTER设100,BACKUP设99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.20    # vip
    }
}

B C主机相同,如下

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state BACKUP        # B C 主机设为BACKUP
    interface eth0      # IP对应的网卡
    virtual_router_id 20    # 三台主机这个id要一致
    priority 99    # MASTER设100,BACKUP设99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.20    # vip
    }
}

3、unit文件

  • /usr/lib/systemd/system/keepalived.service

三台主机的内容一样,如下:

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

[Service]
Type=forking
KillMode=process
EnvironmentFile=-/etc/sysconfig/keepalived
ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

4、将keepalived设置为开机启动

sudo systemctl enable keepalived

Last updated