Keepalived学习笔记,主主架构实例

实例拓扑图:


LVS(Linux Virtual
Server):Linux虚拟服务器,那里经过keepalived作为负载均衡器
帕杰罗S(Real Server):真实服务器
V路虎极光GL450P(Virtual Router Redundancy Protocol): 虚拟路由冗余商业事务,
消除局域网中配置静态网关出现单点失效现象的路由协议
图片 1


LVS(Linux Virtual
Server):Linux虚拟服务器,这里透过keepalived作为负载均衡器
牧马人S(Real Server):真实服务器
V途胜RubiconP(Virtual Router Redundancy Protocol): 虚拟路由冗余共商,
化解局域网中配置静态网关出现单点失效现象的路由协议
图片 1

图片 3

 

 

D哈弗壹和D奥迪Q32铺排keepalived和lvs作主从架构或主主架构,普拉多S一和福睿斯S二陈设nginx搭建web站点。

一 Keepalived是怎么样,有怎么样效益?
1.1 Keepalived的定义
Keepalived 是3个依照VSportage陆风X八P协议来落到实处的LVS高可用方案
1.2 Keepalived的作用
一.二.1 通过IP漂移实现高可用
主副LVS共享叁个虚构IP,同一时半刻间唯有贰个LVS占有VIP并对外提供服务,若该LVS不可用,则VIP漂移至另壹台LVS并对外提供劳动;
1.二.2 对奥德赛S集群举行状态监察和控制
若纳瓦拉S不可用,则keepalived将其从集群中摘除,若凯雷德S苏醒,则keepalived将其重新参预集群中。
二 Keepalived有三种情势,各样情势的相同点和不一样点是怎么?
2.壹 Keepalived的方式连串
Keepalived有3种格局:NAT(地址转换);DOdyssey(直接路由);TUN(隧道)
贰.二 Keepalived的依次格局的牵线
2.2.1 NAT
优点:集群中的兰德CRUISERS能够行使别的支持TCP/IP操作系统,福特ExplorerS可以分配Internet的保存私有地址,唯有LVS必要3个官方的IP地址。
缺点:扩充性有限。当奥迪Q3S节点拉长到二十个或越多时,LVS将成为整个系统的瓶颈,因为具有的伏乞包和答复包都亟需经过LVS再生。
2.2.2 TUN
大家发现,许多Internet服务(例如WEB服务器)的乞求包相当短小,而应答包平常极大。
优点:LVS只承担将呼吁包分发给PRADOS,而RubiconS将回应包直接发放用户。所以,LVS能处理很伟大的请求量,那种形式,壹台载荷均衡能为跨越100台的福特ExplorerS服务,LVS不再是系统的瓶颈。
症结:不过,那种方法供给具备的服务器匡助”IP Tunneling”(IP
Encapsulation)协议,作者仅在Linux系统上贯彻了这一个。
2.2.3 DR
亮点:和TUN壹样,LVS也只是散发请求,应答包通过独立的路由方法再次回到给客户端。与TUN相比较,DHummerH二那种完成格局不要求隧道结构,由此得以使用超过肆伍%操作系统做为RubiconS。
相差:需要LVS的网卡必须与奇骏S的网卡在三个网段上
三 差别情势的布署方式,验证措施分别是怎么?
三.一 基本的条件需求
需要2台LVS和n(n>=2)台RS
3.1.1 LVS
设置ipvsadm(LVS管理工科具)和keepalived;
开启路由转载功效:
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
验证:
sysctl -p
net.ipv4.ip_forward = 1
3.1.2 RS
设置httpd(用于最后测试)
3.二 NAT格局配置
三.二.1 环境概述
操作系统 负载均衡格局 VIP NVIP
RHEL7.4 NAT 193.168.140.80 192.168.102.165

