Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現

実験環境

2つのCentOS-7.5仮想マシン
web1:10.0.11.203
web2:10.0.11.204
VIP:10.0.11.210
webタイプ:nginx
クライアント:自己使用ノートブック(win10)
nginxステータス検出スクリプト:ck_nginx.sh

実験1、keepalivedを使用してWebクラスターの高可用性機能を簡単に実現する

1. 2つのWebサーバーを準備する

1)Web1ネットワークカードの状況
[root @ CentOS〜]#
[root @ CentOS〜]#ip a
Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現

2)Web2ネットワークカードの状況
[root @ CentOS-2〜]#
[root @ CentOS-2〜] #ip a
Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現

3)nginxをインストールします

web1 end:
[root @ CentOS〜]#
[root @ CentOS〜] #yum install nginx
[root @
CentOS〜
] #rpm -qa nginx nginx-1.16.1-1.el7.x86_64 [root @ CentOS〜]#
[ root @
CentOS〜]#[root @ CentOS〜]#> /usr/share/nginx/html/index.html#デフォルトのWebページを空にする
[root @ CentOS〜]#echo web1> / usr / share / nginx / html / index .html #Webページを表示させるweb1
[root @ CentOS〜]
#cat /usr/share/nginx/html/index.html web1
[root @ CentOS〜]#
[root @ CentOS〜]#systemctl start nginx #Start nginx
[root @CentOS〜]#systemctl enable nginx #nginxを起動後に自動的に起動するように設定
C:\ Users \ Administrator> curl 10.0.11.203
Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現

web2端:
[root @ CentOS-2〜]#
[root @ CentOS-2〜] #yum install nginx
[root @ CentOS-2〜]
#rpm -qa nginx nginx-1.16.1-1.el7.x86_64
[root @ CentOS-2〜]#
[root @ CentOS-2〜]#
[root @ CentOS-2〜]#> /usr/share/nginx/html/index.html
[root @ CentOS-2〜]#echo web2> /usr/share/nginx/html/index.html
[root @ CentOS-2〜]
#cat /usr/share/nginx/html/index.html web2
[root @ CentOS-2〜]#
[root @ CentOS-2 〜]#systemctl start nginx
[root @ CentOS-2〜]#systemctl enable nginx
C:\ Users \ Administrator> curl 10.0.11.204
Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現

4)keepalivedのインストール
手順:両方のWebサーバーでkeepalivedサービスをインストールする必要があります

web1端:
[root @ CentOS〜]#
[root @ CentOS〜]#yum install keepalived
[root @
CentOS〜
] #rpm -qa keepalived keepalived-1.3.5-16.el7.x86_64 [root @ CentOS〜]#

web2端:
[root @ CentOS-2〜]#
[root @ CentOS-2〜] #yum install keepalived
[root @ CentOS-2〜]
#rpm -qa keepalived keepalived-1.3.5-16.el7.x86_64
[root @ CentOS-2〜]#

5)2つのkeepalivedを構成して開始

web1 end:
[root @ CentOS〜]#
[root @ CentOS〜]#> /etc/keepalived/keepalived.conf#元の構成を空にする
[root @ CentOS〜]#vim /etc/keepalived/keepalived.conf#構成を編集ファイル、カスタムkeepalived構成ファイル
[root @ CentOS〜]#cat /etc/keepalived/keepalived.conf
Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現
[root @ CentOS〜]#
[root @ CentOS〜]#systemctl start keepalived #start
[root @ CentOS〜]#systemctl keepalivedを有効にする#Set boot start

web2 end:
[root @ CentOS-2〜]#
[root @ CentOS-2〜]#> /etc/keepalived/keepalived.conf
[root @ CentOS-2〜]#vim /etc/keepalived/keepalived.conf
[root @ CentOS-2〜]#cat /etc/keepalived/keepalived.conf
Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現
[root @ CentOS-2〜]#
[root @ CentOS-2〜]#systemctl start keepalived #Start
[root @ CentOS-2〜]#systemctl enable keepalived #Set boot

3.テストアクセス
方法:クライアントを使用して、VIPテストを通じてWebにアクセスします

C:\ Users \ Administrator> curl 10.0.11.210#個人のノートブックがVIPテストを通じてWebにアクセスする
Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現

web1サーバーのネットワークケーブルを取り外し、クライアントを使用してVIPテストを通じてWebにアクセスします:
C:\ Users \ Administrator> curl 10.0.11.210
Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現

web1サーバーのネットワークケーブルを再度接続し、クライアントを使用してVIPテストを通じてWebにアクセスします:
C:\ Users \ Administrator> curl 10.0.11.210
Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現

4.実験の結論:
1)keepalivedを使用して、Webサーバークラスターの高可用性が正常に達成されました
。2)サーバーの異常な高障害の場合、Webの高可用性を実現できますが、残念ながら、Webサービスの例外を達成することはできません。障害が原因の高可用性スイッチオーバー。keepalivedは、nginxサービスの実行ステータスとヘルスステータスを認識できません。


実験2、keepalivedをnginx監視および検出スクリプトと組み合わせて使用​​して、より柔軟で可用性の高いクラスター機能を実現する

