基于 CentOS 7 构建 LVS-DR 群集以及配置nginx负载均衡

目录

[一、基于 CentOS 7 构建 LVS-DR 群集](#一、基于 CentOS 7 构建 LVS-DR 群集)

1、前期准备

1、关闭防火墙

2、安装ifconfig

3、准备四台虚拟机

2、在DS上

2.1、配置LVS虚拟IP

2.2、手工执行配置添加LVS服务并增加两台RS

2.3、查看配置

[3、在RS端(第三台、第四台) 上](#3、在RS端(第三台、第四台) 上)

3.1、配置Web服务器

3.2、配置默认主页

3.3、启动服务

3.4、测试:在客户端访问web服务器

3.5、绑定VIP

3.6、配置主机路由

3.7、抑制ARP响应

4、在客户端上测试

二、配置nginx负载均衡

1、安装部署nginx

2、内网服务器151

3、内网服务器152

4、代理服务器150

5、客户端

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

1、前期准备

1、关闭防火墙

复制代码
[root@localhost ~]# systemctl stop firewalld

2、安装ifconfig

复制代码
yum install net-tools.x86_64 -y

3、准备四台虚拟机

|-----------------|-----|
| ip | 用途 |
| 192.168.226.150 | 客户端 |
| 192.168.226.151 | lvs |
| 192.168.226.152 | RS |
| 192.168.226.153 | RS |

2、在DS上

2.1、配置LVS虚拟IP

复制代码
安装ipvsadm
yum install ipvsadm -y

增加IP
ifconfig ens33:200 192.168.226.200 netmask 255.255.255.255 up

2.2、手工执行配置添加LVS服务并增加两台RS

复制代码
[root@localhost ~]# ipvsadm -C
[root@localhost ~]# ipvsadm -A -t 192.168.226.200:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.226.200:80 -r 192.168.226.151:80 -g
[root@localhost ~]# ipvsadm -a -t 192.168.226.200:80 -r 192.168.226.152:80 -g

2**.3、查看配置**

3、在RS端(第三台、第四台) 上

3.1、配置Web服务器

复制代码
yum install httpd -y

3.2、配置默认主页

复制代码
hostname -I 取地址

[root@backup ~]# echo "web test page, ip is `hostname -I`." > /var/www/html/index.html

3.3、启动服务

复制代码
[root@backup ~]# systemctl start httpd

3.4、测试:在客户端访问web服务器

复制代码
[root@localhost ~]# curl 192.168.226.147
web test page, ip is 192.168.226.147 .
[root@localhost ~]# curl 192.168.226.148
web test page, ip is 192.168.226.148 .

3.5、绑定VIP

复制代码
ifconfig lo:200 192.168.226.200 netmask 255.255.255.255 up

3.6、配置主机路由

复制代码
route add -host 192.168.226.200 dev lo

3.7、抑制ARP响应

复制代码
调整内核参数,关闭arp响应

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/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce

4、在客户端上测试

二、配置nginx负载均衡

1、安装部署nginx

在linux系统上部署Nginx_搞笑狗的博客-CSDN博客

2、内网服务器151

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

3、内网服务器152

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

4、代理服务器150

复制代码
[root@localhost ~]# vim /etc/nginx/conf.d/lb_test.conf
upstream www_server_pools {
        server 192.168.226.151:80 weight=1;
        server 192.168.231.152:80 weight=1;
}
server {
        listen 80;
        server_name web1.haha.com;
        location / {
                proxy_pass http://www_server_pools;
                proxy_set_header Host $host;
        }
}

5、客户端

复制代码
[root@localhost ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.226.150 web1.haha.com
[root@localhost ~]# for ((i=1;i<=6;i++)); do curl web1.haha.com; done
192.168.226.152 web1
192.168.226.151 web1
192.168.226.152 web1
192.168.226.151 web1
192.168.226.152 web1
192.168.226.151 web1
相关推荐
ICT系统集成阿祥7 小时前
实验篇| CentOS 7 下 Keepalived + Nginx 实现双机高可用
linux·运维·服务器·nginx·centos
liudachu8 小时前
Centos固定IP配置
linux·tcp/ip·centos
TechStack 创行者8 小时前
Docker 构建 nginx-redis-alpine 项目详解
运维·redis·nginx·docker·容器
星月昭铭13 小时前
uni-app打包h5并部署到nginx,路由模式history
nginx·uni-app
wjf6300015 小时前
CentOS 7 系统上安装 SQLite
linux·运维·centos
软件聚导航15 小时前
在centOS Linux系统搭建自动化构建工具Jenkins
linux·centos·自动化
大得36916 小时前
centos8安装宝塔报错
centos·宝塔centos
问道飞鱼17 小时前
【服务器知识】Nginx路由匹配规则说明
服务器·网络·nginx
noravinsc17 小时前
install of jenkins-2.501-1.1.noarch conflicts with file
centos·jenkins
蜜獾云18 小时前
nginx: [error] invalid PID number ““ in “/usr/local/nginx/logs/nginx.pid“
java·前端·nginx