1. 程式人生 > >linux安裝keepalived

linux安裝keepalived

1.下載keepalived

cd/usr/local && wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz

2.解壓安裝:

tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/

3.下載外掛openssl(需要安裝一個軟體包)

yum install -y openssl openssl-devel

4.開始編譯keepalived

cd keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived

5.make一下

make && make install

如果報錯: eepalived執行./configure --prefix=/usr/local/keepalived時報錯:configure: error: Popt libraries is required

出現此錯誤的原因:

未安裝popt的開發包

解決方法:

yum install popt-devel

安裝好popt的開發包。重新./configure 即可。

  • keepalived安裝成Linux系統服務

將keepalived安裝成Linux系統服務,因為沒有使用keepalived的預設安裝路徑(預設路徑:/usr/local),安裝完成之後,需要做一些修改工作:

首先建立資料夾,將keepalived配置檔案進行復制:

mkdir /etc/keepalived

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

然後複製keepalived指令碼檔案:

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

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

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

可以設定開機啟動:chkconfig keepalived on,到此我們安裝完畢!

  • keepalived 常用命令

service keepalived start

service keepalived stop

啟動報錯Starting keepalived (via systemctl):  Job for keepalived.service failed. See 'systemctl status keepalived.service' and 'journalctl -xn' for details. 

解決辦法

[[email protected] sbin]# cd /usr/sbin/ 

[[email protected] sbin]# rm -f keepalived  

[[email protected] sbin]# cp /usr/local/keepalived/sbin/keepalived  /usr/sbin/  

  • 使用keepalived虛擬VIP

cd /etc/keepalived

rm -rf keepalived.conf

vi keepalived.conf

! Configuration File for keepalived

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh" #執行指令碼,指令碼內容下面有,就是起到一個nginx宕機以後,自動開啟服務
    interval 2 #檢測時間間隔
    weight -20 #如果條件成立的話,則權重 -20
}

# 定義虛擬路由,VI_1 為虛擬路由的標示符,自己定義名稱
vrrp_instance VI_1 {
    ###MASTER 主的意思  BACKUP 從
    state MASTER #來決定主從
    interface ens33 # 繫結虛擬 IP 的網路介面,根據自己的機器填寫
    virtual_router_id 121 # 虛擬路由的 ID 號,隨便定義一個數字, 兩個節點ID號必須一樣
    mcast_src_ip 192.168.128.152 #填寫本機ip
    priority 100 # 節點優先順序,主要比從節點優先順序高
    nopreempt # 優先順序高的設定 nopreempt 解決異常恢復後再次搶佔的問題
    advert_int 1 # 組播資訊傳送間隔,兩個節點設定必須一樣,預設 1s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    # 將 track_script 塊加入 instance 配置塊
    track_script {
        chk_nginx #執行 Nginx 監控的服務
    }
    ### 虛擬IP地址配置規範 跟本機IP同一網段
    virtual_ipaddress {
        192.168.128.100 # 虛擬ip,也就是解決寫死程式的ip怎麼能切換的ip,也可擴充套件,用途廣泛。可配置多個。
    }
}

啟動nginx

ip addr檢視,虛擬出來了一個IP 

訪問nginx主頁 

 訪問虛擬IP主頁