当前位置: 首页 > news >正文

nginx负载均衡高可用部署

nginx负载均衡高可用部署

文章目录

  • nginx负载均衡高可用部署
    • 配置显示页面
    • 配置负载均衡
    • 配置keepalived

配置显示页面

ip系统服务
192.168.245.129centos8-streamapache
192.168.245.130centos8-streamapache

#关闭所有防火墙

[root@1 nginx]# systemctl disable firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@1 nginx]# setenforce 0
[root@1 nginx]# systemctl stop firewalld.service 

实验开始


[root@3 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:04:e9:0e brd ff:ff:ff:ff:ff:ff
    inet 192.168.245.129/24 brd 192.168.245.255 scope global dynamic noprefixroute ens33
       valid_lft 988sec preferred_lft 988sec
    inet6 fe80::20c:29ff:fe04:e90e/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@3 ~]# yum -y install httpd
[root@3 ~]# echo "apache" > /var/www/html/index.html
[root@3 ~]# cat /var/www/html/index.html
apache
[root@3 ~]# curl 192.168.245.129
apache

[root@4 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:e5:99:45 brd ff:ff:ff:ff:ff:ff
    inet 192.168.245.130/24 brd 192.168.245.255 scope global dynamic noprefixroute ens33
       valid_lft 1730sec preferred_lft 1730sec
    inet6 fe80::20c:29ff:fee5:9945/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@4 ~]# yum -y install apache
[root@4 ~]# echo "apache 负载均衡2 " > /var/www/html/index.html
[root@4 ~]# cat /var/www/html/index.html
apache 负载均衡2 
[root@4 ~]# curl 192.168.245.130
apache 负载均衡2 

到这里,显示页面完成,可以把这两个当成服务看

这两个apache代表着两个需要做Lb+Ha的服务

配置负载均衡

ip系统服务
192.168.245.128centos8-streamkeepalived,nginx
192.168.245.131centos8-streamkeepalived,nginx

这里使用nginx做负载均衡,如果需要使用haproxy做负载均衡也可查看我以前的文章,有写过haproxy+keepalived+nginx

这篇文章的不同点就是:使用的是nginx+keepalived+apache,大同小异

[root@1 ~]# ip a|grep ens33 |grep inet
    inet 192.168.245.128/24 brd 192.168.245.255 scope global dynamic noprefixroute ens33
[root@1 ~]# yum -y install nginx

