Keepalived Nginx主備切換實現高可用

2021-08-17 13:01:46 字數 3849 閱讀 9690

keepalived+nginx主備切換

實現高可用

keepalived:

基於vrrp(虛擬路由器冗餘協議

)來實現對web服務的高可用方案

安裝keepalived

1.  ./configure --prefix=/data/program/keepalived --sysconf=/etc

2. 缺少依賴opensssl .  yum install openssl-devel

3. 建立軟鏈結 ln -s /data/program/keepalived/sbin/keepalived /sbin

4.  ln -s /data/program/keepalived/sbin/keepalived /sbin

5. cp /data/program/keepalived-1.3.4/keepalived/etc/init.d/keepalived /etc/init.d/

6. 新增系統服務:chkconfig  --add keepalived / chkconfig keepalived on

7. 服務開關 service keepalived start|stop..

keepalived日誌檔案配置

在/etc/rsyslog.conf裡新增:local0.*  /var/log/keepalived.log

重新啟動keepalived和rsyslog服務:

service rsyslog restart

service keepalived restart

tail -f  /var/log/keepalived.log  檢視日誌

keepalived+nginx實現雙機熱備

開發乙個虛擬ip:192.168.130.100

配置兩台nginx:ip192.168.130.130、131

修改keepalived配置檔案

vi /etc/keepalived/keepalived.conf

master(主

)  keepalived配置檔案

backup(備

)  keepalived配置檔案

vrrp_script check {

script "/data/program/nginx/sbin/nginx_service.sh"

interval 2

weight -9

global_defs {

router_id lvs_devel

vrrp_instance vi_1 {

state master

inte***ce ens33

virtual_router_id 51

priority 60

advert_int 1

authentication {

auth_type pass

auth_pass 2222

virtual_ipaddress {

192.168.232.100

track_script {

check

virtual_server 192.168.232.100 80 {

delay_loop 5

lb_algo rr

lb_kind nat

persistence_timeout 51

protocol tcp

real_server 192.168.232.133 80 {

weight 1

tcp_check {

connect_timeout 2

nb_get_retry 2

delay_before_retry 2

connect_port 80

vrrp_script check {

script "/data/program/nginx/sbin/nginx_service.sh"

interval 2

weight -9

global_defs {

router_id lvs_devel

vrrp_instance vi_1 {

state backup

inte***ce ens33

virtual_router_id 51

priority 40

advert_int 1

authentication {

auth_type pass

auth_pass 2222

virtual_ipaddress {

192.168.232.100

track_script {

check

virtual_server 192.168.232.100 80 {

delay_loop 5

lb_algo rr

lb_kind nat

persistence_timeout 51

protocol tcp

real_server 192.168.232.132 80 {

weight 1

tcp_check {

connect_timeout 2

nb_get_retry 2

delay_before_retry 2

connect_port 80

配置檔案說明

1. vrrp_instance配置:

state

指定初始狀態(主、備)。

inte***ce

為網絡卡名字。

virtual_router_id

相同的為乙個組。priority為節點優先順序,主的要大於備的。

advert int

檢查間隔,預設 1秒。

auth type

為認證方式。track_script 引用指令碼。

2. virtual_server配置:

delay_loop

健康檢查間隔。lb_algo 排程演算法。 lb_kind負載均衡規則。

會話保持時間。protocol使用的協議。

3. real_server配置:

weight

預設為1,0為失效。

connect_timeout

連線超時時間。

nb_get_retry

重連次數。delay_before_retry 重連間隔時間

。connect_port健康檢查的埠的埠。

4. vrrp_script配置:interval指令碼執行間隔。

weight

指令碼優先順序。script 指令碼路徑。

5.nginx_service.sh判斷

nginx

是否宕機,若宕機,結束keepalived服務。

給指令碼許可權 chmod +x

/data/program/nginx/sbin/nginx_service.sh

指令碼內容:

#!/bin/sh

nginxpidnum=`ps-c nginx --no-header |wc -l`

keepalivedpidnum=`ps-c keepalived --no-header |wc -l`

if [$nginxpidnum -eq 0 ];then

killall keepalived

elif [$keepalivedpidnum -eq 0 ];then

service keepalived start

fi#p#分頁標題#e#

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