目录
1、基于 CentOS 7 构建 LVS-DR 群集。
2、配置nginx负载均衡。
1、基于 CentOS 7 构建 LVS-DR 群集。
1.部署LVS负载调度器
1>安装配置工具
[root@node6 ~]# yum install -y ipvsadm
2>配置LVS虚拟IP(VIP地址)
[root@node6 ~]# ifconfig ens33:200 192.168.111.200 netmask 255.255.255.255 up
3>手工执行配置添加LVS服务并增加两台RS
[root@node6 ~]# ipvsadm -A -t 192.168.111.200:80 -s rr
[root@node6 ~]# ipvsadm -a -t 192.168.111.200:80 -r 192.168.111.7:80 -g
[root@node6 ~]# ipvsadm -a -t 192.168.111.200:80 -r 192.168.111.8:80 -g
[root@node6 ~]# 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.111.200:80 rr-> 192.168.111.7:80 Route 1 0 0 -> 192.168.111.8:80 Route 1 0 0 2.部署web服务器(两台机器同时部署)
1>安装web服务器
[root@node7 ~]# yum install -y httpd
2>写网页文件
[root@node7 ~]# echo "web is `hostname -I`" > /var/www/html/index.html
[root@node7 ~]# systemctl start httpd
3>在客户端就行测试
[root@yx ~]# curl 192.168.111.7
web is 192.168.111.7
[root@yx ~]# curl 192.168.111.8
web is 192.168.111.8
4>手工在RS端绑定VIP,并且添加本机访问VIP的路由
[root@node7 ~]# ifconfig lo:200 192.168.111.200 netmask 255.255.255.255 up
[root@node7 ~]# route add -host 192.168.111.200 dev lo:200
[root@node7 ~]# route -n # 查看路由
5>手工在RS端抑制ARP响应(调整内核参数,关闭arp响应)
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/ens33/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/ens33/arp_announce另一种方法:使用arptables实现抑制arp
[root@node8 ~]# yum install -y arptables
[root@node8 ~]# arptables -A INPUT -d 192.168.111.200 -j DROP
[root@node8 ~]# arptables -A OUTPUT -s 192.168.111.200 -j mangle --mangle-ip-s 192.168.111.8
[root@node8 ~]# arptables -L -n --line-number # 查看
# --line-number 显示行号
[root@node8 ~]# arptables-save > /etc/sysconfig/arptables
[root@node8 ~]# systemctl enable --now arptables.service3.测试(在客户端进行测试)
[root@yx ~]# for ((i=1;i<=6;i++))
> do
> curl 192.168.111.200
> done
web is 192.168.111.8
web is 192.168.111.7
web is 192.168.111.8
web is 192.168.111.7
web is 192.168.111.8
web is 192.168.111.7
2、配置nginx负载均衡。
1.准备两台后端web服务器(同时进行)
# 把nginx的rpm包上传至服务器中,然后进行安装
[root@node7 ~]# yum install -y nginx
[root@node7 ~]# echo "web is `hostname -I`" > /usr/share/nginx/html/index.html
[root@node7 ~]# systemctl start nginx2.负载均衡配置
[root@node6 ~]# yum install -y nginx-1.22.0-1.el7.ngx.x86_64.rpm
[root@node6 ~]# vim /etc/nginx/conf.d/vhost.conf
server {listen 80;server_name www.open.cn;location / {proxy_pass http://web_server;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
}
upstream web_server {server 192.168.111.7;server 192.168.111.8;
}
[root@node6 ~]# nginx -t
[root@node6 ~]# systemctl start nginx3.测试
[root@yx ~]# vim /etc/hosts
192.168.111.6 www.open.cn
[root@yx ~]# for ((i=1;i<=6;i++)); do curl www.open.cn; done
web is 192.168.111.7
web is 192.168.111.8
web is 192.168.111.7
web is 192.168.111.7
web is 192.168.111.8
web is 192.168.111.7
或者在PC端修改主机域名(C:\Windows\System32\drivers\etc/hosts)
192.168.111.6 www.open.cn
# 在浏览器上进行域名访问,访问的内容刷新之后会进行不断的变化,如下