実験環境:
実験1 の環境に基づくと、実験1と実験1の唯一の違いは、nginxヘルスステータス検出スクリプトを準備する必要があることです。

1. nginxヘルス検出スクリプトを記述します

web1側で実行:
[root @ CentOS〜]#
[root @ CentOS〜]#vim /etc/keepalived/ck_nginx.sh #nginxのヘルスステータスを検出するためのkeepalivedスクリプトを作成します

#!/ bin / bash
#nginプロセスが存在するかどうかを確認します
counter = $(ps -C nginx --no-heading | wc -l)
if ["$ {counter}" = "0"];その後、
#ngnxを1回だけ起動します、5秒停止した後、再度確認します
systemctl start nginx
sleep 5
counter = $(ps -C nginx --no-heading | wc -l)
if ["$ {counter}" = "0"];その後、#
起動が失敗した場合、keep-aveを強制終了してマスタースレーブスイッチをトリガーします
systemctl stop nginx
fi
fi
[root @ CentOS〜]#
[root @ CentOS〜]#cat /etc/keepalived/ck_nginx.sh#スクリプトの内容を表示
Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現
[root @ CentOS〜]#
[root @CentOS〜]#chmod + x /etc/keepalived/ck_nginx.sh#スクリプトの実行権限を付与
[root @ CentOS〜]#

Web2の実行終了:
[root @ CentOS-2〜]#
[root @ CentOS-2〜]#vim /etc/keepalived/ck_nginx.sh

#!/ bin / bash
#nginプロセスが存在するかどうかを確認します
counter = $(ps -C nginx --no-heading | wc -l)
if ["$ {counter}" = "0"];その後、
#ngnxを1回だけ起動します、5秒停止した後、再度確認します
systemctl start nginx
sleep 5
counter = $(ps -C nginx --no-heading | wc -l)
if ["$ {counter}" = "0"];その後、#
起動が失敗した場合、Keepaveを強制終了してマスター/スタンバイスイッチ
systemctl stop nginx
fi
fi をトリガーします

[root @ CentOS-2〜]#
[root @ CentOS-2〜]#
[root @ CentOS-2〜]#cat /etc/keepalived/ck_nginx.sh
Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現
[root @ CentOS-2〜]#
[root @ CentOS- 2〜]#chmod + x /etc/keepalived/ck_nginx.sh
[root @ CentOS-2〜]#

2. nginx構成ファイル
web1 endを編集して変更します
[root @ CentOS〜]#
[root @ CentOS〜]#vim /etc/keepalived/keepalived.conf #Custom configuration file

keepalivedの構成ファイル

global_defs {
notification_email { br /> [email protected]
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id 1
}

vrrp_script chk_nginx {
script "/etc/keepalived/ck_nginx.sh"
interval 2
weight -5
fail 3
}

vrrp_instance VI_1 {
state MASTER
interface ens32
mcast_src_ip 10.0.11.203
virtual_router_id 55
priority 100
advert_int 1

authentication {
auth_type PASS
auth_pass 123456
}

virtual_ipaddress {
    10.0.11.210/24
}

track_script {
     chk_nginx
}

}

[root @
CentOS〜]#[root @ CentOS〜]#systemctl restart keepalived#構成を有効にするためにkeepalivedを再起動[root @ CentOS〜
]#

web2の終わり:
[root @ CentOS-2〜]#
[root @ CentOS-2〜]#vim /etc/keepalived/keepalived.conf#カスタム構成ファイル

keepalivedの構成ファイル

global_defs {
notification_email {
br /> [email protected]
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id 1
}

vrrp_script chk_nginx {
script "/etc/keepalived/ck_nginx.sh"
interval 2
weight -5
fail 3
}

vrrp_instance VI_1 {
state MASTER
interface ens32
mcast_src_ip 10.0.11.204
virtual_router_id 55
priority 95
advert_int 1

authentication {
auth_type PASS
auth_pass 123456
}

virtual_ipaddress {
    10.0.11.210/24
}

track_script {
     chk_nginx
}

}

[root @ CentOS-2〜]#
[root @ CentOS-2〜]#
[root @ CentOS-2〜]#systemctl restart keepalived #Restart keepalived設定を有効にする

keepalived関数をテストします。
スクリプトの内容により、nginxサービスは停止した直後に開始するため、サーバーの異常シャットダウンをシミュレートし、nginxサービスが自動的に開始する方法がないようにします
[root @ CentOS〜]#
[root @ CentOS〜]#init 0# web1サーバーをシャットダウンします。

[root @ CentOS-2〜]#
[root @ CentOS-2〜]#ping 10.0.11.203#web2を使用してpingweb1を実行し、web1がシャットダウンされていることを確認します
Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現

この時点で、クライアントを使用してkeepalived関数をテストおよび検証します
。C:\ Users \ Administrator> curl 10.0.11.210#クライアントを使用してVIP経由でWebサーバーにアクセスします
Keepalivedとnginxステータス検出スクリプトを組み合わせて、Webサーバークラスターの高可用性を実現

3.実験のまとめ2
nalixヘルスステータス検出スクリプトと組み合わせたkeepalivedサービスを使用すると、Webサーバークラスターの高可用性が正常に達成されました

おすすめ

転載: blog.51cto.com/14783377/2486411