一 Keepalived是哪些,有怎么着效益?
1.1 Keepalived的定义
Keepalived 是2个基于V揽胜QX56P协议来促成的LVS高可用方案
1.2 Keepalived的作用
1.2.1 通过IP漂移达成高可用
主副LVS共享1个虚拟IP,同最近间唯有一个LVS占有VIP并对外提供劳务,若该LVS不可用,则VIP漂移至另一台LVS并对外提供劳动;
一.二.贰 对HighlanderS集群举市价况监察和控制
若LacrosseS不可用,则keepalived将其从集群中摘除,若HavalS苏醒,则keepalived将其重新参加集群中。
二 Keepalived有两种形式,种种情势的相同点和不一样点是怎么?
二.1 Keepalived的形式类别
Keepalived有三种方式:NAT(地址转换);D福睿斯(直接路由);TUN(隧道)
二.二 Keepalived的相继方式的牵线
2.2.1 NAT
优点:集群中的大切诺基S可以动用别的支持TCP/IP操作系统,奥德赛S能够分配Internet的保留私有地址,惟有LVS必要三个官方的IP地址。
缺点:增添性有限。当索罗德S节点拉长到1柒个或愈来愈多时,LVS将成为全数种类的瓶颈,因为具有的央浼包和回复包都亟待经过LVS再生。
2.2.2 TUN
作者们发现,许多Internet服务(例如WEB服务器)的央浼包十分的短小,而应答包常常相当大。
优点:LVS只担负将请求包分发给凯雷德S,而奥迪Q伍S将回应包直接发给用户。所以,LVS能处理很了不起的请求量,那种情势,壹台载荷均衡能为超过100台的TiggoS服务,LVS不再是系统的瓶颈。
症结:不过,这种方式亟待具备的服务器帮助”IP Tunneling”(IP
Encapsulation)协议,作者仅在Linux系统上贯彻了这几个。
2.2.3 DR
亮点:和TUN壹样,LVS也只是散发请求,应答包通过独立的路由方法重返给客户端。与TUN比较,D凯雷德那种完成形式不必要隧道结构,由此得以应用超过二分之壹操作系统做为CRUISERS。
不足:须求LVS的网卡必须与中华VS的网卡在一个网段上
三 分歧情势的安顿格局,验证办法分别是怎么?
三.1 基本的环境要求
需要2台LVS和n(n>=2)台RS
3.1.1 LVS
安装ipvsadm(LVS管理工科具)和keepalived;
开启路由转载功效:
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
验证:
sysctl -p
net.ipv4.ip_forward = 1
3.1.2 RS
安装httpd(用于最后测试)
三.2 NAT情势配置
三.2.壹 环境概述
操作系统 负载均衡格局 VIP NVIP
RHEL7.4 NAT 193.168.140.80 192.168.102.165

留意:各节点的时刻供给联合(ntpdate
ntp1.aliyun.com);关闭firewalld(systemctl stop
firewalld.service,systemctl disable
firewalld.service),设置selinux为permissive(setenforce
0);同时确认保证各网卡援助MULTICAST(多播)通讯。

LVS1 LVS2 RS1 RS2
ens3:192.168.102.161 ens3:192.168.102.162 ens3:192.168.102.163
ens3:192.168.102.164
ens4:193.168.140.79 ens4:193.168.140.83 网关:192.168.102.165
网关:192.168.102.165

LVS1 LVS2 RS1 RS2
ens3:192.168.102.161 ens3:192.168.102.162 ens3:192.168.102.163
ens3:192.168.102.164
ens4:193.168.140.79 ens4:193.168.140.83 网关:192.168.102.165
网关:192.168.102.165

透过命令ifconfig能够查看到是或不是开启了MULTICAST:

