web集群学习--基于CentOS构建LVS-DR集群、配置nginx负载均衡

基于CentOS构建LVS-DR集群

环境准备

主机名   ip地址
node1   192.168.1.140  client
node2	192.168.1.141  LVS
node3	192.168.1.142  RS1
node4	192.168.1.143  RS2

配置

1.关闭防火墙和SELinux

[root@client~]# systemctl stop firewalld
[root@client~]# systemctl disabled firewalld
[root@client~]# sed -i '/^SELINUX=/ c SELINUX=disabled' /etc/selinux/config

2.在node3和node4上安装httpd服务并配置默认访问页面

node3
[root@RS1 ~]# yum install httpd -y
#设置默认页
[root@RS1 ~]# echo "web test page,ip is `hostname -I`." > /var/www/html/index.html
[root@RS1 ~]# systemctl start httpd
node4
[root@RS2 ~]# yum install httpd -y
#设置默认页
[root@RS2 ~]# echo "web test page,ip is `hostname -I`." > /var/www/html/index.html
[root@RS2 ~]# systemctl start httpd

3.配置LVS负载均衡服务并增加两台RS

node2
[root@LVS ~]# ipvsadm -C
[root@LVS ~]# ipvsadm -A -t 192.168.1.200:80 -s rr
[root@LVS ~]# ipvsadm -a -t 192.168.1.200:80 -r 192.168.1.142 -g
[root@LVS ~]# ipvsadm -a -t 192.168.1.200:80 -r 192.168.1.143 -g#查看本地规则
root@LVS ~]# 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.1.200:80 rr-> 192.168.1.142:80             Route   1      0          0         -> 192.168.1.143:80             Route   1      0          0     

4.在node3和node4上绑定VIP并添加本机访问VIP的路由

node3
[root@RS1 ~]# ifconfig lo:200 192.168.1.200/32
[root@RS1 ~]# route add -host 192.168.1.200 dev lo
node4
[root@RS2 ~]# ifconfig lo:200 192.168.1.200/32
[root@RS2 ~]# route add -host 192.168.1.200 dev lo

5.抑制ARP响应

