keepalived nginx 主備配置

2022-05-26 07:48:10 字數 2265 閱讀 1108

nginx 不區分主備,在兩台服務上安裝兩個即可。

安裝參考:

keepalived 區分主備、多主多備的。

keepalived 多主多備 是把 單主單備 複製多份,調下優先順序配置即可。

安裝參考:

主備配置參考:

兩台伺服器(已按照1、2教程安裝)

ip.192.168.159.129

nginx 已安裝

keepalived 已安裝

ip.192.168.159.130

nginx 已安裝

keepalived 已安裝

192.168.159.129 (主)keepalived.conf

cd /etc/keepalived/

#進入預設配置目錄

vim keepalived.conf

#編輯配置資訊

謹記:該配置慎用空格,可以使用tab或回車排版,空格會被識別為賦值,導致keepalived執行失敗

修改為如下內容

! configuration file for keepalived

vrrp_script chk_nginx

vrrp_instance vi_1

track_script

virtual_ipaddress

}

192.168.159.130(備) keepalived.conf

cd /etc/keepalived/

#進入預設配置目錄

vim keepalived.conf

#編輯配置資訊

修改為以下內容

! configuration file for keepalived

vrrp_script chk_nginx

vrrp_instance vi_1

track_script

virtual_ipaddress

}

192.168.159.129(主)nginx_chk.sh

cd /etc/keepalived/script/

#無目錄,自己建立

vim nginx_chk.sh

編輯指令碼

新增如下內容

#!/bin/bash

if [ "

$(ps -c nginx --no-heading|wc -l)

" = "

0" ]; then

/usr/local/nginx/sbin/nginx

sleep

2if [ "

$(ps -c nginx --no-heading|wc -l)

" = "

0" ]; then

/etc/init.d/keepalived stop

fifi

儲存chmod 775 nginx_chk.sh  #賦值執行許可權 

192.168.159.130(備) nginx_chk.sh

cd /etc/keepalived/script/

#無目錄,自己建立

vim nginx_chk.sh

編輯指令碼

新增如下內容

#!/bin/bash

if [ "

$(ps -c nginx --no-heading|wc -l)

" = "

0" ]; then

/usr/local/nginx/sbin/nginx

sleep

2if [ "

$(ps -c nginx --no-heading|wc -l)

" = "

0" ]; then

/etc/init.d/keepalived stop

fifi

儲存chmod 775 nginx_chk.sh

#賦值執行許可權 

1)停止nginx,nginx自動重啟成功

2)停止主keepalived,虛擬ip自動切換至備keepalived

3)訪問虛擬ip自動跳轉至nginx

4)其他測試不演練了

keepalived nginx主從切換

五 nginx指令碼,新建nginx check.sh指令碼放到 etc keepalived下 指令碼內容 bin bash a ps c nginx no header wc l if a eq 0 then usr local nginx sbin nginx sleep 2 if ps c ...

keepalived nginx集群部署

192.168.132.130 伺服器1 192.168.132.132 伺服器2 虛擬ip 192.168.132.131 yum install gcc c gcc是gnu的c語言編譯器,至於c 編譯器,它的名字叫做g yum install y pcre pcre devel 在nginx編譯...

keepalived nginx負載安裝

環境 centos7.6 keepalived工具可以生成虛擬浮動ip,繫結到網絡卡上。172.21.210.19 master 172.21.210.20 backup 172.21.210.30 vip 浮動ip 1 兩個節點安裝keepalived yum y install keepaliv...