Nginx keepalived實現雙機熱備部署

2021-08-07 12:32:58 字數 2844 閱讀 4972

一、安裝必要的軟體包,以保證nginx和keepalived能安裝:

yum -y install gcc gcc-c++ make

yum -y install pcre-devel      #安裝nginx的依賴軟體包

yum -y install openssl-devel          #安裝keepalived依賴軟體包

yum -y install popt-devel          #安裝keepalived依賴軟體包

二、安裝nginx

tar -zxf nginx-1.2.8.tar.gz

./configure --prefix=/etc/local/nginx

make && make install

測試安裝成功

三、安裝keepalived-1.2.0

wget

(wget

tar -zxf keepalived-1.2.0.tar.gz

cd keepalived-1.2.0

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

錯誤:use ipvs framework  、 ipvs sync daemon support兩個必須為yes,才算編譯成功

四、複製檔案:

1、ln -s /usr/local/keepalived/etc/keepalived /etc/keepalived

2、ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived

/etc/rc.d/init.d/keepalived

3、ln -s /usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/keepalived

4、ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/keepalived

5、chkconfig keepalived on  

五、配置master配置檔案:

vi /etc/keepalived/keepalived.conf

! configuration file for keepalived

global_defs

notification_email_from [email protected]  #設定郵件的傳送位址

smtp_server 127.0.0.1            #設定smtp server位址

smtp_connect_timeout 30    #設定連線smtp server的超時時間

router_id lvs_devel           #表示執行keepalived伺服器的乙個標識。發郵件時顯示在郵件主題的資訊

}vrrp_script chk_nginx

vrrp_instance vi_1

virtual_ipaddress

track_script

}六、配置backup配置檔案:

vi /etc/keepalived/keepalived.conf

! configuration file for keepalived

global_defs

notification_email_from [email protected]

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id lvs_devel

}vrrp_script chk_nginx

vrrp_instance vi_1

virtual_ipaddress

track_script

}七、指令碼編寫:

vi /usr/local/keepalived/sbin/check_nginx.sh

#!/bin/bash

if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == ""

];then

#echo 1

/etc/init.d/nginx start

sleep 5

if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == ""

];then

/etc/init.d/keepalived stop

#echo 2

fifi

八、給指令碼執行許可權

chmod +x /usr/local/keepalived/sbin/check_nginx.sh

九、測試

service keepalived start  #啟動nginx-master

service keepalived start  #啟動nginx-backup

ip addr  #2臺伺服器分別執行,繫結虛擬ip在nginx-master

service keepalived stop  #停止nginx-backup

ip addr  #2臺伺服器分別執行,繫結虛擬ip在nginx-backup

service keepalived start  #再啟動nginx-backup

ip addr  #2臺伺服器分別執行,繫結虛擬ip在nginx-master

分享:yun部署keepalived 

nginx keepalived學習目錄

keepalived權威指南 深入理解nginx nginx模組開發與架構解析第二版 nginx開發從入門到精通 keepalived工作原理和配置說明 1 nginx安裝和介紹 2 nginx的啟動和停止 3 nginx程序模型 4 nginx處理請求過程 5 nginx配置一 6 nginx配置...

Nginx Keepalived 負載均衡

喜歡搞nginx,這兩天研究了一下nginx的負載配置 nginx平台搭建 nginx mysql php fpm搭建高效能nginx平台 單nginx負載均衡 nginx 負載均衡 配置全過程 熟悉上面的配置之後,今天我再來搞nginx keepalived 的配置,具體拓撲如下 一 安裝keep...

nginx keepalived雙機熱備

採用nginx實現反向 配置集群,實現軟f5的功能。nginx.conf的配置如下 server 192.168.2.134 8082 upstream socologin server access log home soco share nginx logs access.log access ...