keepalive安装使用

一、安装keepalived

yum安装:

#yum list keepalived

#yum install keepalived

#rpm -ql keepalived

#systemctl start keepalived

编译安装

#wget https://www.keepalived.org/software/keepalived-2.0.19.tar.gz #下载源码包

#tar -xvf keepalived-2.0.19.tar.gz -C /apps

#cd /apps/keepalived-2.0.19/

# yum install curl gcc autoconf automake openssl-devel libnl3-devel iptables-devel ipset-devel net-snmp-devel libnfnetlink-devel file-devel #安装keepalived所需要的编译工具和依赖包

# ./build_setup

#./configure –prefix=/usr/local/keepalived –with-default-config-file=/usr/local/keepalived/etc/keepalived/keepalived.conf #配置安装选项

# make && make install

#cp /usr/local/keepalived/etc/keepalived/{keepalived.conf,keepalived.conf.bak} #备份配置文件

#vim /usr/local/keepalived/etc/keepalived/keepalived.conf

vrrp_instance VI_1 {

state MASTER

interface ens33 #网卡名

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111 #认证密码

}

virtual_ipaddress {

172.20.56.47/16 dev ens33 label ens33:0 #增加网卡子接口IP

}

}

#systemctl start keepalived

#ps aux |grep keepalived

#ip a

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

link/ether 00:0c:29:43:4c:aa brd ff:ff:ff:ff:ff:ff

inet 172.20.56.7/16 brd 172.20.255.255 scope global noprefixroute ens33

valid_lft forever preferred_lft forever

inet 172.20.56.47/16 scope global secondary ens33:0 #网卡子IP添加成功

valid_lft forever preferred_lft forever

inet6 fe80::1d36:fd69:3afb:d8b8/64 scope link noprefixroute

valid_lft forever preferred_lft forever

二、多播模式

准备两台相同环境的centos主机,使用yum安装keepalived,配置如下:

主机1:

vim /etc/keepalived/keepalived.conf #修改如下配置

# vrrp_strict #注释掉strict模式,这样iptables将不再自动添加拒绝vip策略

vrrp_instance VI_1 {

state MASTER #主模式,优先抢占VIP

interface ens33 #绑定的主机网卡

virtual_router_id 56 #如果同一网络中已有其它keepalived主机组,该id不能和其它主机组一样

priority 100 #主机优先机

advert_int 1

virtual_ipaddress {

172.20.56.107/16 dev ens33 label ens33:0 #虚拟IP,使用主机子网卡模式添加

}

}

主机2:

vim /etc/keepalived/keepalived.conf #修改如下配置

# vrrp_strict

vrrp_instance VI_1 {

state BACKUP #备用模式,当主模式的服务故障时启用抢占VIP

interface ens33

virtual_router_id 56 #此ID需和组中其它ID一致

priority 40 #优先级,低于MASTER主机

advert_int 1

virtual_ipaddress {

172.20.56.107/16 dev ens33 label ens33:0

}

}

修改完成后启动keepalived服务,查看网卡IP地址

主机1:

#systemctl start keepalived

#ip a

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

link/ether 00:0c:29:43:4c:aa brd ff:ff:ff:ff:ff:ff

inet 172.20.56.7/16 brd 172.20.255.255 scope global noprefixroute ens33

valid_lft forever preferred_lft forever

inet 172.20.56.107/16 scope global secondary ens33:0

valid_lft forever preferred_lft forever

inet6 fe80::1d36:fd69:3afb:d8b8/64 scope link noprefixroute

valid_lft forever preferred_lft forever

主机2:

#systemctl start keepalived

#ip a

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

link/ether 00:0c:29:a0:15:26 brd ff:ff:ff:ff:ff:ff

inet 172.20.56.17/16 brd 172.20.255.255 scope global noprefixroute ens33

valid_lft forever preferred_lft forever

inet6 fe80::1d36:fd69:3afb:d8b8/64 scope link tentative noprefixroute dadfailed

valid_lft forever preferred_lft forever

inet6 fe80::c6d2:64a:416:30de/64 scope link noprefixroute

valid_lft forever preferred_lft forever

可以看到VIP被主机1抢占,将主机1的keepalived服务停止,然后在主机2上可以看到VIP地址被主机2抢占

主机1:

#systemctl stop keepalived

主机2

#ip a

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

link/ether 00:0c:29:a0:15:26 brd ff:ff:ff:ff:ff:ff

inet 172.20.56.17/16 brd 172.20.255.255 scope global noprefixroute ens33

valid_lft forever preferred_lft forever

inet 172.20.56.107/16 scope global secondary ens33:0

valid_lft forever preferred_lft forever

inet6 fe80::1d36:fd69:3afb:d8b8/64 scope link tentative noprefixroute dadfailed

valid_lft forever preferred_lft forever

inet6 fe80::c6d2:64a:416:30de/64 scope link noprefixroute

valid_lft forever preferred_lft forever

三、单播模式

主机1:

#vim /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {

state MASTER

interface ens33

virtual_router_id 56

priority 100

advert_int 1

unicast_src_ip 172.20.56.7 #源IP地址配置为本机IP

unicast_peer {

172.20.56.17 #广播地址配置为组中的其它主机,在这里只配置主机2的IP地址

}

virtual_ipaddress {

172.20.56.107/16 dev ens33 label ens33:0

}

}

