keepalived nginx 高可用集群

2021-09-10 03:20:13 字數 2724 閱讀 2985

nginx做負載均衡,能達到分發請求的目的,但是不能很好的避免單點故障。

$ tcpdump -nn -vvv -i ens33 vrrp     #檢視組播的包

192.168.31.40 > 224.0.0.18: vrrp 192.168.31.40 > 224.0.0.18: vrrpv2, advertisement, vrid 51, prio 100, authtype ******, intvl 1s, length 20, addrs: 192.168.31.213 auth "1111^¥##"

輸出解析:192.168.31.40是master,目標位址是224.0.0.18,使用的協議是vrrpv2版本,虛擬id是51,優先順序是100,簡單驗證型別,一秒發一次,長度20位元組,虛ip是192.168.31.213, 驗證的密碼是111#$z%#

$ wget
使用如下shell指令碼keepalived_instll.sh安裝keepalived軟體。

$ sh keepalived_install.sh 

#keepalived安裝指令碼

#!/bin/bash

pkg=keepalived-2.0.8.tar.gz tar xf $pkg

yum -y install kernel-devel

ln -s /usr/src/kernels/3.10.0-862.14.4.el7.x86_64/ /usr/src/linux

cd keepalived-2.0.8/

yum install openssl-* -y

./configure --prefix=/usr/local/keepalived

make

make install

mkdir -pv /etc/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

ln -s /usr/local/keepalived/sbin/keepalived /sbin/

$ systemctl start keepalived
主機名

ip角色

系統配置

軟體master.ayitula.com

192.168.31.40

主分發器

centos7.5

2核4g

nginx+keepalived

backup.ayitula.com

192.168.31.41

備分發器

centos7.5

2核4g

nginx+keepalived

web01.ayitula.com

192.168.31.42

資料伺服器1

centos7.5

2核4g

nginx

web02.ayitula.com

192.168.31.43

資料伺服器2

centos7.5

2核4g

nginx

1)分發器:nginx+keepalived

2)資料伺服器:nginx

3)配置nginx分發器

4)配置資料伺服器頁面(web01 web02)

5)配置keepalived.conf

6)測試

upstream web 

server

}

(1)主分發器keepalived
! configuration file for keepalived

global_defs

vrrp_script check_nginx

vrrp_instance nginx

track_script

virtual_ipaddress

}

(2)備分發器keepalived
#cat /etc/keepalived/keepalived.conf 

! configuration file for keepalived

global_defs

vrrp_script check_nginx

vrrp_instance nginx

track_script

virtual_ipaddress

}

#!/bin/bash

nginx_kp_check ()

nginx_kp_check

指令碼原理:該指令碼檢查nginx程序是否存在,如果程序沒了,說明分發器掛了,嘗試啟動分發器,1秒後檢查分發器啟動沒有,如果沒有啟動直接關閉keepalived,停止發組播,備就自動開始工作了。

分發器由keepalived來管理

$ watch  -n1 killall nginx    # 每隔一秒殺死一次所有nginx
資料伺服器則由nginx來管理。使用如下兩個引數就可實現對資料伺服器管理:

upstream web

posted @

2018-12-24 23:36

休耕 閱讀(

...)

編輯收藏

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...