Nginx负载均衡&高可用配置

Nginx负载均衡&高可用配置

环境说明

各主机均已关闭防火墙与SELinux。

主机名 IP地址 应用服务 系统
LB01 192.168.92.130 keepalived nginx Centos8
LB02 192.168.92.129 keepalived nginx Centos8
RS01 192.168.92.132 nginx Centos8
RS02 192.168.92.133 nginx Centos8

需求

LB01做主负载均衡器,LB02做备负载均衡器,VIP设为192.168.92.200。RS01与RS02做实际处理业务请求的服务器。

部署RS

RS01主机配置

#安装nginx
[root@RS01 ~]# yum -y install nginx

#先将原首页文件备份,再定义新的首页文件内容
[root@RS01 ~]# cd /usr/share/nginx/html/
[root@RS01 html]# ls
404.html  50x.html  index.html  nginx-logo.png  poweredby.png
[root@RS01 html]# mv index.html{,.bak}
[root@RS01 html]# echo 'This is RS01.' > index.html
[root@RS01 html]# ls
404.html  50x.html  index.html  index.html.bak  nginx-logo.png  poweredby.png

#启动nginx并设为开机自启
[root@RS01 html]# systemctl enable --now nginx.service

RS02主机配置

[root@RS02 ~]# dnf -y install nginx
[root@RS02 ~]# cd /usr/share/nginx/html/
[root@RS02 html]# mv index.html{,.bak}
[root@RS02 html]# echo "This is RS02." > index.html
[root@RS02 html]# ls
404.html  50x.html  index.html  index.html.bak  nginx-logo.png  poweredby.png
[root@RS02 html]# systemctl enable --now nginx.service 

测试两台RS能否访问

[root@LB01 ~]# curl 192.168.92.132
This is RS01.
[root@LB01 ~]# curl 192.168.92.133
This is RS02.

部署LB

LB01主机做负载均衡

#安装nginx
[root@LB01 ~]# dnf -y install nginx

#修改配置文件前先对原文件做备份,养成身为运维的良好习惯
[root@LB01 ~]# cd /etc/nginx/
[root@LB01 nginx]# cp nginx.conf nginx.conf.bak
[root@LB01 nginx]# ls
conf.d                fastcgi_params          mime.types          nginx.conf.default   uwsgi_params.default
default.d             fastcgi_params.default  mime.types.default  scgi_params          win-utf
fastcgi.conf          koi-utf                 nginx.conf          scgi_params.default
fastcgi.conf.default  koi-win                 nginx.conf.bak      uwsgi_params

#配置负载均衡
[root@LB01 nginx]# vim nginx.conf
    upstream webserver {		#定义后端实际处理业务请求的服务器池
        server 192.168.92.132;	  #RS01的IP
        server 192.168.92.133;	  #RS02的IP
    }

    server {
        listen       80;
        server_name  _;
        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;

        location / {
            proxy_pass http://webserver;	
        }


[root@LB01 nginx]# systemctl enable --now nginx.service

测试负载均衡

#因没有分配权重,默认是1:1轮询
[root@LB01 nginx]# curl 192.168.92.130
This is RS01.
[root@LB01 nginx]# curl 192.168.92.130
This is RS02.
[root@LB01 nginx]# curl 192.168.92.130
This is RS01.
[root@LB01 nginx]# curl 192.168.92.130
This is RS02.

LB02主机做负载均衡

#安装nginx
[root@LB02 ~]# dnf -y install nginx

[root@LB02 ~]# cd /etc/nginx/
[root@LB02 nginx]# cp nginx.conf 

你可能感兴趣的:(nginx,负载均衡,运维)