主机2:

#vim /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {

state BACKUP

interface ens33

virtual_router_id 56

priority 40

advert_int 1

unicast_src_ip 172.20.56.17 #源地址为本机IP地址

unicast_peer {

172.20.56.7 #广播地址为组中其它主机地址,此处为主机1IP地址

}

virtual_ipaddress {

172.20.56.107/16 dev ens33 label ens33:0

}

}

单播模式和多播模式的区别在于使用单播模式时只有keepalive主机组的主机才能收到vrrp广播,而多播模式下局域网中的所有主机都可以收到vrrp广播。

四、非抢占模式

主机1:

#vim /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {

state BACKUP #非抢占模式下所有主机配置为备用模式

interface ens33

virtual_router_id 56

priority 100

advert_int 1

nopreempt #添加不抢占配置

unicast_src_ip 172.20.56.7

unicast_peer {

172.20.56.17

}

virtual_ipaddress {

172.20.56.107/16 dev ens33 label ens33:0

}

}

主机2:

#vim /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {

state BACKUP

interface ens33

virtual_router_id 56

priority 40

advert_int 1

nopreempt #添加不抢占配置

unicast_src_ip 172.20.56.17

unicast_peer {

172.20.56.7

}

virtual_ipaddress {

172.20.56.107/16 dev ens33 label ens33:0

}

}

说明:在非抢占模式下,只有当占用VIP的主机故障时其它主机会主动抢占VIP,当故障的主机重新上线时,不再抢占VIP,即使优先级高于其它主机。

五、双主模式

主机1:

#vim /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {

state MASTER

interface ens33

virtual_router_id 56

priority 100

advert_int 1

unicast_src_ip 172.20.56.7

unicast_peer {

172.20.56.17

}

virtual_ipaddress {

172.20.56.107/16 dev ens33 label ens33:0

}

}

vrrp_instance VI_2 { #添加新的keepalived组

state BACKUP #使用BACKUP模式

interface ens33

virtual_router_id 156 #此IP要区别于keepalived1组中的ID

priority 40

advert_int 1

unicast_src_ip 172.20.56.7

unicast_peer {

172.20.56.17

}

virtual_ipaddress {

172.20.56.207/16 dev ens33 label ens33:0 #使用新的VIP地址

}

}

主机2:

#vim /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {

state BACKUP

interface ens33

virtual_router_id 56

priority 40

advert_int 1

unicast_src_ip 172.20.56.17

unicast_peer {

172.20.56.7

}

virtual_ipaddress {

172.20.56.107/16 dev ens33 label ens33:0

}

}

vrrp_instance VI_2 { #添加新的keepalive组,对应主机1的第2组

state MASTER #使用MASTER模式

interface ens33

virtual_router_id 156 #此ID对应主机1的第2组ID

priority 100

advert_int 1

unicast_src_ip 172.20.56.17

unicast_peer {

172.20.56.7

}

virtual_ipaddress {

172.20.56.207/16 dev ens33 label ens33:0 #VIP和主机1的第二组VIP相同

}

}

说明:双主模式在keepalive中的每个主机上使用一个MASTER组,两个或多个主机各为主备,当一个主机故障时两个VIP将转移到同一个主机上。

六、邮件配置

使用邮件通知功能当VIP转移时可及时收到通知,以便处理故障。

1.安装mailx包

yum install mailx -y

配置mail,也可以使用其它邮箱,此处为QQ邮箱

vim /etc/mail.rc #在文件末尾添加以下内容

set bsdcompat

set from=6666666@qq.com #使用qq邮箱

set smtp=smtp.qq.com

set smtp-auth-user=66666666@qq.com

set smtp-auth-password=password #此密码并非QQ邮箱密码,是通过QQ邮箱的设置—-帐户—-开启POP3/SMTP 获取到的发信验证码

set smtp-auth=login

set ssl-verify=ignore

2.编写邮箱发送脚本,也可以使用python脚本,此处为shell脚本

vim /etc/keepalived/notify.sh

#!/bin/bash

contact=’666666666@qq.com’ #此处为接收报警的邮箱地址

notify() {

mailsubject=”$(hostname) to be $1, vip转移” #此处$1为脚本参数1

mailbody=”$(date +’%F %T’): vrrp transition, $(hostname) changed to be $1″

echo “$mailbody” | mail -s “$mailsubject” $contact

}

case $1 in

master)

notify master

;;

backup)

notify backup

;;

fault)

notify fault

;;

*)

echo “Usage: $(basename $0) {master|backup|fault}”

exit 1

;;

esac

3.在keepalived配置文件中添加邮箱配置

vim /etc/keepalived/keepalived.conf #在每个主机的每个keepalived组中添加邮件选项

vrrp_instance VI_1 {

state MASTER

interface ens33

virtual_router_id 56

priority 100

advert_int 1

unicast_src_ip 172.20.56.7

unicast_peer {

172.20.56.17

}

virtual_ipaddress {

172.20.56.107/16 dev ens33 label ens33:0

}

notify_master “/etc/keepalived/notify.sh master” #转变为master模式通知

notify_backup “/etc/keepalived/notify.sh backup” #转为backup模式通知

notify_fault “/etc/keepalived/notify.sh fault” #故障通知

}

配置完成后当主机状态发生变化时将收到通知邮件。

暂无评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注