nginx负载均衡调度器高可用

nginx负载均衡调度器高可用

nginx负载均衡调度器高可用配置

环境说明

主机名IP地址安装应用系统
133192.168.17.133nginx、keepalivedcentos8
147192.168.17.147nginx、keepalivedcentos8
134192.168.17.134httpdcentos8
135192.168.17.135nginxcentos8

配置134

[root@134 ~]# dnf -y install httpd
[root@134 ~]# echo 'apache' > /var/www/html/index.html
[root@134 ~]# systemctl enable --now httpd              
[root@134 ~]# ss -antl                                  
State  Recv-Q Send-Q   Local Address:Port   Peer Address:Port Process 
LISTEN 0      128            0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128               [::]:22             [::]:*            
LISTEN 0      128                  *:80                *:*
[root@134 ~]# curl 192.168.17.134
apache

配置135

[root@135 ~]# dnf -y install nginx
[root@135 ~]# echo 'nginx' > /usr/share/nginx/html/index.html 
[root@135 ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@135 ~]# ss -antl                                               
State  Recv-Q Send-Q   Local Address:Port   Peer Address:Port Process 
LISTEN 0      128            0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128            0.0.0.0:80          0.0.0.0:*            
LISTEN 0      128               [::]:22             [::]:*            
LISTEN 0      128               [::]:80             [::]:*            
[root@135 ~]# curl 192.168.17.135
nginx

配置147