[root@1 nginx]# pwd
/etc/nginx
[root@1 nginx]# vim nginx.conf

 37     upstream server_1{
 38         #ip_hash;如果要确定同一个访问者发出的请求总是由同一个后端服务器来处理,可以设置ip_hash
 39         server 192.168.245.130 weight=2;
 40         server 192.168.245.129 ;
 41     }
 42     server {
 43         listen       80 default_server;
 44         listen       [::]:80 default_server;
 45         server_name  _;
 46         root         /usr/share/nginx/html;
 47         
 48         # Load configuration files for the default server block.
 49         include /etc/nginx/default.d/*.conf;
 50         
 51         location / {
 52         proxy_pass http://server_1;
 53         }
[root@1 nginx]# systemctl restart nginx.service 


下面使用windows测试

C:\Users\Administrator>curl 192.168.245.128
apache 负载均衡2

C:\Users\Administrator>curl 192.168.245.128
apache 负载均衡2

C:\Users\Administrator>curl 192.168.245.128
apache

C:\Users\Administrator>curl 192.168.245.128
apache 负载均衡2

C:\Users\Administrator>curl 192.168.245.128
apache 负载均衡2

C:\Users\Administrator>curl 192.168.245.128
apache

image-20221017161446635

下面在备用的负载均衡器上配置

[root@2 ~]# ip a |grep ens33 |grep inet
    inet 192.168.245.131/24 brd 192.168.245.255 scope global dynamic noprefixroute ens33

[root@2 ~]# yum -y install nginx

[root@2 ~]# vim /etc/nginx/nginx.conf
 37   upstream server_2{
 38           #ip_hash;如果要确定同一个访问者发出的请求总是由同一个后端服务器来处理,可以设置ip_hash
 39           server 192.168.245.130 weight=2;
 40           server 192.168.245.129 ;
 41           }               
 42     server {
 43         listen       80 default_server;
 44         listen       [::]:80 default_server;
 45         server_name  _;
 46         root         /usr/share/nginx/html;
 47 
 48         # Load configuration files for the default server block.
 49         include /etc/nginx/default.d/*.conf;
 50 
 51         location / {
 52           proxy_pass http://server_2;
 53         }
[root@2 ~]# systemctl restart nginx.service 

image-20221017162012570

配置keepalived

[root@1 ~]# ip a|grep ens33 |grep inet
    inet 192.168.245.128/24 brd 192.168.245.255 scope global dynamic noprefixroute ens33
    
[root@1 nginx]# yum -y install keepalived.x86_64 
[root@1 nginx]# cd /etc/keepalived/
[root@1 keepalived]# ls
keepalived.conf
#做个备份
[root@1 keepalived]# mv keepalived.conf{,2}
[root@1 keepalived]# ls
keepalived.conf2


[root@1 keepalived]# vim keepalived.conf
[root@1 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lb01
}

#这个脚本待会写
vrrp_script nginx_check {
    script "/scripts/check_nginx.sh"
    interval 1
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass yuxuan
    }
    virtual_ipaddress {
        192.168.245.250
    }
}
    track_script{
        nginx_check
}
	#这个脚本也是待会写
    notify_master "/scripts/notify.sh master"

virtual_server 192.168.245.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.245.128 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.245.131 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}



[root@1 keepalived]# mkdir /scripts
[root@1 keepalived]# cd /scripts/
[root@1 scripts]# touch check_nginx.sh
[root@1 scripts]# vim check_nginx.sh
[root@1 scripts]# cat check_nginx.sh
#!/bin/bash
nginx=$(ps -A|grep -Ev "grep|$0" |grep '\bnginx\b'|wc -l)
if [ $nginx -lt 1 ];then
        systemctl stop keepalived
fi
[root@1 scripts]# chmod +x check_nginx.sh

[root@1 scripts]# vim notify.sh
[root@1 scripts]# cat notify.sh
#!/bin/bash
case "$1" in
        master)
                nginx=$(ps -A|grep -Ev "grep|$0" |grep '\bnginx\b'|wc -l)
                if [ $nginx -lt 1 ];then
                        systemctl start nginx.service
                fi
                ;;
        backup)
                 nginx=$(ps -A|grep -Ev "grep|$0" |grep '\bnginx\b'|wc -l)
                 if [ $nginx -gt 0 ];then
                         systemctl stop nginx.service  
                 fi      
                ;;
        *)
                echo "Usage:$0 master|backup VIP"
                ;;
esac

[root@1 scripts]# chmod +x notify.sh

[root@2 scripts]# yum -y install keepalived.x86_64 
[root@2 scripts]# cd /etc/keepalived/
[root@2 keepalived]# ls
keepalived.conf
[root@2 keepalived]# mv keepalived.conf{,2}
[root@2 keepalived]# vim keepalived.conf
[root@2 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lb02
}

#这个脚本待会写
vrrp_script nginx_check {
    script "/scripts/check_nginx.sh"
    interval 1
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass yuxuan
    }
    virtual_ipaddress {
        192.168.245.250
    }
}
    track_script{
        nginx_check
}
        #这个脚本也是待会写
    notify_master "/scripts/notify.sh master"
    notify_backup "/scripts/notify.sh backup"
virtual_server 192.168.245.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.245.128 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.245.131 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}


#两个脚本一模一样
[root@2 scripts]# cat check_nginx.sh
#!/bin/bash
nginx=$(ps -A|grep -Ev "grep|$0" |grep '\bnginx\b'|wc -l)
if [ $nginx -lt 1 ];then
        systemctl stop keepalived
fi
[root@2 scripts]# chmod +x check_nginx.sh

[root@2 scripts]# vim notify.sh
[root@2 scripts]# cat notify.sh
#!/bin/bash
case "$1" in
        master)
                nginx=$(ps -A|grep -Ev "grep|$0" |grep '\bnginx\b'|wc -l)
                if [ $nginx -lt 1 ];then
                        systemctl start nginx.service
                fi
                ;;
        backup)
                 nginx=$(ps -A|grep -Ev "grep|$0" |grep '\bnginx\b'|wc -l)
                 if [ $nginx -gt 0 ];then
                         systemctl stop nginx.service  
                 fi      
                ;;
        *)
                echo "Usage:$0 master|backup VIP"
                ;;
esac
[root@2 scripts]# chmod +x notify.sh

#我们使用虚拟ip进行访问

image-20221017165430995

相关文章:

  • 蚂蚁云科技集团应用研究院院长李亚锋先生受邀为第十三届中国PMO大会演讲嘉宾
  • Linux 安装 GHCup,GHC, cabal 以及通过 cabal 安装 pandoc
  • web安全学习笔记(12)
  • 类与对象笔记-Java-第一部分
  • Git最佳实践指南:从配置到高效开发的全面教程20240418
  • 排列特征重要性(Permutation Feature Importance)
  • T - SQL使用事务 及 在Winform使用事务
  • Eureka服务搭建
  • 探索 Flutter 中的动画:使用 flutter_animate
  • 【服务器数据恢复】ext3文件系统下硬盘坏道掉线的数据恢复案例
  • 设计模式(三)建造者模式
  • GPT润色指令
  • 【附源码】计算机毕业设计SSM怦然心动网上服装商城
  • YOLOv5实现佩戴安全帽检测和识别(含佩戴安全帽数据集+训练代码)
  • 【以太网硬件十六】双绞线有哪些种类?
  • 从零开始配置tensorflow深度学习环境(含cuda以及其他依赖)
  • 模型机的组合逻辑控制器
  • 私域流量和公域流量有何区别?为什么要打造自己的私域流量?
  • Python中的图像调整和裁剪工具
  • 基于单片机的智能照明系统
  • Visual Studio扩展插件
  • 12.MongoDB系列之副本集管理
  • 【初学者入门C语言】之编译预处理(十)
  • 【JavaWeb】之Maven
  • 牛客网专项练习30天Pytnon篇第25天
  • 【RPA前置知识】 整理并总结 Assign 和 AssignT 类
  • SpringBoot整合RabbitMq实现ACK机制--消息回退机制--消息确认机制
  • 关于“八音盒自定义弹奏”的一些想法
  • linux学习(青少年学Linux)
  • 从此刻开始走进HTML的大门!!!
  • TCP通信相关函数
  • Codeforces Global Round 23(A~C)