基于 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
相关推荐
菜菜子爱学习6 小时前
Nginx学习笔记(二)——环境准备(VMware CentOS版)
笔记·学习·nginx·centos·运维开发
CMCST6 小时前
CentOS 7.9 升级 GLibc 2.34
linux·运维·centos
池以遇11 小时前
云原生高级——nginx
运维·nginx·云原生
懒散猴14 小时前
【无标题】centos 配置阿里云的yum源
linux·阿里云·centos
mohesashou17 小时前
云原生作业(nginx)
运维·nginx
子洋17 小时前
Nginx 启用 NJS 与 QuickJS 支持
前端·后端·nginx
感觉不怎么会1 天前
SIP - Centos 7 搭建freeswitch服务器
linux·服务器·centos
人工智能训练师2 天前
openEuler、 CentOS、Ubuntu等 Linux 系统中,Docker 常用命令总结
linux·ubuntu·centos
菜菜子爱学习2 天前
Nginx学习笔记(三)——在 CentOS 7 中配置阿里云镜像源
笔记·学习·nginx·centos·运维开发·vmware
suwith2 天前
nginx高新能web服务器
linux·服务器·nginx