nginx 負荷分散スケジューラは高可用性サービスを構成します

nginx 負荷分散スケジューラは高可用性サービスを構成します

1. リバースプロキシとロードバランシング

nginx通常はバックエンドサーバーのリバースプロキシとして使用されるため、動的・静的な分離や負荷分散が容易に実現でき、サーバーの処理能力が大幅に向上します。

nginxnginx実際、動的と静的の分離を実現するために、リバース プロキシを使用する場合、静的リソースの場合は、バックグラウンド サーバーから取得するのではなく、公開されたパスから直接読み取ることができます。

Rsyncただし、この場合、バックエンド プログラムとフロントエンド プログラムが一貫していて、サーバー側の自動同期やNFS分散MFS共有ストレージに使用できることを保証する必要があることに注意してください。

Http Proxy`模块,功能很多,最常用的是`proxy_pass`和`proxy_cache

これを使用したい場合はproxy_cache、サードパーティのngx_cache_purgeモジュールを統合して、指定された URL キャッシュをクリアする必要があります。この統合は、nginxインストール中に次のように行う必要があります。
./configure --add-module=../ngx_cache_purge-1.0 ......

nginxモジュールを通じてupstream単純な負荷分散を実現するには、セグメントupstreamで定義する必要があります。http

このセクションではupstream、サーバーのリストを定義します。デフォルトの方法はポーリングです。同じ訪問者によって送信されたリクエストが常に同じバックエンド サーバーによって処理されるようにしたい場合は、次のように ip_hash を設定できます。

upstream idfsoft.com {
  ip_hash;
  server 127.0.0.1:9080 weight=5;
  server 127.0.0.1:8080 weight=5;
  server 127.0.0.1:1111;
}

注: このメソッドの本質は依然としてポーリングであり、クライアントの IP は動的 IP、プロキシ、壁を越えたものなど常に変化する可能性があるため、ip_hash は同じクライアントが常に同じサーバーによって処理されることを完全には保証できません。

を定義した後、セグメントに次のコンテンツを追加するupstream必要があります。server

server {
  location / {
    proxy_pass http://idfsoft.com;
  }
}

2、nginx ロード バランシング スケジューラの高可用性構成

環境説明

CPU名 IPアドレス アプリをインストールする システム
135 192.168.183.135 nginx、キープアライブ 試してみた8
136 192.168.183.136 nginx、キープアライブ 試してみた8
137 192.168.183.137 httpd 試してみた8
138 192.168.183.138 nginx 試してみた8

: 設定前にファイアウォールと selinux をオフにすることを忘れないでください。selinux モードは無効モードです。

137 台のホストを構成する

[root@137 ~]# dnf -y install httpd
[root@137 ~]# echo 'apache!' > /var/www/html/index.html
[root@137 ~]# systemctl enable --now httpd              
[root@137 ~]# 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@137 ~]# curl 192.168.183.137
apache!

138 台のホストを構成する

[root@138 ~]# dnf -y install nginx
[root@138 ~]# echo 'nginx!' > /usr/share/nginx/html/index.html 
[root@138 ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@138 ~]# 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@138 ~]# curl 192.168.183.138
nginx!

136 台のホストを構成する

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

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

//下载软件包并解压编译
[root@136 ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@136 ~]# tar -xf nginx-1.20.2.tar.gz 
[root@136 ~]# cd nginx-1.20.2
[root@136 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@136 ~]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

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

//启动
[root@136 ~]# 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@136 ~]# systemctl daemon-reload
[root@136 ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@136 ~]# 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@136 ~]# vim /usr/local/nginx/conf/nginx.conf
    upstream webservers {
    
    
        server 192.168.183.137;
        server 192.168.183.138;
    }
    ······
    location / {
    
    
        proxy_pass http://webservers;
    }
    
//访问测试
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
nginx!
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
nginx!
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
nginx!

Apacheへのマルチアクセスを設定する

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

//测试
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
nginx!
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
nginx!

Apache への最初のアクセスである ip_hash を設定すると、常に Apache になります。


    upstream webservers {
    
    
        ip_hash;
        server 192.168.183.137 weight=3;
        server 192.168.183.138;
    }
    
//访问测试
[root@136 ~]# curl 192.168.183.136                
apache!
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
apache!
[root@136 ~]# curl 192.168.183.136
apache!

高可用性の構成

135 台のホストを構成する

//安装nginx
[root@135 ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
[root@135 ~]# useradd -rMs /sbin/nologin nginx   
[root@135 ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@135 ~]# tar -xf nginx-1.20.2.tar.gz
[root@135 ~]# cd nginx-1.20.2
[root@135 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@135 nginx-1.20.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
[root@135 nginx-1.20.2]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
[root@135 nginx-1.20.2]# source /etc/profile.d/nginx.sh
[root@135 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@135 nginx-1.20.2]# systemctl daemon-reload
[root@135 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@135 ~]# 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 関数をオフにする

135 ホスト構成ファイルを変更する

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

136 ホスト構成ファイルを変更する

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

キープアライブをインストールする

135 台のホストを構成する

[root@135 ~]# dnf -y install keepalived
[root@135 ~]# cd /etc/keepalived/
[root@135 keepalived]# mv keepalived.conf{,-bak}
[root@135 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 123456
    }
    virtual_ipaddress {
    
      
        192.168.183.250 dev ens33
    }
}
virtual_server 192.168.183.250 80 {
    
    
    delay_loop 3
    lvs_sched rr
    lvs_method DR
    protocol TCP
    real_server 192.168.183.135 80 {
    
    
        weight 1
        TCP_CHECK {
    
    
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.183.136 8080 {
    
    
        weight 1
        TCP_CHECK {
    
    
            connect_port 8080
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@135 keepalived]# systemctl enable --now keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
[root@135 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.183.135/24 brd 192.168.183.255 scope global dynamic noprefixroute ens33
       valid_lft 1122sec preferred_lft 1122sec
    inet 192.168.183.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

136 台のホストを構成する

[root@136 ~]# dnf -y install keepalived 
[root@136 ~]# cd /etc/keepalived/
[root@136 keepalived]# mv keepalived.conf{,-bak}
[root@136 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 123456
    }
    virtual_ipaddress {
    
      
        192.168.183.250 dev ens33
    }
}
virtual_server 192.168.183.250 80 {
    
    
    delay_loop 3
    lvs_sched rr
    lvs_method DR
    protocol TCP
    real_server 192.168.183.135 80 {
    
    
        weight 1
        TCP_CHECK {
    
    
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.183.136 8080 {
    
    
        weight 1
        TCP_CHECK {
    
    
            connect_port 8080
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

[root@136 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.183.136/24 brd 192.168.183.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

135 マスターサーバー障害をシミュレート

[root@135 ~]# systemctl stop keepalived
[root@135 ~]# systemctl stop nginx
[root@135 ~]# 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.183.135/24 brd 192.168.183.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 は 136 スタンバイ サーバーに送信されます

[root@136 ~]# 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.183.136/24 brd 192.168.183.255 scope global dynamic noprefixroute ens33
       valid_lft 1387sec preferred_lft 1387sec
    inet 192.168.183.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.183.250
apache!

おすすめ

転載: blog.csdn.net/qq_65998623/article/details/127373990