目录
一、基于 CentOS 7 构建 LVS-DR 群集
1、准备四台虚拟机
2、配置负载调度器(192.168.2.130)
3、部署共享存储(192.168.2.133)
4、配置两个Web服务器(192.168.2.131、192.168.2.132)
测试集群
二、配置nginx负载均衡。
1、安装部署nginx
2、负载均衡服务器192.168.2.130
3、web1服务器192.168.2.131,web2服务器192.168.2.132
4、配置hosts文件
ip | 角色 |
---|---|
192.168.2.130 vip:192.168.2.200 |
DR服务器 |
192.168.2.131 | Web服务器1 |
192.168.2.132 | Web服务器2 |
192.168.2.133 | NFS服务器 |
systemctl stop firewalld
modprobe ip_vs
cat /proc/net/ip_vs
yum -y install ipvsadm
(1)配置虚拟IP地址(vip:192.168.2.200)
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# vim ifcfg-ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.2.200
NETMASK=255.255.255.255
[root@localhost network-scripts]# ifup ens33:0
[root@localhost network-scripts]# ifconfig ens33:0
(2)调整proc响应参数
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
(3)手工执行配置负载分配策略
[root@localhost ~]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@localhost ~]# systemctl start ipvsadm
[root@localhost ~]# ipvsadm -C
[root@localhost ~]# ipvsadm -A -t 192.168.2.200:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.2.200:80 -r 192.168.2.131:80 -g
[root@localhost ~]# ipvsadm -a -t 192.168.2.200:80 -r 192.168.2.132:80 -g
[root@localhost ~]# ipvsadm
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# yum -y install nfs-utils rpcbind
[root@localhost ~]# mkdir /opt/blue /opt/summer
[root@localhost ~]# chmod 777 /opt/blue /opt/summer
[root@localhost ~]# vim /etc/exports
/usr/share *(ro,sync)
/opt/blue 192.168.2.0/24(rw,sync)
/opt/summer 192.168.2.0/24(rw,sync)
[root@localhost ~]# systemctl start nfs.service
[root@localhost ~]# systemctl start rpcbind.service
两个服务器相同的配置步骤
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vim ifcfg-lo:0
[root@localhost network-scripts]# ifup lo:0
[root@localhost network-scripts]# ifconfig lo:0
配置主机路由
[root@localhost network-scripts]# route add -host 192.168.2.200 dev lo:0
[root@localhost network-scripts]# vim /etc/rc.local
/sbin/route add -host 192.168.2.200 dev lo:0
[root@localhost network-scripts]# chmod +x /etc/rc.d/rc.local
调整内核的 ARP 响应参数以阻止更新 VIP 的 MAC 地址,避免发生冲突
[root@localhost network-scripts]# 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
[root@localhost network-scripts]# yum -y install nfs-utils rpcbind httpd lsof
[root@localhost network-scripts]# systemctl start rpcbind
[root@localhost network-scripts]# systemctl start httpd
[root@localhost network-scripts]# lsof -i:80
[root@localhost network-scripts]# showmount -e 192.168.2.133
--192.168.2.131---
mount.nfs 192.168.2.131:/opt/blue /var/www/html
echo 'this is 131 web' > /var/www/html/index.html
--192.168.2.132---
mount.nfs 192.168.2.132:/opt/summer /var/www/html
echo 'this is 132 web' > /var/www/html/index.html
[root@localhost ~]# vim /etc/nginx/conf.d/nginx.conf
server{
listen 80;
server_name www.first.com;
location /{
proxy_pass http://web_server;
}
}
upstream web_server{
server 192.168.2.131;
server 192.168.2.132;
}
[root@localhost ~]# echo "Welcome to 131 server:192.168.2.131" > /root/index.html
[root@localhost ~]# echo "Welcome to 132 server:192.168.2.132" > /root/index.html
192.168.2.130 www.first.com