首頁 > 軟體

使用nginx+tomcat+keepalived實現高可用的詳細步驟

2022-03-18 13:02:32

架構設計

IP服務應用
192.168.166.128nginx/tomcat/keepalived
192.168.166.129nginx/tomcat/keepalived
192.168.166.130虛擬IP

我這是測試環境,防火牆全部關閉了。

# 檢視防火牆狀態
systemctl status firewalld
# 關閉防火牆
systemctl stop firewalld

安裝tomcat服務

192.168.166.128

在tomcat服務webapps下建立一個目錄:zkc
當前目錄:

mdkir -p webapps/zkc
echo "192.168.166.128" >> webapps/zkc/a.html
#啟動tomcat
./bin/startup.sh

存取地址:http://192.168.166.128:8080/zkc/a.html

192.168.166.129

在tomcat服務webapps下建立一個目錄:zkc
當前目錄:

mdkir -p webapps/zkc
echo "192.168.166.129" >> webapps/zkc/a.html
#啟動tomcat
./bin/startup.sh

存取地址:http://192.168.166.129:8080/zkc/a.html

安裝nginx服務

可以直接到官網下載或者通過yum下載
兩臺機器安裝步驟一樣

# 安裝nginx
yum -y install nginx
# 進入組態檔目錄
cd /etc/nginx/
# 修改組態檔
vim nginx.conf

upstream app_server{
        server  192.168.166.128:8080 weight=1 max_fails=1 fail_timeout=100;
        server  192.168.166.129:8080 weight=1 max_fails=1 fail_timeout=100;
        }
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location /zkc {
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_connect_timeout 10;
                proxy_read_timeout 600;
                proxy_send_timeout 600;
                proxy_pass http://app_server;
                access_log /var/log/nginx/access-app.log main;
    }
# 啟動
nginx

存取: curl http://192.168.166.128/zkc/a.html

存取:curl http://192.168.166.129/zkc/a.html

安裝keepalived

安裝keepalived兩臺機器一樣
可以上網下載keepalived或者通過yum安裝

# 安裝
yum -y install keepalived
cd /etc/keepalived/
# 建立檢測檔案
vim chk_nginx.sh
# 賦予可執行許可權
chmod +x chk_nginx.sh

chk_nginx.sh 內容

#!/bin/bash
status=$(ps aux|grep nginx | grep -v grep | grep -v bash | wc -l)
#echo $status >> check.log
if [ "${status}" = "0" ]; then
        #echo "nginx已關閉,正在重啟" >> check.log
        # 啟動nginx
        /usr/sbin/nginx

        status=$(ps -C nginx --no-header|wc -l)
        #echo $status >> check.log
        # 再次判斷
        if [ $status -eq 0 ];then
        #       echo "nginx已關閉,正在重啟" >> check.log
                # 關閉keepalived
                systemctl stop keepalived;
        fi
fi

建立keepalived組態檔

# 備份原有keepalived.conf內容
mv keepalived.conf keepalived.conf_bk
vim keepalived.conf
global_defs {
   router_id LVS_Ha2
}

vrrp_script chk_mysql {
    script "/etc/keepalived/chk_nginx.sh"
    interval 2
}
vrrp_instance HA_1 {
        state BACKUP
        interface ens33
        virtual_router_id 80
        priority 80
        advert_int 2
        nopreempt
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
          192.168.166.130
        }
        track_script {
        chk_mysql
        }
}

啟動keepalived

systemctl start keepalived
#檢視虛擬IP
ip add

存取虛擬IP環境:curl http://192.168.166.130/zkc/a.html

說明環境已經搭建成功

驗證

1、192.168.166.128 中nginx突發故障,但是keepalived使得nginx又重新啟動

2、重啟伺服器模擬伺服器故障宕機
192.168.166.128重啟
192.168.166.129,此時發現虛擬IP漂移到此伺服器上

為什麼沒有128呢,是因為剛才服務重啟,但是tomcat、nginx、keepalived服務沒有做開機自啟。

到此這篇關於nginx+tomcat+keepalived實現高可用的文章就介紹到這了,更多相關nginx+tomcat+keepalived高可用內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com