负载均衡、软件平滑升级
创始人
2024-09-25 16:48:45
0

安装nginx 1.26.1

平滑升级、负载均衡

安装依赖 gcc  gcc-c++   pcre-devel   openssl-devel

七层负载均衡配置:

[root@f ~]# vim /usr/local/nginx/conf/nginx.conf
43         location / {  44          #   root   html;  45           #  index  index.html index.htm;  46           proxy_pass    http:192.168.1.17:80 #当访问本机的80端口时,跳转到服务器组  47         } 

总结:

1、一般来说会使用nginx代理动态服务器,例如代理tomcat发布的动态web服务

2、在这个案例中是使用nginx代替的

3、nginx反向代理,是不需要被代理的服务器同意的,只需要在nginx中的location中配置

location  /  {

      proxy_pass   协议   域名|ip  : 端口

}

:wq

./sbin/nginx  -s   reload  #重载配置文件

负载均衡的基础

nginx访客IP黑名单

设置黑名单、白名单

[root@CYX ~]# scp root@192.168.1.17:~/nginx-1.26.1.tar.gz ./

[root@CYX ~]# yum -y install gcc gcc-c++ openssl-devel pcre-devel
 

[root@CYX ~]# tar -zxvf nginx-1.26.1.tar.gz

[root@CYX ~]# cd nginx-1.26.1/
[root@CYX nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream

[root@CYX nginx-1.26.1]# make && make install
 

账号,修改index.html里的内容,并且访问测试,都能访问到

[root@CYX nginx-1.26.1]# useradd -s /bin/nologin -M nginx
[root@CYX nginx-1.26.1]# /usr/local/nginx/sbin/nginx 
[root@CYX nginx-1.26.1]# echo "wxnl" > /usr/local/nginx/html/index.html 
[root@CYX nginx-1.26.1]# curl localhost
wxnl
 

设置仅一个主机可以访问,其他都不可以访问

在配置文件中的server模块中设置

allow允许,deny禁止

可以对IP生效,也可以对网段生效

[root@CYX nginx-1.26.1]# vim /usr/local/nginx/conf/nginx.conf

负载均衡

让每一台主机都能获得相应的压力

负载均衡策略

(1)轮询

upstream backend {

           server 192.168.33.11:8080;

           server 192.168.33.22:8080;

}

(2)weight 加权

upstream backend {

            server 192.168.33.11:8080 weight=5;

            server 192.168.33.22:8080 weight=2;            # 权重默认为 1,谁权重大,谁优先处理请求

}

(3)ip_hash

注意: 使用ip_hash指令无法保证后端服务器的负载均衡,可能导致有些 后端服务器接收到的请求多,有些后端服务器接受的请求少,而且设置 后端服务器权重等方法将不起作用

upstream backend {

         ip_hash;          # ip_hash算法

         server 192.168.33.11:8080;

         server 192.168.33.22:8080;

}

(4)least_conn

最少连接,把请求转发给连接数较少的后端服务器

upstream backend {

                  least_conn;                            # 将请求转发给连接数较少的后端服务器

                  server 192.168.33.11:8080;

                  server 192.168.33.22:8080;

}

(5)url_hash

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,

要配合缓存命中来使用

upstream backend {

              hash $request_uri;

              server 192.168.33.11:8080;

               server 192.168.33.22:8080;

 }

nginx 四层负载均衡

使用stream模块,与七层的http模块同级

[root@f ~]# nginx -v  #查看版本及安装模块

进行备份

[root@f ~]# cp /usr/local/nginx/sbin/nginx  /usr/local/nginx/sbin/nginxbak

安装指定模块并编译

657  cd nginx-1.26.1/ 658  ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream 659  make && make install

模块添加成功

# nginx -V 

修改配置文件

[root@server ~]# vim /usr/local/nginx/conf/nginx.conf

kill不仅仅用于杀死进程,还可以向软件进程发送信号

常用的-9和-15一个是强杀,一个是正常杀

kill 信号 进程编号

# 重新加载nginx配置文件

sbin/nginx -c /usr/local/nginx/conf/nginx.conf

#查看进程

[root@f ~]# ps -aux|grep nginx
root       1583  0.0  0.1  46128  1956 ?        Ss   07:35   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      1684  0.0  0.2  46588  2256 ?        S    07:41   0:00 nginx: worker process
root       6782  0.0  0.0 112824   980 pts/1    R+   19:36   0:00 grep --color=auto nginx

上传新版本,并且解压、编译,安装相应的依赖(新版本必须和旧版本保持一致)

用kill -USR2 启用新版本的Nginx的软件

kill -USR2 老版本的pid编号

主要功能会根据上一次的启动方式再重新运行一次之前的启动命令

[root@f ~]# ls /usr/local/nginx/sbin/ nginx  nginxbak  nginx.old 

重装新的版本以后,会出现新的启动工具

[root@f ~]# /usr/local/nginx/sbin/nginx -v nginx version: nginx/1.26.1 [root@f ~]# /usr/local/nginx/sbin/nginx.old -v nginx version: nginx/1.26.1  

再次查看进程,找到老版本的pid

[root@f ~]# ps -ef|grep nginx root       1583      1  0 07:35 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nginx      1684   1583  0 07:41 ?        00:00:00 nginx: worker process root       6839   6464  0 19:46 pts/1    00:00:00 grep --color=auto nginx

使用老的nginx进程创建新的进程

[root@f ~]# kill -USR2 1583 [root@f ~]# ps -ef|grep nginx root       1583      1  0 07:35 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nginx      1684   1583  0 07:41 ?        00:00:00 nginx: worker process root       6847   6464  0 19:47 pts/1    00:00:00 grep --color=auto nginx 

此时会出现两套master进程,这个时候处理客户请求的就是新的nginx服务 了

关闭老版本的所有子进程

关闭老版本的主进程

[root@f ~]# kill -QUIT 1583 [root@f ~]# ps -ef|grep nginx root       6866   6464  0 19:50 pts/1    00:00:00 grep --color=auto nginx

使用curl查看当前服务器的版本

 curl -I localhost

相关内容

热门资讯

科普教程!炸金花链接房卡怎么搞... 微信游戏中心:新大海在哪里买打开微信,添加客服【8488009】,进入游戏中心或相关小程序,搜索“微...
玩家攻略,金花房卡制作链接大圣... 微信游戏中心:大圣大厅/新西部房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或...
安卓系统换苹果数据怎么,安卓到... 你是不是也和我一样,从安卓系统跳到了苹果的怀抱?换系统后,是不是发现手机里的数据怎么也迁移不过来?别...
秒懂百科”牛牛是如何购买的“新... 是如何购买的是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:113857776许多玩家在游戏中会购买...
科普教程!拼三张链接房卡怎么搞... 微信游戏中心:新蛮王在哪里买打开微信,添加客服【33903369】,进入游戏中心或相关小程序,搜索“...
正规平台有哪些,金花房卡制作链... 毛豆互娱房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 3、根...
一秒了解”新详心房卡客服“人海... 一秒了解”新详心房卡客服“人海大厅房卡充值微信房卡充值 添加房卡批售商:微【113857776】复制...
我来教你/牛牛房卡制作链接嫦娥... 微信游戏中心:嫦娥大厅房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程...
推荐一款!游戏推荐牛牛房卡出售... 您好!微信烛龙大厅/新道游大厅链接获取房卡可以通过以下几种方式购买: 1.微信渠道:(烛龙大厅/新...
实测分享”新神牛房卡“先锋大厅... 来教大家如何使用房卡充值房卡充值 添加房卡批售商:微【113857775】复制到微信搜索、直接添加房...
一分钟了解!斗牛房间怎么创建的... 今 日消息,火神大厅房卡添加微信33549083 苹果今日发布了 iOS 16.1 正式版更新,简单...
1分秒分析”新众亿怎么买房卡“... 来教大家如何使用怎么买房卡房卡充值 添加房卡批售商:微【113857775】复制到微信搜索、直接添加...
玩家须知”新下游是如何购买的“... 玩家须知”新下游是如何购买的“牛牛房卡充值游戏中心打开微信,添加客服【113857776】,进入游戏...
头条推荐!牛牛房卡代理火星大厅... 今 日消息,火星大厅/新道游房卡添加微信33549083 苹果今日发布了 iOS 16.1 正式版更...
头条推荐!金花房卡批发价老神兽... 老神兽/海贝大厅房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 ...
科技实测!游戏推荐牛牛房卡出售... 大众互娱是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332900...
秒懂百科”黄帝大厅房卡购买“卡... 来教大家如何使用房卡充值房卡充值 添加房卡批售商:微【113857775】复制到微信搜索、直接添加房...
头条推荐!游戏推荐牛牛房卡出售... 头条推荐!游戏推荐牛牛房卡出售皇豪互娱/新道游/房卡怎么搞Sa9Ix苹果iPhone 17手机即将进...
正规平台有哪些,金花房卡怎么购... 您好!微信牛魔王/皇豪互娱大厅链接获取房卡可以通过以下几种方式购买: 1.微信渠道:(牛魔王/皇豪...
安卓系统引导加载程序,揭秘启动... 你有没有想过,当你打开手机的那一刻,安卓系统引导加载程序(Bootloader)是如何悄无声息地为你...