#安装依赖
[root@147 ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim

#创建用户
[root@147 ~]# useradd -rMs /sbin/nologin nginx

#下载软件包并解压编译
[root@147 ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@147 ~]# tar -xf nginx-1.20.2.tar.gz 
[root@147 ~]# cd nginx-1.20.2
[root@147 nginx-1.20.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log

[root@147 ~]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

#配置环境变量
[root@147 ~]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
[root@147 ~]# source /etc/profile.d/nginx.sh

#启动
[root@147 ~]# cat > /usr/lib/systemd/system/nginx.service << EOF
> [Unit]
> Description=nginx server daemon
> After=network.target
> 
> [Service]
> Type=forking
> ExecStart=/usr/local/nginx/sbin/nginx
> ExecStop=/usr/local/nginx/sbin/nginx -s stop
> ExecReload=/bin/kill -HUP \$MAINPID
> 
> [Install]
> WantedBy=multi-user.target
> EOF
[root@147 ~]# systemctl daemon-reload
[root@147 ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@147 ~]# ss -antl
State  Recv-Q Send-Q   Local Address:Port   Peer Address:Port Process 
LISTEN 0      128            0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128            0.0.0.0:80          0.0.0.0:*            
LISTEN 0      128               [::]:22             [::]:* 


#配置负载均衡反向代理
[root@147 ~]# vim /usr/local/nginx/conf/nginx.conf
    upstream webservers {
        server 192.168.17.134;
        server 192.168.17.135;
    }
    ······
    location / {
        proxy_pass http://webservers;
    }
    
#访问测试
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
nginx
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
nginx
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
nginx

配置多次访问apache

[root@147 ~]# vim /usr/local/nginx/conf/nginx.conf
    upstream webservers {
        server 192.168.17.134 weight=3;
        server 192.168.17.135;
    }
[root@147 ~]# !system
systemctl restart nginx


#测试
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
nginx
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
nginx

配置ip_hash,第一个访问到apache,那么就一直是apache

    upstream webservers {
        ip_hash;
        server 192.168.17.134 weight=3;
        server 192.168.17.135;
    }
    
#访问测试
[root@147 ~]# curl 192.168.17.147               
apache
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
apache
[root@147 ~]# curl 192.168.17.147
apache

配置高可用

配置133

#安装nginx
[root@133 ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
[root@133 ~]# useradd -rMs /sbin/nologin nginx   
[root@133 ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@133 ~]# tar -xf nginx-1.20.2.tar.gz
[root@133 ~]# cd nginx-1.20.2
[root@133 nginx-1.20.2]# ./configure \           
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@133 nginx-1.20.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
[root@133 nginx-1.20.2]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
[root@133 nginx-1.20.2]# source /etc/profile.d/nginx.sh
[root@133 nginx-1.20.2]# cat > /usr/lib/systemd/system/nginx.service << EOF     
> [Unit]
> Description=nginx server daemon
> After=network.target
> 
> [Service]
> Type=forking
> ExecStart=/usr/local/nginx/sbin/nginx
> ExecStop=/usr/local/nginx/sbin/nginx -s stop
> ExecReload=/bin/kill -HUP \$MAINPID
> 
> [Install]
> WantedBy=multi-user.target
> EOF
[root@133 nginx-1.20.2]# systemctl daemon-reload
[root@133 nginx-1.20.2]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@133 ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process   
LISTEN   0        128              0.0.0.0:80            0.0.0.0:*               
LISTEN   0        128              0.0.0.0:22            0.0.0.0:*               
LISTEN   0        128                 [::]:22               [::]:*

关闭ip_hash功能

修改133主机配置文件

[root@133 ~]# vim /usr/local/nginx/conf/nginx.conf
    upstream webservers {
        server 192.168.17.134;
        server 192.168.17.135;
    }
    ······
    location / {
        proxy_pass http://webservers;
    }
    

修改147主机配置文件

[root@135 ~]# vim /usr/local/nginx/conf/nginx.conf
    upstream webservers {
        server 192.168.17.134;
        server 192.168.17.135;
    }
    ······
    location / {
        proxy_pass http://webservers;
    }

安装keepalived

配置133

[root@133 ~]# dnf -y install keepalived
[root@133 ~]# cd /etc/keepalived/
[root@133 keepalived]# mv keepalived.conf{,-bak}
[root@133 keepalived]# cat keepalived.conf
global_defs {
    router_id LVS_Server
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass yqz1230
    }
    virtual_ipaddress {  
        192.168.17.250 dev ens33
    }
}
virtual_server 192.168.17.250 80 {
    delay_loop 3
    lvs_sched rr
    lvs_method DR
    protocol TCP
    real_server 192.168.17.133 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.17.147 8080 {
        weight 1
        TCP_CHECK {
            connect_port 8080
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@133 keepalived]# systemctl enable --now keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
[root@133 keepalived]# 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:7f:37:b0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.17.133/24 brd 192.168.17.255 scope global dynamic noprefixroute ens33
       valid_lft 1122sec preferred_lft 1122sec
    inet 192.168.17.250/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe7f:37b0/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

配置147

[root@147 ~]# dnf -y install keepalived 
[root@147 ~]# cd /etc/keepalived/
[root@147 keepalived]# mv keepalived.conf{,-bak}
[root@147 keepalived]# cat keepalived.conf
bal_defs {
    router_id LVS_Server
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass yqz1230
    }
       virtual_ipaddress {  
        192.168.17.250 dev ens33
    }
}
	virtual_server 192.168.17.250 80 {
    delay_loop 3
    lvs_sched rr
    lvs_method DR
    protocol TCP
    real_server 192.168.17.133 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.17.147 8080 {
        weight 1
        TCP_CHECK {
            connect_port 8080
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

[root@147 keepalived]# 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:07:de:9b brd ff:ff:ff:ff:ff:ff
    inet 192.168.17.147/24 brd 192.168.17.255 scope global dynamic noprefixroute ens33
       valid_lft 1502sec preferred_lft 1502sec
    inet6 fe80::20c:29ff:fe07:de9b/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

模拟133主服务器故障

[root@133 ~]# systemctl stop keepalived
[root@133 ~]# systemctl stop nginx
[root@133 ~]# 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:7f:37:b0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.17.133/24 brd 192.168.17.255 scope global dynamic noprefixroute ens33
       valid_lft 1395sec preferred_lft 1395sec
    inet6 fe80::20c:29ff:fe7f:37b0/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

虚拟IP会到147备服务器

[root@147 ~]# 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:07:de:9b brd ff:ff:ff:ff:ff:ff
    inet 192.168.17.147/24 brd 192.168.17.255 scope global dynamic noprefixroute ens33
       valid_lft 1387sec preferred_lft 1387sec
    inet 192.168.17.250/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe07:de9b/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever


[root@136 ~]# curl 192.168.17.250
apache
   link/ether 00:0c:29:07:de:9b brd ff:ff:ff:ff:ff:ff
    inet 192.168.17.147/24 brd 192.168.17.255 scope global dynamic noprefixroute ens33
       valid_lft 1387sec preferred_lft 1387sec
    inet 192.168.17.250/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe07:de9b/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever


[root@136 ~]# curl 192.168.17.250
apache
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值