在uos1上:
apt-get install keepalived
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf
global_defs {
route_id uos1 #修改路由器名称
}
vrrp_instance VI_1 { #修改,定义一个热备实例
state MASTER #默认,主默认MASTER,从改为BACKUP
interface ens33 #默认,承载VIP地址的物理接口
virtual_router_id 51 #默认,虚拟路由ID号,每个热备组统一
priority 100 #默认,优先级越大越优先,不要相同
advert_int 1 #默认,心跳检测频率(秒)
authentication { #默认,每个热备组需保持一致
auth_type PASS #默认,相互之间采用密码认证
auth_pass 123 #热备组成员间认证密码,需一致
}
virtual_ipaddress { #默认,飘移地址VIP,可指定多个
192.168.200.188 #修改,飘移地址VIP
}
}
systemctl restart keepalived
systemctl enable keepalived
在uos2上:
apt-get install keepalived
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf
global_defs {
route_id uos2 #修改路由器名称
}
vrrp_instance VI_1 { #修改,定义一个热备实例
state BACKUP #默认,主默认MASTER,从改为BACKUP
interface ens33 #默认,承载VIP地址的物理接口
virtual_router_id 51 #默认,虚拟路由ID号,每个热备组统一
priority 90 #默认,优先级越大越优先,不要相同
advert_int 1 #默认,心跳检测频率(秒)
authentication { #默认,每个热备组需保持一致
auth_type PASS #默认,相互之间采用密码认证
auth_pass 123 #热备组成员间认证密码,需一致
}
virtual_ipaddress { #默认,飘移地址VIP,可指定多个
192.168.200.188 #修改,飘移地址VIP
}
}
systemctl restart keepalived #重启keepalived服务
systemctl enable keepalived
ip addr show dev ens33 #VIP目前绑定在server1上
systemctl stop keepalived
ip addr show dev ens33 #VIP目前绑定在server2上
systemctl start keepalived
ip addr show dev ens33 #VIP切换回server1上
实验-uos1和uos2上运行keepalived实现高可用,并运行lvs实现负载均衡(DR模式),uos3和uos4上运行Nginx服务
在uos1上:
apt install -y keepalived
vim /etc/keepalived/keepalived.conf
global_defs {
route_id uos1 #修改路由器名称
}
vrrp_instance VI_1 { #修改,定义一个热备实例
state MASTER #默认,主默认MASTER,从改为BACKUP
interface ens33 #默认,承载VIP地址的物理接口
virtual_router_id 51 #默认,虚拟路由ID号,每个热备组统一
priority 100 #默认,优先级越大越优先,不要相同
advert_int 1 #默认,心跳检测频率(秒)
authentication { #默认,每个热备组需保持一致
auth_type PASS #默认,相互之间采用密码认证
auth_pass 123 #热备组成员间认证密码,需一致
}
virtual_ipaddress { #默认,飘移地址VIP,可指定多个
192.168.200.188 #修改,飘移地址VIP
}
}
virtual_server 192.168.200.188 80 {
delay_loop 15
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 60
protocol TCP
real_server 192.168.200.12 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
delay_before_retry 4
}
}
real_server 192.168.100.13 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
delay_before_retry 4
}
}
}
systemctl enable keepalived
systemctl restart keepalived
在uos2上:
apt install -y keepalived
vim /etc/keepalived/keepalived.conf
global_defs {
route_id uos2 #修改路由器名称
}
vrrp_instance VI_1 { #修改,定义一个热备实例
state BACKUP #默认,主默认MASTER,从改为BACKUP
interface ens33 #默认,承载VIP地址的物理接口
virtual_router_id 51 #默认,虚拟路由ID号,每个热备组统一
priority 90 #默认,优先级越大越优先,不要相同
advert_int 1 #默认,心跳检测频率(秒)
authentication { #默认,每个热备组需保持一致
auth_type PASS #默认,相互之间采用密码认证
auth_pass 123 #热备组成员间认证密码,需一致
}
virtual_ipaddress { #默认,飘移地址VIP,可指定多个
192.168.200.188 #修改,飘移地址VIP
}
}
virtual_server 192.168.200.188 80 {
delay_loop 15
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 60
protocol TCP
real_server 192.168.200.12 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
delay_before_retry 4
}
}
real_server 192.168.200.13 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
delay_before_retry 4
}
}
}
systemctl enable keepalived
systemctl restart keepalived
在uos3和uos4上
vim /etc/profile.d/realserver.sh
/sbin/ifconfig lo:0 192.168.200.188 netmask 255.255.255.255 broadcast 192.168.200.255
/sbin/route add -host 192.168.200.188 dev lo:0
echo “1” >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo “2” >/proc/sys/net/ipv4/conf/lo/arp_announce
echo “1” >/proc/sys/net/ipv4/conf/all/arp_ignore
echo “2” >/proc/sys/net/ipv4/conf/all/arp_announce
source /etc/profile.d/realserver.sh
apt install -y nginx
systemctl enable nginx
systemctl restart nginx
在uos3上执行:
echo uos3 > /var/www/html/index.html
在uos4上执行:
uos4改为echo uos4 > /var/www/html/index.html
健康检查测试
ipvsadm -L -n #查看lvs负载列表
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.200.188:80 rr persistent 60
-> 192.168.200.12:80 Route 1 0 0
-> 192.168.200.13:80 Route 1 0 0
systemctl stop nginx #关闭uos1上的nginx服务
ipvsadm -L -n #查看lvs负载列表,uos1已经被踢出
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.200.188:80 rr persistent 60
-> 192.168.200.13:80 Route 1 0 0
systemctl start nginx #开启uos1上的nginx服务
ipvsadm -L -n #查看lvs负载列表,uos1已经加进来了
实验-uos1和uos2上运行keepalived实现高可用、并运行lvs实现负载均衡(NAT模式),uos3和uos4上运行Nginx服务
uos1和uos2服务器上各添加一块网卡,设置网卡IP如下
服务器类型 网卡1(外网),网卡2(内网)
LVS VIP: 100.100.200.188,192.168.200.188
keepalived Master(uos1): 100.100.200.10,192.168.200.10
keepalived Slave (uos2): 100.100.200.11,192.168.200.11
NginxA(uos3) : / ,192.168.200.12
NginxB(uos4) : / ,192.168.200.13
在uos1上
添加新网卡ens38,连接到VMnet2上
nmcli connection add type ethernet con-name ens38 ifname ens38 connection.autoconnect yes
nmcli connection modify ens38 ipv4.method manual ipv4.addresses 100.100.200.188/24 connection.autoconnect yes
nmcli connection up ens38
vim /etc/keepalived/keepalived.conf
global_defs {
router_id uos1 #修改路由器名称
}
vrrp_instance VI_1 { #修改,定义一个热备实例
state MASTER #默认,主默认MASTER,从改为BACKUP
interface ens33 #默认,承载VIP地址的物理接口
virtual_router_id 51 #默认,虚拟路由ID号,每个热备组统一
priority 100 #默认,优先级越大越优先,不要相同
advert_int 1
lvs_sync_daemon_inteface ens33
authentication {
auth_type PASS #默认,相互之间采用密码认证
auth_pass 123 #热备组成员间认证密码,需一致
}
virtual_ipaddress {
100.100.200.188 #修改,飘移地址VIP
}
}
vrrp_instance LAN_1 { #修改,定义一个热备实例
state MASTER #默认,主默认MASTER,从改为BACKUP
interface ens38 #默认,承载VIP地址的物理接口
virtual_router_id 52 #默认,虚拟路由ID号,每个热备组统一
priority 100 #默认,优先级越大越优先,不要相同
advert_int 1
lvs_sync_daemon_inteface ens38
authentication {
auth_type PASS #默认,相互之间采用密码认证
auth_pass 123 #热备组成员间认证密码,需一致
}
virtual_ipaddress {
192.168.200.188 #修改,飘移地址VIP
}
}
virtual_server 100.100.200.188 80 {
delay_loop 15
lb_algo rr
lb_kind NAT
persistence_timeout 60
protocol TCP
real_server 192.168.200.12 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
delay_before_retry 4
}
}
real_server 192.168.200.13 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
delay_before_retry 4
}
}
}
systemctl restart keepalived #重启keepalived服务
在uos2上
vim /etc/keepalived/keepalived.conf
global_defs {
route_id uos1 #修改路由器名称
}
vrrp_instance VI_1 { #修改,定义一个热备实例
state BACKUP #默认,主默认MASTER,从改为BACKUP
interface ens33
lvs_sync_daemon_inteface ens33
virtual_router_id 51 #默认,虚拟路由ID号,每个热备组统一
priority 90 #默认,优先级越大越优先,不要相同
advert_int 1 #默认,心跳检测频率(秒)
authentication { #默认,每个热备组需保持一致
auth_type PASS #默认,相互之间采用密码认证
auth_pass 123 #热备组成员间认证密码,需一致
}
virtual_ipaddress {
100.100.200.188 #修改,飘移地址VIP
}
}
vrrp_instance LAN_1 { #修改,定义一个热备实例
state BACKUP #默认,主默认MASTER,从改为BACKUP
interface ens38
lvs_sync_daemon_inteface ens38
virtual_router_id 52
priority 90 #默认,优先级越大越优先,不要相同
advert_int 1 #默认,心跳检测频率(秒)
authentication { #默认,每个热备组需保持一致
auth_type PASS #默认,相互之间采用密码认证
auth_pass 123 #热备组成员间认证密码,需一致
}
virtual_ipaddress {
192.168.200.188 #修改,飘移地址VIP
}
}
virtual_server 100.100.200.188 80 {
delay_loop 15
lb_algo rr
lb_kind NAT
persistence_timeout 60
protocol TCP
real_server 192.168.200.12 80 { #后端nginx服务器
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
delay_before_retry 4
}
}
real_server 192.168.200.13 80 { #后端nginx服务器
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
delay_before_retry 4
}
}
}
systemctl restart keepalived #重启keepalived服务
在uos3和uos4上
apt install -y nginx
echo uos3 > /var/www/html/index.html #uos4修改为echo uos4 > /var/www/html/index.html
route add default gw 192.168.200.188 #修改nginx服务器网关
systemctl enable nginx
systemctl restart nginx
宿主机配置NAT网络,IP为192.168.200.20,可以PING通LVS内网VIP:192.168.200.188
健康检查测试
在宿主机上,浏览器访问LVS公网VIP,出现Nginx页面,访问正常
在uos1上
ipvsadm -L -n #查看lvs负载列表
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 100.100.200.188:80 rr persistent 60
-> 192.168.200.12:80 Masq 1 1 1
-> 192.168.200.13:80 Masq 1 0 0
ip addr #查看IP网络情况
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
link/ether 00:0c:29:f9🆎74 brd ff:ff:ff:ff:ff:ff
inet 100.100.200.10/24 brd 10.10.21.255 scope global ens33
valid_lft forever preferred_lft forever
inet 100.100.200.188/32 scope global ens33
valid_lft forever preferred_lft forever
ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:f9🆎7e brd ff:ff:ff:ff:ff:ff
inet 192.168.200.10/24 brd 192.168.10.255 scope global ens38
valid_lft forever preferred_lft forever
inet 192.168.200.188/32 scope global ens38
valid_lft forever preferred_lft forever
systemctl stop keepalived #关闭uos1上的keepalived服务
宿主机打开浏览器访问 LVS公网VIP,访问正常。
在uos2上
ipvsadm -L -n #查看lvs负载列表
irtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 100.100.200.188:80 rr persistent 60
-> 192.168.200.12:80 Masq 1 0 0
-> 192.168.200.13:80 Masq 1 0 0
ip addr #查看IP网络情况
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
link/ether 00:0c:29:9d:7f:47 brd ff:ff:ff:ff:ff:ff
inet 100.100.200.11/24 brd 10.10.21.255 scope global ens33
valid_lft forever preferred_lft forever
inet 100.100.200.188/32 scope global ens33
valid_lft forever preferred_lft forever
ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:9d:7f:51 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.11/24 brd 192.168.10.255 scope global ens38
valid_lft forever preferred_lft forever
inet 192.168.200.188/32 scope global ens38
valid_lft forever preferred_lft forever
systemctl start keepalived #开启uos1上的keepalived服务
LVS 内外网VIP会重新飘回到server1中,并且宿主机打开浏览器访问LVS公网VIP,访问正常。
如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!
添加我为好友,拉您入交流群!
请使用微信扫一扫!