實驗環境:centos7
節點1:10.15.192.21
節點2:10.15.192.22
vip地址:10.15.192.23
1、下載文件
cd /usr/local/src wget https://www.keepalived.org/software/keepalived-2.0.19.tar.gz # 官網地址 https://www.keepalived.org/download.html
2、解壓文件
tar xzf keepalived-2.0.19.tar.gz cd keepalived-2.0.19
3、安裝依賴
yum -y install libnl libnl-devel openssl-devel
4、初始化配置
cd keepalived-2.0.19/ ./configure --prefix=/usr/local/keepalived
5、編譯安裝
make && make install
6、配置
mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived/sbin/keepalived /etc/init.d/
節點1配置文件如下,注意兩個節點mcast_src_ip 和 priority 值
cat keepalived.conf ! Configuration File for keepalived global_defs { router_id hf-ayd-web-balance-01 script_user root enable_script_security } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 151 mcast_src_ip 10.15.192.21 priority 100 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.15.192.23 } track_script { chk_nginx } }
節點2配置文件如下
cat keepalived.conf ! Configuration File for keepalived global_defs { router_id hf-ayd-web-balance-02 script_user root enable_script_security } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 151 mcast_src_ip 10.15.192.22 priority 90 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.15.192.23 } track_script { chk_nginx } }
7、管理服務
systemctl start keepalived #啟動 systemctl restart keepalived #重啟 systemctl stop keepalived #關閉 systemctl status keepalived # 狀態
常見問題一:keepalived配置nopreempt參數無效解決方法
nopreempt #設置為不搶占 注:這個配置只能設置在backup主機上,而且這個主機優先級要比另外一台高 master不能設置nopreempt 解決方案是:不設置master,全部設置成backup,這樣大家都是backup,就都能添加nopreempt,即使原本成為master的LB壞掉重新修好之后也不會搶占master。 通常如果master服務死掉后backup會變成master,但是當master服務又好了的時候 master此時會搶占VIP,這樣就會發生兩次切換對業務繁忙的網站來說是不好的。所以我們要在配置文件加入 nopreempt 非搶占,但是這個參數只能用於state 為backup,故我們在用HA的時候最好master 和backup的state都設置成backup 讓其通過priority來競爭
常見問題二:編譯安裝keepalived時提示以下錯誤
make cd . && /bin/sh /data/keepalived-2.0.19/missing automake-1.15 --foreign Makefile /data/keepalived-2.0.19/missing: line 81: automake-1.15: command not found WARNING: 'automake-1.15' is missing on your system. You should only need it if you modified 'Makefile.am' or 'configure.ac' or m4 files included by 'configure.ac'. The 'automake' program is part of the GNU Automake package: <http://www.gnu.org/software/automake> It also requires GNU Autoconf, GNU m4 and Perl in order to run: <http://www.gnu.org/software/autoconf> <http://www.gnu.org/software/m4/> <http://www.perl.org/> make: *** [Makefile.in] Error 127
解決方法如下:
yum install automake -y autoreconf -ivf 再次執行make
編譯報錯問題安裝參考:https://blog.csdn.net/arackethis/article/details/42222905