1、基于 CentOS 7 构建 LVS-DR 群集。
139配置如下也计算lvs
yum install -y ipvsadm
然后规划一个vip地址
因为ip地址本身再88网段,所以要规定一个没有使用过的比如192.168.88.200
[root@kelong ~]# ifconfig ens160:200 192.168.88.200/24添加的
然后布置web服务器
138配置如下
[root@haha ~]# yum install httpd -y
echo "web test page , `hostname -I`." > /var/www/html/index.html
[root@haha ~]# systemctl start httpd
140配置如下
yum install httpd -y
echo "web test page , `hostname -I`." > /var/www/html/index.html
systemctl start httpd
做好以上步骤,使用135客户端curl 192.168.88.138进行测试是否输出内容
接下来两个web服务器138,140绑定vip绑定在loopback接口上
138配置
ifconfig lo:200 192.168.88.200 netmask 255.255.255.255 up
route add -host 192.168.88.200 dev lo
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/ens160/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/ens160/arp_announce
140配置
ifconfig lo:200 192.168.88.200 netmask 255.255.255.255 up
route add -host 192.168.88.200 dev lo
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/ens160/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/ens160/arp_announce
回到139配置
[root@kelong ~]# ipvsadm -Ln查看是否配置过lvs,-C就是清除之前lvs配置
[root@kelong ~]# ipvsadm -A -t 192.168.88.200:80 -s rr
[root@kelong ~]# ipvsadm -a -t 192.168.88.200:80 -r 192.168.88.138:80 -g
[root@kelong ~]# ipvsadm -a -t 192.168.88.200:80 -r 192.168.88.140:80 -g
用135做测试
[root@xixi ~]# for ((i=1;i<=6;i++)); do curl 192.168.88.200; done
web test page , 192.168.88.140 172.17.0.1 .
web test page , 192.168.88.138 172.17.0.1 .
web test page , 192.168.88.140 172.17.0.1 .
web test page , 192.168.88.138 172.17.0.1 .
web test page , 192.168.88.140 172.17.0.1 .
web test page , 192.168.88.138 172.17.0.1 .
2、配置nginx负载均衡
同时准备5台虚拟机,三台下载了nginx(135,141,143),两台当作web(138,142)
web(138,142):
echo "web test page,ip is `hostname -I`." > /var/www/html/index.html
143配置nginx反向代理
server{
listen 80;
server_name www1.openlab.cn;
location / {
proxy_pass http://192.168.88.138;
}
}
server {
listen 80;
server_name www2.openlab.cn;
location / {
proxy_pass http://192.168.88.142;
}
}
systemctl status nginx启动
然后再C:\Windows\System32\drivers\etc修改hosts文件
192.168.88.143 www1.openlab.cn www2.openlab.cn
在143修改:
vim /etc/nginx/conf.d/vhost.conf
upstream web_pools {
server 192.168.88.138;
server 192.168.88.142;
}
server{
listen 80;
server_name www1.openlab.cn;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}