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

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

DS(Director Server):DIP 192.168.231.132 & VIP 192.168.231.200

bash 复制代码
[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

bash 复制代码
[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

bash 复制代码
[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内网服务器

bash 复制代码
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内网服务器

bash 复制代码
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代理服务器

bash 复制代码
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;
        }
}

客户端

bash 复制代码
[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
相关推荐
旺仔学IT10 小时前
Centos7中使用yum命令时候报错 “Could not resolve host: mirrorlist.centos.org; 未知的错误“
linux·运维·centos
denglei.12 小时前
在Nginx部署Web应用,如何保障后端API的安全
nginx·安全
广而不精zhu小白14 小时前
CentOS Stream 9 挂载Windows共享FTP文件夹
linux·windows·centos
赵大仁15 小时前
在 CentOS 7 上安装 Node.js 20 并升级 GCC、make 和 glibc
linux·运维·服务器·ide·ubuntu·centos·计算机基础
苹果醋315 小时前
React系列(八)——React进阶知识点拓展
运维·vue.js·spring boot·nginx·课程设计
我要出家当道士15 小时前
Nginx单向链表 ngx_list_t
数据结构·nginx·链表·c
fat house cat_17 小时前
Linux环境下使用tomcat+nginx部署若依项目
linux·nginx·tomcat
march of Time19 小时前
centos系统如何安装kubectl和部署kube-apiserver
linux·运维·centos
꧁༺朝花夕逝༻꧂19 小时前
nginx-代理服务
运维·服务器·nginx
Smile_Gently19 小时前
Ubuntu环境 nginx.conf详解(二)
运维·服务器·前端·nginx·ubuntu