LVS负载均衡实现双向热备
LVS1服务器配置
一、 准备四台虚拟机
首先将四台虚拟机关闭防火墙
systemctl stop firewalld
iptabled -F
setenforce 0
第一台虚拟机
一、安装ipvsadm,keepalived
[root@localhost ~]# yum -y install ipvsadm
[root@localhost ~]# yum -y install keepalived
二、配置keepalived文件
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# ls
keepalived.conf
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# vim keepalived.conf
1 ! Configuration File for keepalived
2
3 global_defs {
4 notification_email {
5 to@163.com
6 }
7 notification_email_from from@163.com
8 smtp_server 192.168.200.1
9 smtp_connect_timeout 30
10 router_id LVS_MASTER
11 vrrp_skip_check_adv_addr
12 vrrp_strict
13 vrrp_garp_interval 0
14 vrrp_gna_interval 0
15 }
16
17 vrrp_instance VI_1 {
18 state MASTER
19 interface eno16777728
20 virtual_router_id 51
21 priority 100
22 advert_int 1
23 authentication {
24 auth_type PASS
25 auth_pass 1111
26 }
27 virtual_ipaddress {
28 192.168.200.16
29 192.168.200.17
30 192.168.200.18
31 }
32 }
33
34 virtual_server 192.168.200.254 80 {
35 delay_loop 6
36 lb_algo rr
37 lb_kind DR
38 protocol TCP
39
40 real_server 192.168.200.14 80 {
41 weight 1
42 TCP_CHECK {
43 connect_timeout 3
44 nb_get_retry 3
45 delay_before_retry 3
46 connect_port 80
47 }
48 real_server 192.168.200.15 80 {
49 weight 1
50 TCP_CHECK {
51 connect_timeout 3
52 nb_get_retry 3
53 delay_before_retry 3
54 connect_port 80
55 }
56 }
三、 启动keepalived服务
[root@localhost ~]# systemctl start keepalived
四、 查看VIP
ifconfig eno16777728:0 192.168.200.254 netmask 255.255.255.0
ifconfig eno16777728:0
[root@localhost ~]# ip a
五、 查看策略
[root@localhost ~]# ipvsadm -A -t 192.168.200.253:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.200.253:80 -r 192.168.200.14:80 -g -w 1
[root@localhost ~]# ipvsadm -a -t 192.168.200.253:80 -r 192.168.200.15:80 -g -w 1
[root@localhost ~]# 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.200.254:80 rr
-> 192.168.200.14:80 Route 1 0 0
-> 192.168.200.15:80 Route 1 0 0
第二台虚拟机(LVS2备用)
一、安装ipvsadm,keepalived
[root@localhost ~]# yum -y install ipvsadm
[root@localhost ~]# yum -y install keepalived
二、配置keepalived文件
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# ls
keepalived.conf
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# vim keepalived.conf
1 ! Configuration File for keepalived
2
3 global_defs {
4 notification_email {
5 to@163.com
6 }
7 notification_email_from from@163.com
8 smtp_server 192.168.200.1
9 smtp_connect_timeout 30
10 router_id LVS_ BACKUP
11 vrrp_skip_check_adv_addr
12 vrrp_strict
13 vrrp_garp_interval 0
14 vrrp_gna_interval 0
15 }
16
17 vrrp_instance VI_1 {
18 state MASTER
19 interface eno16777728
20 virtual_router_id 51
21 priority 90
22 advert_int 1
23 authentication {
24 auth_type PASS
25 auth_pass 1111
26 }
27 virtual_ipaddress {
28 192.168.200.16
29 192.168.200.17
30 192.168.200.18
31 }
32 }
33
34 virtual_server 192.168.200.253 80 {
35 delay_loop 6
36 lb_algo rr
37 lb_kind DR
38 protocol TCP
39
40 real_server 192.168.200.14 80 {
41 weight 1
42 TCP_CHECK {
43 connect_timeout 3
44 nb_get_retry 3
45 delay_before_retry 3
46 connect_port 80
47 }
48 real_server 192.168.200.15 80 {
49 weight 1
50 TCP_CHECK {
51 connect_timeout 3
52 nb_get_retry 3
53 delay_before_retry 3
54 connect_port 80
55 }
56 }
六、 启动keepalived服务
[root@localhost ~]# systemctl start keepalived
七、 查看VIP
ifconfig eno16777728:0 192.168.200.254 netmask 255.255.255.0
ifconfig eno16777728:0
[root@localhost ~]# ip a
八、 查看策略
[root@localhost ~]# ipvsadm -A -t 192.168.200.253:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.200.253:80 -r 192.168.200.14:80 -g -w 1
[root@localhost ~]# ipvsadm -a -t 192.168.200.253:80 -r 192.168.200.15:80 -g -w 1
[root@localhost ~]# 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.200.253:80 rr
-> 192.168.200.14:80 Route 1 0 0
-> 192.168.200.15:80 Route 1 0 0
第三台虚拟机(Apache服务器的配置)
一、 网络配置
[root@localhost ~]# ifconfig lo:0 192.168.200.254 netmask 255.255.255.255
[root@localhost ~]# ifconfig lo:1 192.168.200.253 netmask 255.255.255.255
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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
inet 192.168.200.254/32 scope global lo:0
valid_lft forever preferred_lft forever
inet 192.168.200.253/32 scope global lo:1
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:76:5e:d7 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.14/24 brd 192.168.200.255 scope global eno16777728
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe76:5ed7/64 scope link
valid_lft forever preferred_lft forever
二、路由配置
[root@localhost ~]# route add -host 192.168.200.254 dev lo:0
[root@localhost ~]# route add -host 192.168.200.253 dev lo:1
三、参数配置
[root@localhost ~]# vim /etc/sysctl.conf
1 net.ipv4.conf.all.arp_ignore = 1
2 net.ipv4.conf.all.arp_announce = 2
3 net.ipv4.conf.default.arp_ignore = 1
4 net.ipv4.conf.default.arp_announce = 2
5 net.ipv4.conf.lo.arp_ignore = 1
6net.ipv4.conf.lo.arp_announce = 2
启用参数
[root@localhost ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
四、安装http服务
准备测试文件
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo "1111" > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd
第四台虚拟机(Apache服务器的配置)
一、 网络配置
[root@localhost ~]# ifconfig lo:0 192.168.200.254 netmask 255.255.255.255
[root@localhost ~]# ifconfig lo:1 192.168.200.253 netmask 255.255.255.255
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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
inet 192.168.200.254/32 scope global lo:0
valid_lft forever preferred_lft forever
inet 192.168.200.253/32 scope global lo:1
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:76:5e:d7 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.14/24 brd 192.168.200.255 scope global eno16777728
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe76:5ed7/64 scope link
valid_lft forever preferred_lft forever
二、路由配置
[root@localhost ~]# route add -host 192.168.200.254 dev lo:0
[root@localhost ~]# route add -host 192.168.200.253 dev lo:1
三、参数配置
[root@localhost ~]# vim /etc/sysctl.conf
1 net.ipv4.conf.all.arp_ignore = 1
2 net.ipv4.conf.all.arp_announce = 2
3 net.ipv4.conf.default.arp_ignore = 1
4 net.ipv4.conf.default.arp_announce = 2
5 net.ipv4.conf.lo.arp_ignore = 1
6net.ipv4.conf.lo.arp_announce = 2
启用参数
[root@localhost ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
四、安装http服务
准备测试文件
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo "2222" > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd
五、测试