Keepalived是一個免費開源的,用C編寫的相似於layer3, 4 & 7交換機制軟件,具有咱們平時說的第3層、第4層和第7層交換機的功能。主要提供loadbalancing(負載均衡)和 high-availability(高可用)功能,負載均衡實現須要依賴Linux的虛擬服務內核模塊(ipvs),而高可用是經過VRRP協議實現多臺機器之間的故障轉移服務。html
Keepalived的全部功能是配置keepalived.conf文件來實現的。linux
將從官網下載的keepalived-1.3.4.tar.gz包,上傳到/tmp目錄下。ios
tar -zxvfkeepalived-1.3.4.tar.gz服務器
cd keepalived-1.3.4網絡
./configure --prefix=安裝路徑負載均衡
make && make installurl
安裝完成後,進入安裝目錄的etc目錄下,將keepalived相應的配置文件拷貝到系統相應的目錄當中。keepalived啓動時會從/etc/keepalived目錄下查找keepalived.conf配置文件,若是沒有找到則使用默認的配置。/etc/keepalived目錄安裝時默認是沒有安裝的,須要手動建立。配置文件目錄結構以下所示:.net
-- keepalivedrest
| |-- keepalived.confcode
| `-- samples
| |-- keepalived.conf.status_code
| |-- keepalived.conf.track_interface
| |-- keepalived.conf.vrrp
| |-- 。。。
|-- rc.d
| `-- init.d
| `-- keepalived
`-- sysconfig
`-- keepalived
分別對應系統目錄:
/etc/keepalived/keepalived.conf
/etc/rc.d/init.d/keepalived
/etc/sysconfig/keepalived
將配置文件拷貝到系統對應的目錄下:
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived.conf /etc/keepalived/keepalived.conf
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/keepalived
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
keepalived服務控制
systemctl enable keepalived.service #設置開機自動啓動
systemctl disable keepalived.service #取消開機自動啓動
systemctl start keepalived.service #啓動服務
systemctl restart keepalived.service #重啓服務
systemctl stop keepalived.service #中止服務
systemctl status keepalived.service #查看服務狀態
設置防火牆打開
firewall-cmd --permanent–-add-service=keepalived
firewall-cmd --reload
再次強調:Keepalived的全部功能是配置keepalived.conf文件來實現的。
2.2 配置文件的詳細說明
keepalived.conf的配置
以下圖所示:
主機配置:
備機配置:
全局定義塊
一、email通知(notification_email、smtp_server、smtp_connect_timeout):用於服務有故障時發送郵件報警,可選項,不建議用。須要系統開啓sendmail服務,建議用第三獨立監控服務,如用nagios全面監控代替。
二、lvs_id:lvs負載均衡器標識,在一個網絡內,它的值應該是惟一的。
三、router_id:用戶標識本節點的名稱,一般爲hostname
四、花括號{}:用來分隔定義塊,必須成對出現。若是寫漏了,keepalived運行時不會獲得預期的結果。因爲定義塊存在嵌套關係,所以很容易遺漏結尾處的花括號,這點須要特別注意。
VRRP實例定義塊
在keepalived.conf配置文件中加入如下內容
1.notify_master「想要執行的腳本路徑」 #表示當切換到master狀態時,要執行的腳本
2.notify_backup 「想要執行的腳本路徑」#表示當切換到backup狀態時,要執行的腳本
3.notify_fault「想要執行的腳本路徑」#表示切換出現故障時要執行的腳本
轉載自:https://www.cnblogs.com/liyasen/p/6611079.html