文章目录
环境准备
4台虚拟机
LVS-Master:DIP 10.0.0.41;VIP 10.0.0.100
LVS-Backup: DIP 10.0.0.41
RS1: RIP:10.0.0.43
RS2: RIP:10.0.0.44
永久设置arp级别
vim /etc/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
sysctl -p #查看
通过keepalived来管理lvs
1.可以自动生成VIP地址
2.可以自动创建虚拟服务器和真实服务器
3.可以实现对后端的真实服务器实现健康检查,工作异常的主机,会从调度表中移除,恢复后再自动添加回来
4.可以实现lvs的高可用
安装配置
LVS-Master
[root@ c7-41 ~]# yum -y install keepalived ipvsadm #安装keepalived+ipvsadm
[root@ c7-41 ~]# vim /etc/keepalived/keepalived.conf #编辑配置文件
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
! smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
! vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER #查看是否为MASTER
interface ens33 #修改网卡
virtual_router_id 51
priority 100 #优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.100 #设置VIP
}
}
virtual_server 10.0.0.100 80 { #定义VIP虚拟服务器
delay_loop 6
lb_algo rr #负载均衡算法
lb_kind DR #工作模式
protocol TCP #tcp协议
real_server 10.0.0.43 80 { #定义真实服务器
weight 1 #权重值
TCP_CHECK { #健康检查TCP连接端口
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 10.0.0.44 80 { #定义真实服务器
weight 1 #权重值
TCP_CHECK { #健康检查TCP连接端口
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@ c7-41 ~]# systemctl start keepalived #启动keepalived
[root@ c7-41 ~]# systemctl enable keepalived #设置开机自启
测试
[root@ c7-41 ~]# ip a #查看VIP是否生成
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group defaultqlen 1000
link/ether 00:0c:29:d8:22:01 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.41/24 brd 10.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet 10.0.0.100/32 scope global ens33 #成功生成VIP
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed8:2201/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group defaultqlen 1000
link/ether 00:0c:29:d8:22:0b brd ff:ff:ff:ff:ff:ff
inet 172.16.0.41/16 brd 172.16.255.255 scope global ens37
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed8:220b/64 scope link
valid_lft forever preferred_lft forever
[root@ c7-41 ~]# ipvsadm -Ln #查看lvs调度表
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.100:80 rr
LVS-Backup
[root@ c7-42 ~]# yum -y install keepalived ipvsadm #安装keepalived+ipvsadm
[root@ c7-42 ~]# scp root@10.0.0.41:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf #复制Master的配置文件到Backup
The authenticity of host '10.0.0.41 (10.0.0.41)' can't be established.
ECDSA key fingerprint is SHA256:azg36VNWOuua+J1VKdb0Cy20RuWDGqdwQIjSSA4FZqE.
ECDSA key fingerprint is MD5:24:06:df:b1:5c:3b:6f:79:9a:74:56:0a:32:fb:b6:be.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.41' (ECDSA) to the list of known hosts.
root@10.0.0.41's password:
keepalived.conf 100% 1126 710.2KB/s 00:00
[root@ c7-42 ~]# vim /etc/keepalived/keepalived.conf #修改配置文件
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
! smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
! vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP #改为BACKUP
interface ens33 #查看网卡
virtual_router_id 51
priority 90 #修改优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.100 #设置VIP
}
}
virtual_server 10.0.0.100 80 { #虚拟服务器
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 10.0.0.43 80 { #真实服务器
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 10.0.0.44 80 { #真实服务器
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@ c7-42 ~]# systemctl start keepalived #启动keepalived
[root@ c7-42 ~]# systemctl enable keepalived #设置开机自启
测试
[root@ c7-42 ~]# ip a #查看是否有VIP,没有VIP则正确
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group defaultqlen 1000
link/ether 00:0c:29:69:5e:a2 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.42/24 brd 10.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe69:5ea2/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group defaultqlen 1000
link/ether 00:0c:29:69:5e:ac brd ff:ff:ff:ff:ff:ff
inet 172.16.0.42/16 brd 172.16.255.255 scope global ens37
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe69:5eac/64 scope link
valid_lft forever preferred_lft forever
[root@ c7-42 ~]# ipvsadm -Ln #查看lvs调度表,有调度表为正确
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.100:80 rr
RS1
配置安装
[root@ c7-43 ~]# yum -y install httpd #安装httpd
[root@ c7-43 ~]# systemctl start httpd #启动httpd
[root@ c7-43 ~]# systemctl enable httpd #设置开机自启
[root@ c7-43 ~]# echo 111111 >/var/www/html/index.html #在httpd主页面添加内容,方便一会查看
[root@ c7-43 network-scripts]# netstat -nltp |grep 80 #查看httpd端口是否起来
tcp6 0 0 :::80 :::* LISTEN 3207/httpd
配置VIP
[root@ c7-43 ~]# cd /etc/sysconfig/network-scripts/ #切换到网卡路径
[root@ c7-43 network-scripts]# cp ifcfg-lo ifcfg-lo:0 #复制网卡
[root@ c7-43 network-scripts]# vim ifcfg-lo:0 #修改网卡
DEVICE=lo:0 #刚才复制网卡的名字
IPADDR=10.0.0.100 #VIP
NETMASK=255.255.255.255 #设置掩码
ONBOOT=yes
[root@ c7-43 network-scripts]# ifup lo:0 #启动lo:0
[root@ c7-43 network-scripts]# ifconfig #查看是否有刚才添加的网卡
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.43 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::20c:29ff:fecc:edf7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:cc:ed:f7 txqueuelen 1000 (Ethernet)
RX packets 19705 bytes 26722557 (25.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7649 bytes 531368 (518.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.0.43 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::20c:29ff:fecc:ed01 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:cc:ed:01 txqueuelen 1000 (Ethernet)
RX packets 6 bytes 360 (360.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 42 bytes 3048 (2.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 #刚刚添加的网卡
inet 10.0.0.100 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
ARP级别
[root@ c7-43 network-scripts]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@ c7-43 network-scripts]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@ c7-43 network-scripts]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@ c7-43 network-scripts]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
静态路由
[root@ c7-43 network-scripts]# route add 10.0.0.100 dev lo:0 #因为设置的网络比较简单,所有用静态路由
RS2
配置安装
[root@ c7-44 ~]# yum -y install httpd #安装httpd
[root@ c7-43 ~]# systemctl start httpd #启动httpd
[root@ c7-43 ~]# systemctl enable httpd #设置开机自启
[root@ c7-43 ~]# echo 222222222 >/var/www/html/index.html #为两台服务器添加不同的内容
[root@ c7-44 network-scripts]# netstat -nltp |grep 80 #查看httpd端口是否起来
tcp6 0 0 :::80 :::* LISTEN 3207/httpd
配置VIP
#和RS1服务器一样
[root@ c7-44 ~]# cd /etc/sysconfig/network-scripts/
[root@ c7-44 network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@ c7-44 network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=10.0.0.100
NETMASK=255.255.255.255
ONBOOT=yes
[root@ c7-44 network-scripts]# ifup lo:0
[root@ c7-44 network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.44 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::20c:29ff:fe43:b78a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:43:b7:8a txqueuelen 1000 (Ethernet)
RX packets 19730 bytes 26721480 (25.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7429 bytes 519674 (507.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.0.44 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::20c:29ff:fe43:b794 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:43:b7:94 txqueuelen 1000 (Ethernet)
RX packets 2 bytes 120 (120.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 44 bytes 3168 (3.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 10.0.0.100 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
ARP级别
[root@ c7-44 network-scripts]# echo 1 >/proc/sys/net/ipv4/conf/lo/arp_ignore
[root@ c7-44 network-scripts]# echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore
[root@ c7-44 network-scripts]# echo 2 >/proc/sys/net/ipv4/conf/all/arp_announce
[root@ c7-44 network-scripts]# echo 2 >/proc/sys/net/ipv4/conf/lo/arp_announce
[root@ c7-44 network-scripts]# route add 10.0.0.100 dev lo:0 #静态路由
测试
测试负载均衡效果
curl 10.0.0.100 #访问VIP,若能看到任务调度到不同的服务器上,说明实现了负载均衡
记得刷新查看
lvs高可用测试
停止master主机上keepalived,查看VIP是否漂移到BACKUP节点,成功漂移效果,说明高可用实现成功
[root@ c7-41 ~]# systemctl kill keepalived #干掉keepalived
[root@ c7-41 ~]# ip a #查看MASTER的ip,发现VIP已经不在了
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group defaultqlen 1000
link/ether 00:0c:29:d8:22:01 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.41/24 brd 10.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed8:2201/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group defaultqlen 1000
link/ether 00:0c:29:d8:22:0b brd ff:ff:ff:ff:ff:ff
inet 172.16.0.41/16 brd 172.16.255.255 scope global ens37
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed8:220b/64 scope link
valid_lft forever preferred_lft forever
[root@ c7-42 ~]# ip a #去查看BACKUP的ip,发现已经成功漂移
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group defaultqlen 1000
link/ether 00:0c:29:69:5e:a2 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.42/24 brd 10.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet 10.0.0.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe69:5ea2/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group defaultqlen 1000
link/ether 00:0c:29:69:5e:ac brd ff:ff:ff:ff:ff:ff
inet 172.16.0.42/16 brd 172.16.255.255 scope global ens37
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe69:5eac/64 scope link
valid_lft forever preferred_lft forever
这时候也是可以实现负载的
健康检查
停止其中一台RS,在调度表中可以看到被自动删除,访问虚拟服务器,任务不再给故障的主机调度,说明健康检查功能实现成功
[root@ c7-41 ~]# ipvsadm -Ln #两台RS配置完成之后,我们可以看到调度表中都存在两台RS的ip
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.100:80 rr
-> 10.0.0.43:80 Route 1 0 0
-> 10.0.0.44:80 Route 1 2 0
[root@ c7-43 network-scripts]# systemctl stop httpd #停止httpd
[root@ c7-43 network-scripts]# netstat -nltp |grep 80 #查看端口
[root@ c7-41 ~]# ipvsadm -Ln #停止RS1后,调度表不存在
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.100:80 rr
-> 10.0.0.44:80 Route 1 4 1