node3
[root@RS1 ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@RS1 ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@RS1 ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@RS1 ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
node4
[root@RS2 ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@RS2 ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@RS2 ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@RS2 ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce

PS:也可以使用arptables实现抑制arp

arptables -A INPUT -d $VIP -j DROP
arptables -A OUTPUT -s $VIP -j mangle --mangle-ip-s $RIP
# arptables-save > /etc/sysconfig/arptables
# systemctl enable --now arptables

6.测试访问VIP

#访问node3
[root@client ~]# curl 192.168.1.142
web test page,ip is 192.168.1.142 .
#访问node4
[root@client ~]# curl 192.168.1.143
web test page,ip is 192.168.1.143 .#访问VIP
[root@client ~]# for((i=0;i<=5;i++)) do curl 192.168.1.200; done
web test page,ip is 192.168.1.143 .
web test page,ip is 192.168.1.142 .
web test page,ip is 192.168.1.143 .
web test page,ip is 192.168.1.142 .
web test page,ip is 192.168.1.143 .
web test page,ip is 192.168.1.142 .
#查看本地规则
[root@LVS ~]# 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.1.200:80 rr-> 192.168.1.142:80             Route   1      0          3         -> 192.168.1.143:80             Route   1      0          3 

至此,基于CentOS构建LVS-DR集群实验完成

配置nginx负载均衡

环境准备

主机名 ip地址        作用
node1 192.168.1.140 负载均衡服务器
node2 192.168.1.141 Web服务器
node3 192.168.1.142 Web服务器

配置

1.在node1上配置主机文件vhost.conf

[root@node1 ~]# vim /etc/nginx/conf.d/vhost.confupstream web_pools {server 192.168.1.141:80;server 192.168.1.142:80;}server {listen 80;server_name www1.ming.com;location / {proxy_pass http://web_pools;}
}
解释:proxy_pass 是反向代理

2.检查语法并重启服务

#检查语法是否正确
[root@node1 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful#重启服务
[root@node1 ~]# systemctl restart nginx

3.配置hosts文件

#在linux中修改
[root@node1 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.140 www1.ming.com
#测试
[root@node1 ~]# for((i=1;i<=4;i++))
> do
> curl www1.ming.com
> done
web test page,ip is 192.168.1.141 
web test page ,ip is 192.168.1.142 
web test page,ip is 192.168.1.141 
web test page ,ip is 192.168.1.142 #在windows中修改
进入C:\Windows\System32\drivers\etc,找到此目录下的hosts文件
将“192.168.1.140 www1.ming.com”添加到最后一行

在这里插入图片描述
在这里插入图片描述
至此,配置nginx负载均衡实验完成

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/58302.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

K最近邻算法:简单高效的分类和回归方法(三)

文章目录 &#x1f340;引言&#x1f340;训练集和测试集&#x1f340;sklearn中封装好的train_test_split&#x1f340;超参数 &#x1f340;引言 本节以KNN算法为主&#xff0c;简单介绍一下训练集和测试集、超参数 &#x1f340;训练集和测试集 训练集和测试集是机器学习和深…

一文了解 Android Auto 车载开发~

作者&#xff1a;牛蛙点点申请出战 背景 我的的产品作为一个海外音乐播放器&#xff0c;在车载场景听歌是一个很普遍的需求。在用户反馈中&#xff0c;也有很多用户提到希望能在车上播放音乐。同时车载音乐也可以作为提升用户消费时长一个抓手。 出海产品&#xff0c;主要服务…

初中信息技术考试编程题,初中信息技术python教案

大家好&#xff0c;小编来为大家解答以下问题&#xff0c;初中信息技术python编程题库 网盘&#xff0c;初中信息技术python编程教学&#xff0c;今天让我们一起来看看吧&#xff01; ID:12450455 资源大小&#xff1a;934KB 资料简介: 2019-2020学年初中信息技术【轻松备课】P…

svg使用技巧

什么是svg SVG 是一种基于 XML 语法的图像格式&#xff0c;全称是可缩放矢量图&#xff08;Scalable Vector Graphics&#xff09;。其他图像格式都是基于像素处理的&#xff0c;SVG 则是属于对图像的形状描述&#xff0c;所以它本质上是文本文件&#xff0c;体积较小&#xf…

瞅一眼nginx

目录 &#x1f9ac;什么是nginx? &#x1f9ac;nginx配置官方yum源&#xff1a; &#x1f9ac;nginx优点 &#x1f9ac;nginx 缺点 &#x1f9ac;查看nginx默认模块 &#x1f40c;nginx新版本的配置文件&#xff1a; &#x1f40c;nginx目录索引 &#x1f40c;nginx状态…

机器学习实战1-kNN最近邻算法

文章目录 机器学习基础机器学习的关键术语 k-近邻算法&#xff08;KNN&#xff09;准备&#xff1a;使用python导入数据实施kNN分类算法示例&#xff1a;使用kNN改进约会网站的配对效果准备数据&#xff1a;从文本文件中解析数据分析数据准备数据&#xff1a;归一化数值测试算法…

JavaWeb(9)——前端综合案例3(悬停显示下拉列表)

一、实例需求 ⌛ 实现类似百度首页的“一个简单的鼠标悬停显示的下拉列表效果”。 二、代码实现 ☕ <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><style>.dropdown-cont…

Flink-串讲面试题

1. 概念 有状态的流式计算框架 可以处理源源不断的实时数据&#xff0c;数据以event为单位&#xff0c;就是一条数据。 2. 开发流程 先获取执行环境env&#xff0c;然后添加source数据源&#xff0c;转换成datastream&#xff0c;然后使用各种算子进行计算&#xff0c;使用…

【数据结构OJ题】轮转数组

原题链接&#xff1a;https://leetcode.cn/problems/rotate-array/ 目录 1. 题目描述 2. 思路分析 3. 代码实现 1. 题目描述 2. 思路分析 1. 方法一&#xff1a;暴力求解&#xff0c;将数组的第一个元素用临时变量tmp存起来&#xff0c;再将数组其他元素往右挪动一步&…

SpringBoot+MyBatis多数据源配置

1.先在配置文件application.yml中配置好数据源 spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedb1:driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: rootjdbc-url: jdbc:mysql://192.168.110.128:3306/CampusHelp?useUnicodeyes&…

基于dockerfile构建sshd、httpd、nginx、tomcat、mysql、lnmp、redis镜像

一、镜像概述 Docker 镜像是Docker容器技术中的核心&#xff0c;也是应用打包构建发布的标准格式。一个完整的镜像可以支撑多个容器的运行&#xff0c;在Docker的整个使用过程中&#xff0c;进入一个已经定型的容器之后&#xff0c;就可以在容器中进行操作&#xff0c;最常见的…

【深度学习】Collage Diffusion,拼接扩散,论文,实战

论文&#xff1a;https://arxiv.org/abs/2303.00262 代码&#xff1a;https://github.com/VSAnimator/collage-diffusion 文章目录 AbstractIntroductionProblem Definition and Goals论文其他内容实战 Abstract 基于文本条件的扩散模型能够生成高质量、多样化的图像。然而&a…