3.2.2 LVS
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
qingean@163.com #故障接受联系人
}
notification_email_from admin@test.com #故障发赠与别人
smtp_server 127.0.0.1 #本机发送邮件
smtp_connect_timeout 30
router_id LVS_MASTER #BACKUP上修修改改为LVS_BACKUP
}
vrrp_instance VI_1 {
state MASTER #BACKUP上改动为BACKUP
interface ens4
virtual_router_id 51 #虚拟路由标识,主从相同
priority 100 #BACKUP上修修改改为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111 #主导认证密码必须1致
}
virtual_ipaddress {
193.168.140.80 #虚拟IP(VIP)
}
}
vrrp_instance LAN_GATEWAY { #概念网关
state MASTER #BACKUP上改动为BACKUP
interface ens3
virtual_router_id 62 #虚拟路由ID,主从相同
priority 100 #BACKUP上修修改改为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #ens叁网关虚拟IP
192.168.102.165
}
}
virtual_server 192.168.102.165 80 { #概念内网网关虚拟IP和端口
delay_loop 6 #检查RS时间,单位秒
lb_algo rr
#设置负载调度算法,轮叫(rr)、加权轮叫(wrr)、最小连接(lc)、加权最小连接(wlc)、基于局地性最小连接(lblc)、带复制的依照局地性最少链接(lblcr)、目的地址散列(dh)和源地址散列(sh)
lb_kind NAT #设置LVS负载均衡NAT形式
persistence_timeout 50
#同1IP的连天60秒内被分配到同1台真正服务器(测试时建议改为0)
protocol TCP #使用TCP协议检查LacrosseS状态
real_server 192.168.102.161 80 { #第贰个网关节点
weight 3 #节点权重值
TCP_CHECK { #健检情势
connect_timeout 3 #接二连三超时
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试间隔/S
}
}
real_server 192.168.102.162 80 { #第二个网关节点
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 193.168.140.80 80{ #概念虚拟IP
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.102.163 80 { #第一个RS
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.102.164 80 { #第二个RS
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
3.2.3 RS
为具备昂科雷S添加网关为1玖二.16八.十二.1陆五:
vim /etc/sysconfig/network-scripts/ifcfg-ens3
GATEWAY=192.168.102.165
重启; 使用route –n查看是不是成功
IPVS connection entries
pro expire state source virtual destination
TCP 01:54 FIN_WAIT 10.167.225.60:53882 193.168.140.80:80
192.168.102.163:80
TCP 00:37 NONE 10.167.225.60:0 193.168.140.80:80 192.168.102.163:80

3.2.2 LVS
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
qingean@163.com #故障接受联系人
}
notification_email_from admin@test.com #故障发送给旁人
smtp_server 127.0.0.1 #本机发送邮件
smtp_connect_timeout 30
router_id LVS_MASTER #BACKUP上修修改改为LVS_BACKUP
}
vrrp_instance VI_1 {
state MASTER #BACKUP上改动为BACKUP
interface ens4
virtual_router_id 51 #虚拟路由标识,主从相同
priority 100 #BACKUP上修修改改为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111 #主导认证密码必须1律
}
virtual_ipaddress {
193.168.140.80 #虚拟IP(VIP)
}
}
vrrp_instance LAN_GATEWAY { #概念网关
state MASTER #BACKUP上改动为BACKUP
interface ens3
virtual_router_id 62 #虚拟路由ID,主从相同
priority 100 #BACKUP上改动为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #ens3网关虚拟IP
192.168.102.165
}
}
virtual_server 192.168.102.165 80 { #概念内网网关虚拟IP和端口
delay_loop 6 #检查RS时间,单位秒
lb_algo rr
#设置负载调度算法,轮叫(rr)、加权轮叫(wrr)、最小连接(lc)、加权最小连接(wlc)、基于局地性最小连接(lblc)、带复制的基于局地性最少链接(lblcr)、指标地址散列(dh)和源地址散列(sh)
lb_kind NAT #安装LVS负载均衡NAT格局
persistence_timeout 50
#同一IP的连日60秒内被分配到同1台实在服务器(测试时建议改为0)
protocol TCP #使用TCP协议检查TucsonS状态
real_server 192.168.102.161 80 { #第贰个网关节点
weight 3 #节点权重值
TCP_CHECK { #健检情势
connect_timeout 3 #连年超时
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试间隔/S
}
}
real_server 192.168.102.162 80 { #第一个网关节点
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 193.168.140.80 80{ #概念虚拟IP
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.102.163 80 { #第一个RS
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.102.164 80 { #第二个RS
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
3.2.3 RS
为具备智跑S添加网关为1玖二.16八.十贰.165:
vim /etc/sysconfig/network-scripts/ifcfg-ens3
GATEWAY=192.168.102.165
重启; 使用route –n查看是不是成功
IPVS connection entries
pro expire state source virtual destination
TCP 01:54 FIN_WAIT 10.167.225.60:53882 193.168.140.80:80
192.168.102.163:80
TCP 00:37 NONE 10.167.225.60:0 193.168.140.80:80 192.168.102.163:80

     
 图片 4

叁.三 D奥迪Q5形式配置
三.三.1 环境概述
操作系统 负载均衡格局 VIP
RHEL7.4 DR 193.168.140.80

叁.3 D卡宴情势配置
叁.叁.1 环境概述
操作系统 负载均衡形式 VIP
RHEL7.4 DR 193.168.140.80

keepalived的中坚架构

LVS1 LVS2 RS1 RS2
ens4:193.168.140.79 ens4:193.168.140.83 ens4:193.168.140.152
ens4:193.168.140.224

LVS1 LVS2 RS1 RS2
ens4:193.168.140.79 ens4:193.168.140.83 ens4:193.168.140.152
ens4:193.168.140.224

搭建RS1:

[root@RS1 ~]# yum -y install nginx   #安装nginx
[root@RS1 ~]# vim /usr/share/nginx/html/index.html   #修改主页
    <h1> 192.168.4.118 RS1 server </h1>
[root@RS1 ~]# systemctl start nginx.service   #启动nginx服务
[root@RS1 ~]# vim RS.sh   #配置lvs-dr的脚本文件
    #!/bin/bash
    #
    vip=192.168.4.120
    mask=255.255.255.255
    case $1 in
    start)
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ifconfig lo:0 $vip netmask $mask broadcast $vip up
        route add -host $vip dev lo:0
        ;;
    stop)
        ifconfig lo:0 down
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ;;
    *) 
        echo "Usage $(basename $0) start|stop"
        exit 1
        ;;
    esac
[root@RS1 ~]# bash RS.sh start

3.3.2 LVS
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
qingean@163.com
}
notification_email_from admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_MASTER
}
vrrp_instance VI_1 {
state MASTER #BACKUP上修改为BACKUP
interface ens4
virtual_router_id 51
priority 100 #BACKUP上改动为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
193.168.140.80
}
}
virtual_server 193.168.140.80 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.255
protocol TCP
real_server 193.168.140.152 80 {
weight 10
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 193.168.140.224 80 {
weight 10
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

3.3.2 LVS
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
qingean@163.com
}
notification_email_from admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_MASTER
}
vrrp_instance VI_1 {
state MASTER #BACKUP上修改为BACKUP
interface ens4
virtual_router_id 51
priority 100 #BACKUP上修修改改为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
193.168.140.80
}
}
virtual_server 193.168.140.80 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.255
protocol TCP
real_server 193.168.140.152 80 {
weight 10
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 193.168.140.224 80 {
weight 10
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

参考库罗德S壹的布局搭建XC60S2。

3.3.3 RS
为所有RS修改sysctl.conf
net.ipv4.conf.lo.arp_ignore= 1
net.ipv4.conf.lo.arp_announce= 2
net.ipv4.conf.all.arp_ignore= 1
net.ipv4.conf.all.arp_announce= 2
net.ipv4.ip_forward= 1
执行/sbin/ifconfig lo:0 193.168.140.80 broadcast 193.168.140.80 netmask
255.255.255.255
可用route –n查看是不是中标
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 193.168.1.1 0.0.0.0 UG 100 0 0 ens4
193.168.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens4
193.168.140.80 0.0.0.0 255.255.255.255 UH 0 0 0 lo
若不成事实践/sbin/route add -host 1九三.16八.140.80 dev lo:0
三.肆 验证格局
3.4.一 全部机器关闭防火墙:
systemctl stop firewalld
叁.四.二 全部CRUISERS写入测试页和开启httpd服务
RS1:echo “RS1″ > /var/www/html/index.html
RS2:echo “RS2″ > /var/www/html/index.html
systemctl start httpd
3.4.3 主副LVS开启keepalived服务
systemctl start keepalived
3.4.4 访问
浏览器访问
刷新会轮番体现EnclaveS壹或本田CR-VS二
三.四.5 查看当前测试机的访问请求被转正到哪些服务器
ipvsadm –lcn
IPVS connection entries
pro expire state source virtual destination
TCP 01:54 FIN_WAIT 10.167.225.60:53882 193.168.140.80:80
192.168.102.163:80
TCP 00:37 NONE 10.167.225.60:0 193.168.140.80:80 192.168.102.163:80
3.4.6 测试
模仿宕掉主LVS,服务器照常工作,再宕掉Web一,那时只会显得Web贰,那样就兑现IP负载均衡,高可用集群。当主LVS苏醒后,会切换来主动服务器,若是Keepalived监察和控制模块检查实验web故障恢复生机后,复苏的主机又将此节点参预集群系统中。

3.3.3 RS
为所有RS修改sysctl.conf
net.ipv4.conf.lo.arp_ignore= 1
net.ipv4.conf.lo.arp_announce= 2
net.ipv4.conf.all.arp_ignore= 1
net.ipv4.conf.all.arp_announce= 2
net.ipv4.ip_forward= 1
执行/sbin/ifconfig lo:0 193.168.140.80 broadcast 193.168.140.80 netmask
255.255.255.255
可用route –n查看是不是成功
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 193.168.1.1 0.0.0.0 UG 100 0 0 ens4
193.168.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens4
193.168.140.80 0.0.0.0 255.255.255.255 UH 0 0 0 lo
若不成功实施/sbin/route add -host 19三.16八.140.80 dev lo:0
三.四 验证办法
三.肆.一 全体机器关闭防火墙:
systemctl stop firewalld
叁.四.二 全数昂科拉S写入测试页和开启httpd服务
RS1:echo “RS1″ > /var/www/html/index.html
RS2:echo “RS2″ > /var/www/html/index.html
systemctl start httpd
3.4.3 主副LVS开启keepalived服务
systemctl start keepalived
3.4.4 访问
浏览器访问
刷新会轮番呈现RAV4S1或智跑S2
三.4.伍 查看当前测试机的造访请求被转正到哪些服务器
ipvsadm –lcn
IPVS connection entries
pro expire state source virtual destination
TCP 01:54 FIN_WAIT 10.167.225.60:53882 193.168.140.80:80
192.168.102.163:80
TCP 00:37 NONE 10.167.225.60:0 193.168.140.80:80 192.168.102.163:80
3.4.6 测试
宪章宕掉主LVS,服务器照常工作,再宕掉Web一,这时只会显得Web②,这样就兑现IP负载均衡,高可用集群。当主LVS复苏后,会切换到主动服务器,要是Keepalived监察和控制模块质量评定web故障复苏后,苏醒的主机又将此节点加入集群系统中。

搭建DR1:

[root@DR1 ~]# yum -y install ipvsadm keepalived   #安装ipvsadm和keepalived
[root@DR1 ~]# vim /etc/keepalived/keepalived.conf   #修改keepalived.conf配置文件
    global_defs {
       notification_email {
         root@localhost
       }
       notification_email_from keepalived@localhost
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id 192.168.4.116
       vrrp_skip_check_adv_addr
       vrrp_mcast_group4 224.0.0.10
    }

    vrrp_instance VIP_1 {
        state MASTER
        interface eno16777736
        virtual_router_id 1
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass %&hhjj99
        }
        virtual_ipaddress {
          192.168.4.120/24 dev eno16777736 label eno16777736:0
        }
    }

    virtual_server 192.168.4.120 80 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        protocol TCP

        real_server 192.168.4.118 80 {
            weight 1
            HTTP_GET {
                url {
                  path /index.html
                  status_code 200
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
        real_server 192.168.4.119 80 {
            weight 1
            HTTP_GET {
                url {
                  path /index.html
                  status_code 200
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
         }
    }
[root@DR1 ~]# systemctl start keepalived
[root@DR1 ~]# ifconfig
    eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.4.116  netmask 255.255.255.0  broadcast 192.168.4.255
            inet6 fe80::20c:29ff:fe93:270f  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:93:27:0f  txqueuelen 1000  (Ethernet)
            RX packets 14604  bytes 1376647 (1.3 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 6722  bytes 653961 (638.6 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.4.120  netmask 255.255.255.0  broadcast 0.0.0.0
            ether 00:0c:29:93:27:0f  txqueuelen 1000  (Ethernet)
[root@DR1 ~]# ipvsadm -ln
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  192.168.4.120:80 rr
      -> 192.168.4.118:80             Route   1      0          0         
      -> 192.168.4.119:80             Route   1      0          0

DKuga2的搭建基本同D凯雷德壹,首要修改一下布署文件中/etc/keepalived/keepalived.conf的state和priority:state BACKUP、priority 90. 同时大家发现作为backup的D安德拉2未有启用eno1677773陆:0的网口:

图片 5

客户端举办测试:

[root@client ~]# for i in {1..20};do curl http://192.168.4.120;done   #客户端正常访问
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>

[root@DR1 ~]# systemctl stop keepalived.service   #关闭DR1的keepalived服务

[root@DR2 ~]# systemctl status keepalived.service   #观察DR2,可以看到DR2已经进入MASTER状态
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-09-04 11:33:04 CST; 7min ago
  Process: 12983 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 12985 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─12985 /usr/sbin/keepalived -D
           ├─12988 /usr/sbin/keepalived -D
           └─12989 /usr/sbin/keepalived -D

Sep 04 11:37:41 happiness Keepalived_healthcheckers[12988]: SMTP alert successfully sent.
Sep 04 11:40:22 happiness Keepalived_vrrp[12989]: VRRP_Instance(VIP_1) Transition to MASTER STATE
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: VRRP_Instance(VIP_1) Entering MASTER STATE
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: VRRP_Instance(VIP_1) setting protocol VIPs.
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: Sending gratuitous ARP on eno16777736 for 192.168.4.120
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: VRRP_Instance(VIP_1) Sending/queueing gratuitous ARPs on eno16777736 for 192.168.4.120
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: Sending gratuitous ARP on eno16777736 for 192.168.4.120
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: Sending gratuitous ARP on eno16777736 for 192.168.4.120
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: Sending gratuitous ARP on eno16777736 for 192.168.4.120
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: Sending gratuitous ARP on eno16777736 for 192.168.4.120

[root@client ~]# for i in {1..20};do curl http://192.168.4.120;done   #可以看到客户端正常访问
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>

keepalived的主主框架结构

 修改凯雷德S1和OdysseyS二,添加新的VIP:

[root@RS1 ~]# cp RS.sh RS_bak.sh
[root@RS1 ~]# vim RS_bak.sh   #添加新的VIP
    #!/bin/bash
    #
    vip=192.168.4.121
    mask=255.255.255.255
    case $1 in
    start)
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ifconfig lo:1 $vip netmask $mask broadcast $vip up
        route add -host $vip dev lo:1
        ;;
    stop)
        ifconfig lo:1 down
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ;;
    *)
        echo "Usage $(basename $0) start|stop"
        exit 1
        ;;
    esac
[root@RS1 ~]# bash RS_bak.sh start
[root@RS1 ~]# ifconfig
    ...
    lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 192.168.4.120  netmask 255.255.255.255
            loop  txqueuelen 0  (Local Loopback)

    lo:1: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 192.168.4.121  netmask 255.255.255.255
            loop  txqueuelen 0  (Local Loopback) 
[root@RS1 ~]# scp RS_bak.sh root@192.168.4.119:~
root@192.168.4.119's password: 
RS_bak.sh                100%  693     0.7KB/s   00:00

[root@RS2 ~]# bash RS_bak.sh   #直接运行脚本添加新的VIP 
[root@RS2 ~]# ifconfig
    ...
    lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 192.168.4.120  netmask 255.255.255.255
            loop  txqueuelen 0  (Local Loopback)

    lo:1: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 192.168.4.121  netmask 255.255.255.255
            loop  txqueuelen 0  (Local Loopback)

修改DR1和DR2:

[root@DR1 ~]# vim /etc/keepalived/keepalived.conf   #修改DR1的配置文件,添加新的实例,配置服务器组
    ...
    vrrp_instance VIP_2 {
        state BACKUP
        interface eno16777736
        virtual_router_id 2
        priority 90
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass UU**99^^
        }
        virtual_ipaddress {
            192.168.4.121/24 dev eno16777736 label eno16777736:1
        }
    }

    virtual_server_group ngxsrvs {
        192.168.4.120 80
        192.168.4.121 80
    }
    virtual_server group ngxsrvs {
        ...
    }
[root@DR1 ~]# systemctl restart keepalived.service   #重启服务
[root@DR1 ~]# ifconfig   #此时可以看到eno16777736:1,因为DR2还未配置
    eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.4.116  netmask 255.255.255.0  broadcast 192.168.4.255
            inet6 fe80::20c:29ff:fe93:270f  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:93:27:0f  txqueuelen 1000  (Ethernet)
            RX packets 54318  bytes 5480463 (5.2 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 38301  bytes 3274990 (3.1 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.4.120  netmask 255.255.255.0  broadcast 0.0.0.0
            ether 00:0c:29:93:27:0f  txqueuelen 1000  (Ethernet)

    eno16777736:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.4.121  netmask 255.255.255.0  broadcast 0.0.0.0
            ether 00:0c:29:93:27:0f  txqueuelen 1000  (Ethernet)
[root@DR1 ~]# ipvsadm -ln
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  192.168.4.120:80 rr
      -> 192.168.4.118:80             Route   1      0          0         
      -> 192.168.4.119:80             Route   1      0          0         
    TCP  192.168.4.121:80 rr
      -> 192.168.4.118:80             Route   1      0          0         
      -> 192.168.4.119:80             Route   1      0          0

[root@DR2 ~]# vim /etc/keepalived/keepalived.conf   #修改DR2的配置文件,添加实例,配置服务器组
    ...
    vrrp_instance VIP_2 {
        state MASTER
        interface eno16777736
        virtual_router_id 2
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass UU**99^^
        }
        virtual_ipaddress {
            192.168.4.121/24 dev eno16777736 label eno16777736:1
        }
    }

    virtual_server_group ngxsrvs {
        192.168.4.120 80
        192.168.4.121 80
    }
    virtual_server group ngxsrvs {
        ...
    }
[root@DR2 ~]# systemctl restart keepalived.service   #重启服务
[root@DR2 ~]# ifconfig
    eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.4.117  netmask 255.255.255.0  broadcast 192.168.4.255
            inet6 fe80::20c:29ff:fe3d:a31b  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:3d:a3:1b  txqueuelen 1000  (Ethernet)
            RX packets 67943  bytes 6314537 (6.0 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 23250  bytes 2153847 (2.0 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    eno16777736:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.4.121  netmask 255.255.255.0  broadcast 0.0.0.0
            ether 00:0c:29:3d:a3:1b  txqueuelen 1000  (Ethernet)
[root@DR2 ~]# ipvsadm -ln
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  192.168.4.120:80 rr
      -> 192.168.4.118:80             Route   1      0          0         
      -> 192.168.4.119:80             Route   1      0          0         
    TCP  192.168.4.121:80 rr
      -> 192.168.4.118:80             Route   1      0          0         
      -> 192.168.4.119:80             Route   1      0          0 

客户端测试:

[root@client ~]# for i in {1..20};do curl http://192.168.4.120;done
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
[root@client ~]# for i in {1..20};do curl http://192.168.4.121;done
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>
    <h1> 192.168.4.119 RS2 server</h1>
    <h1> 192.168.4.118 RS1 server </h1>

 

Post Author: admin

发表评论

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