實驗拓撲圖
(1)本次基於VMware Workstation搭建一個四臺Linux(CentOS 7.4)系統所構成的一個服務器集羣,其中兩臺nginx作前端調度服務器(一臺爲主機,另外一臺爲備機),另外兩臺做爲真實的Web服務器(向外部提供http服務,這裏僅僅使用了CentOS默認自帶的http服務,沒有安裝其餘的相似Tomcat、Jexus服務)。html
(2)本次實驗設置了一個VIP(Virtual IP)爲172.18.38.99,用戶只須要訪問這個IP地址便可得到網頁服務。其中,nginx主機爲172.18.38.100,備機爲172.18.38.101。Web服務器A爲172.18.38.200,Web服務器B爲172.18.38.201。前端
1,在http語句塊中定義調度規則 vim /etc/nginx/nginx.conf http { ..... upstream webser { server 172.18.38.200:80; server 172.18.38.201:80; } ..... } 2,然後在server中調用 vim /etc/nginx/conf.d/vhost.conf server { listen 172.18.38.99:80; server_name www.a.com; location / { proxy_pass http://webser; } }
2,重啓nginx,使配置生效nginx
systemctl restart nginx
1,安裝keepalivedweb
yum install keepalived
2,修改keepalived配置文件vim
vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id proxy1 vrrp_mcast_group4 224.1.1.1 } vrrp_script chk_nginx { script "killall -0 nginx && exit 0 || exit 1" interval 1 weight -30 fall 2 rise 2 } vrrp_instance VI_1 { state MASTER interface ens37 virtual_router_id 66 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 172.18.38.99/16 } track_script { chk_nginx } } 關鍵配置: 1,定義nginx健康檢查腳本 vrrp_script chk_nginx { script "killall -0 nginx && exit 0 || exit 1" interval 1 #以秒觸發一次 weight -30 #nginx宕機就立馬減去有限級30 fall 2 #檢查兩次若是都是宕機就表示nginx掛掉了 rise 2 #宕機以後兩次檢查nginx是活着的就從新+30優先級 } 2,調用 track_script { chk_nginx
3,啓動keepalived後端
systemctl start keepalived systemctl enable keepalived
vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id proxy2 vrrp_mcast_group4 224.1.1.1 } vrrp_instance VI_1 { state BACKUP interface ens37 virtual_router_id 66 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 172.18.38.99/16 } }
7,啓動keepalived服務器
systemctl start keepalived systemctl enable keepalived
1,安裝httpd軟件curl
1,安裝httpd軟件 yum install http 2,啓動服務 systemctl start httpd systemctl enable httpd 2,生成web頁面, A主機 echo web_server_A > /var/www/html/index.html b主機 echo web_server_B > /var/www/html/index.html
1,不當任何服務ide
[root@testsrvr ~]# for i in {1..10};do sleep 0.5;curl 172.18.38.99;done web_server_B web_server_B web_server_A web_server_A web_server_B web_server_B web_server_A web_server_A web_server_B web_server_B
2,宕一臺keepalived服務測試
[root@ke_nginx_S ~]# systemctl stop keepalived.service [root@testsrvr ~]# for i in {1..10};do sleep 0.5;curl 172.18.38.99;done web_server_B web_server_B web_server_A web_server_A web_server_B web_server_B web_server_A web_server_A web_server_B web_server_A
3,宕掉nginx_master服務器
[root@ke_nginx-M ~]# systemctl stop nginx [root@testsrvr ~]# for i in {1..100};do sleep 0.5;curl 172.18.38.99;done web_server_A web_server_A web_server_B web_server_B web_server_A curl: (7) couldn't connect to host curl: (7) couldn't connect to host curl: (7) couldn't connect to host curl: (7) couldn't connect to host web_server_B web_server_B web_server_A web_server_A