1、基于CentOS 7 构建LVS-DR群集
实验环境准备
客户端 | 192.168.86.140 |
LVS | 192.168.86.137 |
RS | 192.168.86.138 |
RS | 192.168.86.139 |
虚拟vip | 192.168.86.150 |
(1)配置LVS虚拟IP
[root@node1 ~]# yum install ipvsadm -y
[root@node1 ~]# ifconfig ens33:200 192.168.86.150 netmask 255.255.255.0 up
(2)添加LVS服务,增加两台RS
[root@node1 ~]# ipvsadm -C
[root@node1 ~]# ipvsadm -A -t 192.168.86.150:80 -s rr
[root@node1 ~]# ipvsadm -a -t 192.168.86.150:80 -r 192.168.86.138:80 -g -w 1
[root@node1 ~]# ipvsadm -a -t 192.168.86.150:80 -r 192.168.86.139:80 -g -w 1
[root@node1 ~]# 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.86.150:80 rr
-> 192.168.86.138:80 Route 1 0 0
-> 192.168.86.139:80 Route 1 0 0
(3)RS端绑定VIP
[root@node2 ~]# ifconfig lo:100 192.168.86.150 netmask 255.255.255.255 up
[root@node2 ~]# route add -host 192.168.86.200 dev lo
(4)RS端抑制ARP
[root@node2 ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@node2 ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
[root@node2 ~]# echo "1" > /proc/sys/net/ipv4/conf/ens33/arp_ignore
[root@node2 ~]# echo "2" > /proc/sys/net/ipv4/conf/ens33/arp_announce
(5)RS端书写html页面
[root@node2 ~]# yum install httpd -y
[root@node2 ~]# mkdir -p /var/www/html
[root@node2 ~]# echo "web test page , ip is `hostname -I`." > /var/www/html/index.html
(6)客户端测试
2、配置Nginx负载均衡
(1)内网服务器140
[root@server ~]# yum localinstall nginx-1.22.0-1.el7.ngx.x86_64.rpm
[root@server ~]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name web1.luyilin.com;
location / {
root /usr/share/nginx/html/web1;
index index.html index.htm;
}
access_log /usr/share/nginx/html/web1/logs/access_bbs.log main;
}
[root@server ~]# mkdir -p /usr/share/nginx/html/web1/logs
[root@server ~]# echo "`hostname -I` web1" > /usr/share/nginx/html/web1/index.html
[root@server ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@server ~]# systemctl restart nginx
(2)内网服务器139
[root@server ~]# yum localinstall nginx-1.22.0-1.el7.ngx.x86_64.rpm
[root@server ~]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name web1.luyilin.com;
location / {
root /usr/share/nginx/html/web1;
index index.html index.htm;
}
access_log /usr/share/nginx/html/web1/logs/access_bbs.log main;
}
[root@server ~]# mkdir -p /usr/share/nginx/html/web1/logs
[root@server ~]# echo "`hostname -I` web1" > /usr/share/nginx/html/web1/index.html
[root@server ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@server ~]# systemctl restart nginx
(3)代理服务器
[root@server ~]yum localinstall nginx-1.22.0-1.el7.ngx.x86_64.rpm
[root@server ~]# vim /etc/nginx/conf.d/lb_test.conf
upstream www_server_pools {
server 192.168.86.139:80 weight=1;
server 192.168.86.140:80 weight=1;
}
server {
listen 80;
server_name web1.haha.com;
location / {
proxy_pass http://www_server_pools;
proxy_set_header Host $host;
}
}
(4)客户端
[root@server ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.86.137 web1.haha.com
[root@server ~]# for ((i=1;i<=4;i++)); do curl web1.haha.com; done
192.168.86.139 web1
192.168.86.140 web1
192.168.86.139 web1
192.168.86.140 web1