Nginx+Keepalived實現簡單的服務高可用
一般情況下,如果我們做小型項目,前端用一個nginx做反向代理即可,大概是這樣的

image.png
但是,作為互聯網項目,純2C的話必然需要做高可用,不僅后端的Server有N個,Nginx同樣需要有N個,一主N備,當有一個服務器掛掉的時候,服務能瞬間切換到其他服務器,大概是這樣的

image.png
下面就以上圖為例,說明一下如何實現server的高可用。
1、准備
虛擬機兩台,同樣安裝nginx,keepalived,最簡單的安裝方法yum -y install nginx,yum -y install keepalived。如果找不到安裝到哪兒了,可以使用whereis nginx查看,這里不再贅述。
網絡划分如下
名稱 | IP | 虛擬IP | 操作系統 |
---|---|---|---|
虛擬機1(VM1) | 192.168.136.2 | 192.168.136.99 | centos7.6 |
虛擬機2(VM2) | 192.168.136.4 | 192.168.136.99 | centos7.6 |
2、關閉防火牆,修改nginx首頁,啟動nginx
- 關閉防火牆
systemctl stop firewalld.service #臨時關閉,重啟失效
systemctl disable firewalld.service #禁止開機啟動
- 簡單起見,我們認為每個nginx都是代理一個服務,只用nginx默認帶的靜態頁作為測試,分別修改頁面內容為"Welcome to 192.168.136.4"和“Welcome to 192.168.136.2”
- 啟動nginx
systemctl start nginx
3、修改keepalived的配置文件
主配置如下(默認配置文件:/etc/keepalived/keepalived.conf):
! Configuration File for keepalived global_defs { # notification_email { # acassen@firewall.loc # failover@firewall.loc # sysadmin@firewall.loc # } # notification_email_from Alexandre.Cassen@firewall.loc # smtp_server 192.168.200.1 # smtp_connect_timeout 30 router_id LVS_DEVEL # vrrp_skip_check_adv_addr # vrrp_strict # vrrp_garp_interval 0 # vrrp_gna_interval 0 } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } vrrp_instance VI_1 { state MASTER # 標識為主服務 interface ens33 #綁定虛擬機的IP virtual_router_id 51 # 虛擬路由id,和從機保持一致 #mcast_src_ip 192.168.126.2 #本機ip priority 100 #權重,需要高於從機 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx ## 執行 Nginx 監控的服務 } virtual_ipaddress { 192.168.136.99 #/32 brd 255.255.255.0 dev ens33 label ens33:vip #虛擬IP地址 # 192.168.200.17 # 192.168.200.18 } }
從機配置(默認配置文件:/etc/keepalived/keepalived.conf)
! Configuration File for keepalived global_defs { # notification_email { # acassen@firewall.loc # failover@firewall.loc # sysadmin@firewall.loc # } # notification_email_from Alexandre.Cassen@firewall.loc # smtp_server 192.168.200.1 # smtp_connect_timeout 30 router_id dreamer1 # vrrp_skip_check_adv_addr # vrrp_strict # vrrp_garp_interval 0 # vrrp_gna_interval 0 } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" ## 檢測 nginx 狀態的腳本路徑 interval 2 ## 檢測時間間隔 weight -20 ## 如果條件成立,權重-20 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 #mcast_src_ip 192.168.136.4 ## 本機 IP 地址 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx ## 執行 Nginx 監控的服務 } virtual_ipaddress { 192.168.136.99 #192.168.200.17 #192.168.200.18 } }
3、編寫監測心跳腳本
上面配置中可以看到有一個腳本文件:/etc/keepalived/nginx_check.sh
查看nginx是否啟動,如果沒啟動則啟動,如果啟動不起來,停掉keepalived服務,此時心跳斷掉,服務轉向另一個nginx。
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/usr/sbin/nginx
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/etc/init.d/keepalived stop
fi
fi
4、測試
- 啟動192.168.136.2上的nginx和keepalive
- 啟動192.168.136.4上的nginx和keepalive
-
訪問虛擬IP:http://192.168.136.99
image.png -
停掉192.168.136.2上的keepalive
image.png - 重新啟動192.168.136.2上的keepalive,又會回到 Welcome to 192.168.136.2
-
停掉192.168.136.2上的nginx,系統會自動調回Welcome to 192.168.136.4
image.png
就是這樣。