測試環(huán)境:Red Hat Enterprise Linux Server release 7.2 (Maipo)
虛擬IP:172.16.253.130
節(jié)點(diǎn)1:172.16.253.131
節(jié)點(diǎn)2:172.16.253.132
1.安裝依賴
yum -y?install?libnl libnl-devel openssl-devel
本地沒有yum源啄清,可以替換阿里云yum源 詳見:點(diǎn)我查看
2.下載keepalived文件
cd /usr/local
wget https://www.keepalived.org/software/keepalived-2.0.19.tar.gz
3.解壓文件
tar?xzf keepalived-2.0.19.tar.gz
cd?keepalived-2.0.19
?4.初始化配置
cd?keepalived-2.0.19/
./configure?--prefix=/usr/local/keepalived
5.編譯安裝
make?&&?make?install
6.配置
mkdir?/etc/keepalived
cp?/usr/local/keepalived/etc/keepalived/keepalived.conf?/etc/keepalived/
cp?/usr/local/keepalived/sbin/keepalived?/etc/init.d/
master 節(jié)點(diǎn)配置文件:keepalived.conf
! Configuration File for keepalived
global_defs {? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #全局定義部分
????notification_email {? ? ? ? ? ? ? ? ? #設(shè)置警報(bào)郵箱
????????acassen@firewall.loc? ? ? ? ? ? ? #郵箱
????}
? notification_email_from Alexandre.Cassen@firewall.loc? ? ? #設(shè)置發(fā)件人地址
? smtp_server 192.168.50.1? ? ? ? #設(shè)置smtp server地址
? smtp_connect_timeout 30? ? ? ? #設(shè)置smtp超時(shí)連接時(shí)間? ? 以上參數(shù)可以不配置
? router_id lb01? ? ? ? ? ? ? ? ? ? ? ? ? ?#是Keepalived服務(wù)器的路由標(biāo)識在一個(gè)局域網(wǎng)內(nèi)撵儿,這個(gè)標(biāo)識(router_id)是唯一的
}
vrrp_instance VI_1 {? ? ? #VRRP實(shí)例定義區(qū)塊名字是VI_1
????state MASTER? ? ? ? ? #表示當(dāng)前實(shí)例VI_1的角色狀態(tài)這個(gè)狀態(tài)只能有MASTER和BACKUP兩種狀態(tài),并且需要大寫這些字符ASTER為正式工作的狀態(tài)棵介,BACKUP為備用的狀態(tài)
????interface eno16777736
????virtual_router_id 130 #虛擬路由ID標(biāo)識,這個(gè)標(biāo)識最好是一個(gè)數(shù)字,在一個(gè)keepalived.conf配置中是唯一的, MASTER和BACKUP配置中相同實(shí)例的virtual_router_id必須是一致的.
????mcast_src_ip 172.16.253.131 #本機(jī)IP地址
????priority 100? ? ? ? ? ? ? ? #priority為優(yōu)先級 越大越優(yōu)先
????advert_int 1? ? ? ? ? ? ? #為同步通知間隔钉鸯。MASTER與BACKUP之間通信檢查的時(shí)間間隔帅矗,單位為秒绊汹,默認(rèn)為1.
????authentication {? ? ? ? ? #authentication為權(quán)限認(rèn)證配置不要改動,同一vrrp實(shí)例的MASTER與BACKUP使用相同的密碼才能正常通信永票。
????????auth_type PASS
????????auth_pass 1111
}
? ? virtual_ipaddress {? ? ? ? #設(shè)置虛擬IP地址
????????172.16.253.130/24? ? #/24可加可不加搂捧,具體看啟動是否正常驮俗,通過ip addr 查看對應(yīng)的/數(shù)值
????}
}
backup節(jié)點(diǎn)配置文件:keepalived.conf
! Configuration File for keepalived
global_defs {? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #全局定義部分
????notification_email {? ? ? ? ? ? ? ? ? #設(shè)置警報(bào)郵箱
????acassen@firewall.loc? ? ? ? ? ? ? #郵箱
????}
notification_email_from Alexandre.Cassen@firewall.loc? ? ? #設(shè)置發(fā)件人地址
smtp_server 192.168.50.1? ? ? ? #設(shè)置smtp server地址
smtp_connect_timeout 30? ? ? ? #設(shè)置smtp超時(shí)連接時(shí)間? ? 以上參數(shù)可以不配置
router_id lb01? ? ? ? ? ? ? ? ? ? ? ? ? ?#是Keepalived服務(wù)器的路由標(biāo)識在一個(gè)局域網(wǎng)內(nèi),這個(gè)標(biāo)識(router_id)是唯一的
}
vrrp_instance VI_1 {? ? ? #VRRP實(shí)例定義區(qū)塊名字是VI_1
????state BACKUP????#表示當(dāng)前實(shí)例VI_1的角色狀態(tài)這個(gè)狀態(tài)只能有MASTER和BACKUP兩種狀態(tài)允跑,并且需要大寫這些字符ASTER為正式工作的狀態(tài)王凑,BACKUP為備用的狀態(tài)
? ? interface eno16777736
? ? virtual_router_id 130 #虛擬路由ID標(biāo)識,這個(gè)標(biāo)識最好是一個(gè)數(shù)字,在一個(gè)keepalived.conf配置中是唯一的, MASTER和BACKUP配置中相同實(shí)例的virtual_router_id必須是一致的.
? ? mcast_src_ip 172.16.253.132 #本機(jī)IP地址
? ? priority 100? ? ? ? ? ? ? ? #priority為優(yōu)先級 越大越優(yōu)先
? ? advert_int 1? ? ? ? ? ? ? #為同步通知間隔。MASTER與BACKUP之間通信檢查的時(shí)間間隔聋丝,單位為秒索烹,默認(rèn)為1.
? ? authentication {? ? ? ? ? #authentication為權(quán)限認(rèn)證配置不要改動,同一vrrp實(shí)例的MASTER與BACKUP使用相同的密碼才能正常通信。
????????auth_type PASS
????????auth_pass 1111
????}
????virtual_ipaddress {? ? ? ? #設(shè)置虛擬IP地址
????????172.16.253.130/24? ? #/24可加可不加弱睦,具體看啟動是否正常百姓,通過ip addr 查看對應(yīng)的/數(shù)值
????}
}
注意:同一局域網(wǎng)內(nèi)有多個(gè)keepalive組存在時(shí),virtual_router_id 和router_id 不一致時(shí)况木,會出現(xiàn)ping 出現(xiàn)丟包
7.加入開機(jī)啟動
systemctl enable /usr/lib/systemd/system/keepalived.service
8.服務(wù)掛掉垒拢,自動重啟
vim? /usr/lib/systemd/system/keepalived.service
[Unit]
Description=LVS and VRRP High Availability Monitor
After=network-online.target syslog.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/keepalived.pid
KillMode=process
EnvironmentFile=-/usr/local/keepalived/etc/sysconfig/keepalived
ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
LimitCORE=infinity
LimitNOFILE=2000000
LimitNPROC=2000000
PrivateTmp=true
Restart=on-failure
[Install]
WantedBy=multi-user.target
保存后運(yùn)行以下命令生效
systemctl daemon-reload