基于CentOS构建LVS-DR集群
环境准备
主机名 ip地址
node1 192.168.1.140 client
node2 192.168.1.141 LVS
node3 192.168.1.142 RS1
node4 192.168.1.143 RS2
配置
1.关闭防火墙和SELinux
[root@client~]# systemctl stop firewalld
[root@client~]# systemctl disabled firewalld
[root@client~]# sed -i '/^SELINUX=/ c SELINUX=disabled' /etc/selinux/config
2.在node3和node4上安装httpd服务并配置默认访问页面
node3
[root@RS1 ~]# yum install httpd -y
#设置默认页
[root@RS1 ~]# echo "web test page,ip is `hostname -I`." > /var/www/html/index.html
[root@RS1 ~]# systemctl start httpd
node4
[root@RS2 ~]# yum install httpd -y
#设置默认页
[root@RS2 ~]# echo "web test page,ip is `hostname -I`." > /var/www/html/index.html
[root@RS2 ~]# systemctl start httpd
3.配置LVS负载均衡服务并增加两台RS
node2
[root@LVS ~]# ipvsadm -C
[root@LVS ~]# ipvsadm -A -t 192.168.1.200:80 -s rr
[root@LVS ~]# ipvsadm -a -t 192.168.1.200:80 -r 192.168.1.142 -g
[root@LVS ~]# ipvsadm -a -t 192.168.1.200:80 -r 192.168.1.143 -g#查看本地规则
root@LVS ~]# 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.1.200:80 rr-> 192.168.1.142:80 Route 1 0 0 -> 192.168.1.143:80 Route 1 0 0
4.在node3和node4上绑定VIP并添加本机访问VIP的路由
node3
[root@RS1 ~]# ifconfig lo:200 192.168.1.200/32
[root@RS1 ~]# route add -host 192.168.1.200 dev lo
node4
[root@RS2 ~]# ifconfig lo:200 192.168.1.200/32
[root@RS2 ~]# route add -host 192.168.1.200 dev lo
5.抑制ARP响应
node3
[root@RS1 ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@RS1 ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@RS1 ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@RS1 ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
node4
[root@RS2 ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@RS2 ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@RS2 ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@RS2 ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
PS:也可以使用arptables实现抑制arp
arptables -A INPUT -d $VIP -j DROP
arptables -A OUTPUT -s $VIP -j mangle --mangle-ip-s $RIP
# arptables-save > /etc/sysconfig/arptables
# systemctl enable --now arptables
6.测试访问VIP
#访问node3
[root@client ~]# curl 192.168.1.142
web test page,ip is 192.168.1.142 .
#访问node4
[root@client ~]# curl 192.168.1.143
web test page,ip is 192.168.1.143 .#访问VIP
[root@client ~]# for((i=0;i<=5;i++)) do curl 192.168.1.200; done
web test page,ip is 192.168.1.143 .
web test page,ip is 192.168.1.142 .
web test page,ip is 192.168.1.143 .
web test page,ip is 192.168.1.142 .
web test page,ip is 192.168.1.143 .
web test page,ip is 192.168.1.142 .
#查看本地规则
[root@LVS ~]# 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.1.200:80 rr-> 192.168.1.142:80 Route 1 0 3 -> 192.168.1.143:80 Route 1 0 3
至此,基于CentOS构建LVS-DR集群实验完成
配置nginx负载均衡
环境准备
主机名 ip地址 作用
node1 192.168.1.140 负载均衡服务器
node2 192.168.1.141 Web服务器
node3 192.168.1.142 Web服务器
配置
1.在node1上配置主机文件vhost.conf
[root@node1 ~]# vim /etc/nginx/conf.d/vhost.confupstream web_pools {server 192.168.1.141:80;server 192.168.1.142:80;}server {listen 80;server_name www1.ming.com;location / {proxy_pass http://web_pools;}
}
解释:proxy_pass 是反向代理
2.检查语法并重启服务
#检查语法是否正确
[root@node1 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful#重启服务
[root@node1 ~]# systemctl restart nginx
3.配置hosts文件
#在linux中修改
[root@node1 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.140 www1.ming.com
#测试
[root@node1 ~]# for((i=1;i<=4;i++))
> do
> curl www1.ming.com
> done
web test page,ip is 192.168.1.141
web test page ,ip is 192.168.1.142
web test page,ip is 192.168.1.141
web test page ,ip is 192.168.1.142 #在windows中修改
进入C:\Windows\System32\drivers\etc,找到此目录下的hosts文件
将“192.168.1.140 www1.ming.com”添加到最后一行
至此,配置nginx负载均衡实验完成