CentOS 7 构建 LVS-DR 群集 nginx负载均衡

1、基于 CentOS 7 构建 LVS-DR 群集。

DS(Director Server):DIP 192.168.231.132 & VIP 192.168.231.200

[root@132 ~]# nmcli c show
NAME   UUID                                  TYPE      DEVICE 
ens33  c89f4a1a-d61b-4f24-a260-6232c8be18dc  ethernet  ens33  
[root@132 ~]# nmcli c m ens33 +ipv4.addresses 192.168.231.200/24
[root@132 ~]# nmcli c up ens33
[root@132 ~]# yum install ipvsadm -y
[root@132 ~]# ipvsadm -A -t 192.168.231.200:80 -s rr
[root@132 ~]# ipvsadm -a -t 192.168.231.200:80 -r 192.168.231.136 -g -w 1
[root@132 ~]# ipvsadm -a -t 192.168.231.200:80 -r 192.168.231.137 -g -w 1
[root@132 ~]# 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.231.200:80 rr
  -> 192.168.231.136:80           Route   1      0          0         
  -> 192.168.231.137:80           Route   1      0          0   

rs1

[root@137 ~]# yum install net-tools.x86_64
[root@137 ~]# yum install httpd -y
[root@137 ~]# echo "web test pages ip is `hostname -I`" > /var/www/html/index.html
[root@137 ~]# systemctl restart httpd
[root@137 ~]# ifconfig lo:200 192.168.231.200 netmask 255.255.255.255 up
[root@137 ~]# route add -host 192.168.231.200 dev lo
[root@137 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.231.2   0.0.0.0         UG    100    0        0 ens33
192.168.231.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.231.200 0.0.0.0         255.255.255.255 UH    0      0        0 lo
[root@137 ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@137 ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@137 ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@137 ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce

rs2

[root@136 ~]# yum install net-tools.x86_64
[root@136 ~]# yum install httpd -y
[root@136 ~]# echo "web test pages ip is `hostname -I`" > /var/www/html/index.html
[root@136 ~]# ifconfig lo:200 192.168.231.200 netmsak 255.255.255.255 up
[root@136 ~]# ifconfig lo:200 192.168.231.200 netmask 255.255.255.255 up
[root@136 ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@136 ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@136 ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@136 ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce

测试

2、配置nginx负载均衡。

132内网服务器

yum localinstall  nginx-1.22.0-1.el7.ngx.x86_64.rpm
[root@localhost ~]# vim /etc/nginx/conf.d/vhost.conf
server {
        listen 80;
        server_name web1.yunjisuan.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@localhost ~]# mkdir -p /usr/share/nginx/html/web1/logs
[root@localhost ~]# echo "`hostname -I `web1" > /usr/share/nginx/html/web1/index.html
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx

136内网服务器

yum localinstall  nginx-1.22.0-1.el7.ngx.x86_64.rpm
[root@localhost ~]# vim /etc/nginx/conf.d/vhost.conf
server {
        listen 80;
        server_name web1.yunjisuan.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@localhost ~]# mkdir -p /usr/share/nginx/html/web1/logs
[root@localhost ~]# echo "`hostname -I `web1" > /usr/share/nginx/html/web1/index.html
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx

137代理服务器

yum localinstall  nginx-1.22.0-1.el7.ngx.x86_64.rpm
[root@localhost ~]# vim /etc/nginx/conf.d/lb_test.conf
upstream www_server_pools {
        server 192.168.231.132:80 weight=1;
        server 192.168.231.136:80 weight=1;
}
server {
        listen 80;
        server_name web1.haha.com;
        location / {
                proxy_pass http://www_server_pools;
                proxy_set_header Host $host;
        }
}

客户端

[root@localhost ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.231.137 web1.haha.com
[root@localhost ~]# for ((i=1;i<=4;i++)); do curl web1.haha.com; done
192.168.231.136 web1
192.168.231.132 web1
192.168.231.136 web1
192.168.231.132 web1
相关推荐
叫我龙翔29 分钟前
【计网】实现reactor反应堆模型 --- 多线程方案优化 ,OTOL方案
linux·运维·网络
mit6.82431 分钟前
[Docker#9] 存储卷 | Volume、Bind、Tmpfs | -v/mount | MySQL 灾难恢复 | 问题
linux·运维·docker·容器·架构
Z1eaf_complete34 分钟前
Docker的基础使用
运维·docker·容器·云计算
WangYaolove13141 小时前
请解释Python中的装饰器是什么?如何使用它们?
linux·数据库·python
群联云防护小杜1 小时前
服务器被挂马怎么办?——解决服务器被挂马的方法和步骤
运维·服务器·网络协议·tcp/ip·安全·ddos
明志致远淡泊宁静1 小时前
记录一次服务器redis被入侵
运维·服务器·redis
Jtti1 小时前
Jtti:服务器总是自动重启怎么办?
运维·服务器
我是黄大仙1 小时前
利用飞书多维表格自动发布版本
运维·服务器·数据库·飞书
7yewh1 小时前
嵌入式硬件实战提升篇(一)-泰山派RK3566制作多功能小手机
linux·arm开发·驱动开发·嵌入式硬件·物联网·智能手机·硬件架构
YRr YRr2 小时前
ubuntu ros 解决建完图后 保存的地图非常小的问题
linux·运维·ubuntu