keepalived安裝與配置,組建高可用服務器
一、準備環境
linux系統:CentOS7
keepalived版本:keepalived-1.3.5.tar.gz
keepalived下載地址:http://www.keepalived.org/download.html
在CentOS7系統中安裝keepalived(主、從服務器都要安裝):
- 關閉CentOS7防火墻
- 關閉selinux策略
- 下載keepalived依賴包gcc、openssl
yum install gcc
yum install openssl-devel
二、安裝和配置keepalived(只對keepalived主程序配置,LVS則不進行配置)
- 將源碼keepalived-1.3.5.tar.gz文件解壓縮
命令:[[email protected] keepalived]# tar zxvf keepalived-1.3.5.tar.gz
2.進入keepalived-1.3.5文件夾,配置keepalived源文件並指定路徑。
創建指定路徑:(此路徑隨意指定)
[[email protected] keepalived-1.3.5]# mkdir /usr/local/keepalived
配置源文件:
[[email protected] keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived
(出現下圖表示配置源文件成功)
編譯keepalived:
[[email protected] keepalived-1.3.5]# make
安裝keepalived:
[[email protected] keepalived-1.3.5]# make install
3.keepalived啟動讀取配置文件的路徑為/etc/keepalived
路徑創建命令:[[email protected] keepalived-1.3.5]# mkdir /etc/keepalived
將/usr/local/keepalived/etc/keepalived下的keepalived.conf復制到/etc/keepalived路徑下
復制命令:[[email protected] keepalived-1.3.5]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
4.編輯/etc/keepalived/keepalived.conf文件
編輯命令:[[email protected] keepalived-1.3.5]# vi /etc/keepalived/keepalived.conf
主服務器具體配置如下圖:
5.經過上述步驟主服務器配置已完成,從服務器只需更改keepalived.conf配置文件中的兩處即可其余與主服務器的配置信息一樣:
state處改為 BACKUP
priority處改為 50
6.啟動keepalived服務:systemctl start keepalived.service
重啟keepalived服務:systemctl restart keepalived.service
停止keepalived服務:systemctl stop keepalived.service
設置為開機自啟動:systemctl enable keepalived.service
取消開機自啟動:systemctl disable keepalived.service
查看錯誤日誌:cat /var/log/messages
如何檢查keepalived正常運行:
1) 查看keepalived三個進程是否都啟動,命令:ps aux | grep keepalived
Keepalived 正常運行時,共啟動 3 個進程,其中一個進程是父進程,負責監控其子進程;一個是 vrrp 子進程;另外一個是 checkers 子進程。
2) 查看浮動ip是否綁定到網卡(下圖中IP已綁定到網卡,本機ip為192.168.1.9),命令:ip a 或 ip addr s
3) 在keepalived服務所在局域網中,ping浮動ip查看是否可以ping通
至此keepalived安裝配置已完成。
但有一個問題,正常情況下A(master)主服務器運行時浮動ip(vip)綁定在A服務器的網卡上,當A服務器宕機時,B(backup)從服務器自動接替工作浮動ip綁定到B服務器網卡上,若修復好A服務器從新上線,A服務器會搶占浮動ip,強制重新綁定到A服務器網卡上。
如何避免搶占浮動ip,A宕機時B接替工作,當A恢復上線時自動當從機,B繼續工作?
修改A(master)主服務的/etc/keepalived/keepalived.conf文件中state的值為BACKUP,在參數中添加nopreempt命令,B服務器的配置文件不改變。
【這個配置只能設置在state為BACKUP的主機上,而且這個主機的priority必須比另外一臺高】
keepalived安裝與配置,組建高可用服務器