基于LVS的web集群部署(http)
LVS部署
3台主机,2RS,1balancer(DR)
2RS httpd服务部署
DR调度器部署:安装ipvsadm命令,2网卡(nat,hostonly)
部署NAT模式http负载集群
准备工作
- 准备3台虚拟机
ip | 作用 |
---|---|
192.168.94.141(内dip) 192.168.35.129(外vip) |
DR(调度机) |
192.168.94.143 | RS1(真实服务器1) |
192.168.92.129 | RS2(真实服务器2) |
-
关闭防火墙selinux
systemctl stop firewalld && setenforce 0
-
RS安装httpd启动服务,修改index网页内容方便验证
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# echo RS1> /var/www/html/index.html
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# systemctl enable --now httpd
[root@RS2 ~]# echo RS2> /var/www/html/index.html
- RS端添加路由指向DR的dip
[root@RS1 ~]# route add default gw 192.168.94.141
[root@RS2 ~]# route add default gw 192.168.94.141
- DR端安装ipvsadm命令
[root@DR ~]# yum -y install ipvsadm
[root@DR ~]# systemctl enable ipvsadm.service
- DR端添加2块网卡(nat,hostonly)
[root@DR ~]# ip a|grep ens.*
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
inet 192.168.94.141/24 brd 192.168.94.255 scope global dynamic noprefixroute ens160
3: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
inet 192.168.35.129/24 brd 192.168.35.255 scope global dynamic noprefixroute ens224
- DR启动ip转发功能
[root@DR ~]# vim /etc/sysctl.conf
...
net.ipv4.ip_forward = 1 //添加该行
[root@DR ~]# sysctl -p
net.ipv4.ip_forward = 1
-
DR添加规则
//添加虚拟服务 [root@DR ~]# ipvsadm -A -t 192.168.35.129:80 -s rr //添加RS -m(NAT模式) [root@DR ~]# ipvsadm -a -t 192.168.35.129:80 -r 192.168.94.143 -m [root@DR ~]# ipvsadm -a -t 192.168.35.129:80 -r 192.168.94.129 -m //保存规则 [root@DR ~]# ipvsadm -S > /etc/sysconfig/ipvsadm [root@DR ~]# cat /etc/sysconfig/ipvsadm -A -t DR:http -s rr -a -t DR:http -r node3:http -m -w 1 -a -t DR:http -r node2:http -m -w 1 //查看规则 [root@DR ~]# 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.35.129:80 rr -> 192.168.94.129:80 Masq 1 0 0 -> 192.168.94.143:80 Masq 1 0 0
-
测试验证
部署DR模式的http负载集群
准备工作
- 准备3台虚拟机
ip | 作用 |
---|---|
192.168.94.141(内dip);192.168.94.120(vip) | DR(调度机) |
192.168.94.143;192.168.94.120(vip) | RS1(真实服务器1) |
192.168.92.129;192.168.94.120(vip) | RS2(真实服务器2) |
-
关闭防火墙selinux
systemctl stop firewalld && setenforce 0
-
RS安装httpd启动服务,修改index网页内容方便验证
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# echo RS1> /var/www/html/index.html
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# systemctl enable --now httpd
[root@RS2 ~]# echo RS2> /var/www/html/index.html
- DR端添加新ip
[root@DR ~]# ip addr add 192.168.94.120/32 dev ens160
[root@DR ~]# ip a|grep 'inet 192'
inet 192.168.94.141/24 brd 192.168.94.255 scope global dynamic noprefixroute ens160
inet 192.168.94.120/32 scope global ens160
- RS端修改内核参数
[root@RS1 ~]# vim /etc/sysctl.conf
...
net.ipv4.conf.all.arp_ignore = 1 #只响应目的IP地址为接收网卡上的本地地址的arp请求
net.ipv4.conf.all.arp_announce = 2 #只向该网卡回应与该网段匹配的ARP报文
[root@RS1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@RS2 ~]# vim /etc/sysctl.conf
...
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@RS2 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
- 设置RS vip
[root@RS1 ~]# ip addr add 192.168.94.120/32 dev lo
[root@RS1 ~]# ip a
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
inet 192.168.94.120/32 scope global lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
[root@RS2 ~]# ip addr add 192.168.94.120/32 dev lo
[root@RS2 ~]# ip a
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
inet 192.168.94.120/32 scope global lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
- 添加路由指向vip
[root@RS1 ~]# route add -host 192.168.94.120/32 dev lo
[root@RS2 ~]# route add -host 192.168.94.120/32 dev lo
- 添加规则
[root@DR ~]# ipvsadm -A -t 192.168.94.120:80 -s wrr
[root@DR ~]# ipvsadm -a -t 192.168.94.120:80 -r 192.168.94.129:80 -g
[root@DR ~]# ipvsadm -a -t 192.168.94.120:80 -r 192.168.94.143:80 -g
[root@DR ~]# 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.94.120:80 wrr
-> 192.168.94.129:80 Route 1 0 0
-> 192.168.94.143:80 Route 1 0 0
- 测试验证