准备三台主机A,B,C
虚拟IP地址:192.168.65.100:80(是LVS-DR群集中的虚拟服务器)
DR服务器A:192.168.65.136
web服务器B:192.168.65.137
web服务器C:192.168.65.138
基于 CentOS 7 构建 LVS-DR 群集
一、 在DR服务器A(192.168.65.136)
(1)安装ipvsadm软件包
(2)配置LVS-DR群集中的虚拟IP地址
(3) 配置负载分配策略
(4)配置ipvsadm
-A
:添加一个新的虚拟服务器规则
-s rr
:指定负载均衡调度算法为"rr"(轮询)。
-a
:向现有的虚拟服务器规则中添加一个新的真实服务器。
-g
:将真实服务器标记为网关模式,即将请求的源IP地址保留为客户端的IP地址。
-w 1
:为真实服务器分配权重1(默认权重为1)
二、在web服务器B(192.168.65.137)
(1)安装httpd服务
(2)关闭防火墙selinux,给站点文件添加信息
(3) 配置虚拟IP
(4)设置路由
[root@B network-scripts]# route add -host 192.168.65.100 dev lo:0
[root@B network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.65.2 0.0.0.0 UG 100 0 0 ens33
192.168.65.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.65.100 0.0.0.0 255.255.255.0 UH 0 0 0 lo
[root@B network-scripts]# vim /etc/rc.d/rc.local
/usr/sbin/route add -host 192.168.65.100 dev lo:0
#调整 proc 响应参数
[root@B network-scripts]# vim /etc/sysctl.conf
[root@B network-scripts]# 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
三、在web服务器C(192.168.65.138)
#安装httpd,开启服务
[root@web2 ~]# yum install httpd -y
[root@web2 ~]# systemctl start httpd
[root@web2 ~]# echo "这是web服务器C" > /var/www/html/index.html
#添加虚拟IP
[root@C ~]# cd /etc/sysconfig/network-scripts/
[root@C network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@C network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.65.100
NETMASK=255.255.255.0
NETWORK=127.0.0.0
#设置路由
[root@C network-scripts]# route add -host 192.168.65.100 dev lo:0
[root@C network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.65.2 0.0.0.0 UG 100 0 0 ens33
192.168.65.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.65.100 0.0.0.0 255.255.255.0 UH 0 0 0 lo
#开机执行命令
[root@C network-scripts]# vim /etc/rc.d/rc.local
/usr/sbin/route add -host 192.168.65.100 dev lo:0
#调整 proc 响应参数
[root@C network-scripts]# vim /etc/sysctl.conf
[root@C network-scripts]# 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
测试
配置nginx负载均衡
安装nginx
#配置虚拟机ip,
[root@zyw ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
#安装C语言编译库和依赖包,
[root@zyw ~]# yum install gcc gcc-c++ -y
[root@zyw ~]# yum install pcre-devel openssl-devel -y
#解压,执行安装即可
[root@zyw ~]# tar zxvf nginx-1.22.0.tar.gz
[root@zyw nginx-1.22.0]# ./configure --prefix=/usr/local/nginx
[root@zyw nginx-1.22.0]# make install
#启动nginx及配置nginx开机启动
[root@zyw nginx-1.22.0]# cd /usr/local/nginx/sbin/
[root@zyw sbin]# ./nginx
#编写一个脚本称谓系统服务
[root@zyw sbin]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload #重启nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop #停止
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#重新加载系统服务
[root@localhost sbin]# systemctl daemon-reload
#启动服务
[root@localhost sbin]# systemctl status nginx
配置反向代理
[root@zyw sbin]# vim /etc/nginx/conf.d/vhost.conf
#配置域名为www.zyw.com的虚拟主机
server {
listen 443;
server_name www.zyw.com;
#域名www.zyw.com的请求全部转发到Web服务器192.168.65.137
location / {
proxy_pass http://192.168.65.137;
}
}
#配置域名为www.zyw.com的虚拟主机
server {
listen 443;
server_name www.zyw.com;
#域名www.zyw.com的请求转发到Web服务器192.168.65.138
location / {
proxy_pass http://192.168.65.138;
}
}
在web服务器B,C中编辑index.html文件
配置负载均衡
在windows主机hosts文件添加以下内容
192.168.65.136 www.openlab123.com