nginx负载均衡高可用部署

nginx负载均衡高可用部署

配置显示页面

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值