keepalived安裝詳細步驟說明

主機安裝keepalived

用root執行

keepalived暫時找不到不用root安裝或者運行方法,可能須要做網絡層的虛擬ip映射,須要權限。html

下載安裝包

\[nginx@pay-test ~\]$ sftp myc@1.13.22.112 sftp> get keepalived-1.3.9.tar.gz \[nginx@pay-test ~\]$ gzip -d keepalived-1.3.9.tar.gz

\[root@pay-test nginx\]# cp keepalived-1.3.9.tar /usr/local/src/ \[root@pay-test nginx\]# cd /usr/local/src/ \[root@pay-test src\]# tar xvf keepalived-1.3.9.tar

編譯

\[root@pay-test keepalived-1.3.9\]# pwd /usr/local/src/keepalived-1.3.9 \[root@pay-test keepalived-1.3.9\]# ./configure --prefix=/usr/local/keepalived

[root@pay-test keepalived-1.3.9]# make && make install 要注意,最好分開執行,剛剛連一塊兒執行失敗沒有生成/usr/local/keepalived目錄,也沒有任何提示。nginx

拷貝文件

mkdir /etc/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/src/keepalived-1.3.9/keepalived/etc/init.d/keepalived /etc/init.d/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/ ln -s /usr/local/keepalived/sbin/keepalived /sbin/ (這個其實不須要,公司機器已自動拷貝) ln前要先刪除,若是有的話。

配置文件

[nginx@pay-test keepalived]$ pwd /etc/keepalived [nginx@pay-test keepalived]$ vi keepalived.confbash

! Configuration File for keepalived

global_defs {
   router_id pay-test
}

vrrp_script chk_nginx { 
    script "/etc/keepalived/check_nginx_pid.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 110
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        1.13.22.118
    }

} # 很重要

119主機:網絡

! Configuration File for keepalived

global_defs {
   router_id pay-test2
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx_pid.sh"
    interval 5
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP # 很重要
    interface eth0
    virtual_router_id 51
    priority 110
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        1.13.22.118
    }

}

[nginx@pay-test keepalived]$ vi check_nginx_pid.sh測試

#!/bin/bash
LOG_DIR="/etc/keepalived"  
echo $(date "+%Y-%m-%d %H:%M:%S") "check nginx status" >> $LOG_DIR/log_nginx.log 
A=`ps -C nginx --no-header |wc -l`        
if [ $A -eq 0 ];then                            
      /usr/sbin/nginx                #重啓nginx
      echo  $(date "+%Y-%m-%d %H:%M:%S") "restarting nginx..." >> $LOG_DIR/restart.log
      sleep 2
      if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    #nginx重啓失敗,則停掉keepalived服務,進行VIP轉移
              killall keepalived                    
              echo $(date "+%Y-%m-%d %H:%M:%S") "killing keepalived..." >> $LOG_DIR/restart.log
      fi
fi

[nginx@pay-test keepalived]$ chmod +x check_nginx_pid.shui

測試: nginx -s stop 自動生成日誌:restart.log,ps一下nginx,發現重啓成功。.net

check腳本增長: sleep 2rest

啓動

失敗: [root@pay-test ~]# service keepalived start Reloading systemd: [ OK ] Starting keepalived (via systemctl): Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details. [FAILED]日誌

查看日誌: systemctl status keepalived.servicecode

Starting keepalived: /bin/bash: keepalived: command not found 解決: /usr/sbin/裏必須有keepalived(make install沒執行,沒生成/usr/local/keepalived致使)

成功: [root@pay-test ~]# service keepalived start Starting keepalived (via systemctl): [ OK ]

\[nginx@pay-test keepalived\]$ ps -ef|grep keepalived root 27566 1 0 17:21 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D root 27567 27566 0 17:21 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D root 27568 27566 0 17:21 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D nginx 37088 21123 0 17:47 pts/1 00:00:00 grep --color=auto keepalived

中止

\[root@zhifu-db1 keepalived\]# service keepalived stop Stopping keepalived (via systemctl): \[ OK \]

或者: killall keepalived

修改日誌路徑

Keepalived默認的日誌文件保存在/var/log/messages 修改/etc/sysconfig/keepalived爲:KEEPALIVED_OPTIONS="-D -d -S 0"

[nginx@pay-test sysconfig]$ vi keepalived
#KEEPALIVED_OPTIONS="-D"
KEEPALIVED_OPTIONS="-D -S 0"

[root@pay-test ~]# vi /etc/rsyslog.conf

# Save boot messages also to boot.log

local7.* /var/log/boot.log local0.* /var/log/keepalived.log

重啓rsyslog:

\[root@pay-test ~\]# /etc/init.d/rsyslog restart -bash: /etc/init.d/rsyslog: No such file or directory \[root@pay-test /\]# /etc/sysconfig/rsyslog restart -bash: /etc/sysconfig/rsyslog: Permission denied \[root@pay-test rsyslog\]# /etc/rsyslog.conf restart -bash: /etc/rsyslog.conf: Permission denied \[root@pay-test rsyslog\]# systemctl restart rsyslog \[root@pay-test rsyslog\]#

重啓keepalived:

\[root@pay-test rsyslog\]# /etc/init.d/keepalived restart Restarting keepalived (via systemctl): \[ OK \] \[root@pay-test rsyslog\]# tail -f /var/log/keepalived.log tail: cannot open ‘/var/log/keepalived.log’ for reading: No such file or directory tail: no files remaining

重啓日誌:

Jun 27 09:53:32 pay-test systemd: Stopping LVS and VRRP High Availability Monitor… Jun 27 09:53:32 pay-test Keepalived\[123109\]: Stopping Jun 27 09:53:32 pay-test Keepalived_healthcheckers\[123110\]: Stopped Jun 27 09:53:33 pay-test Keepalived_vrrp\[123111\]: Stopped Jun 27 09:53:33 pay-test Keepalived\[123109\]: Stopped Keepalived v1.3.9 (10/21,2017)

Jun 27 09:53:33 pay-test systemd: Starting LVS and VRRP High Availability Monitor… Jun 27 09:53:33 pay-test Keepalived\[123755\]: Starting Keepalived v1.3.9 (10/21,2017) Jun 27 09:53:33 pay-test Keepalived\[123755\]: Opening file '/etc/keepalived/keepalived.conf'. Jun 27 09:53:33 pay-test systemd: PID file /var/run/keepalived.pid not readable (yet?) after start. Jun 27 09:53:33 pay-test Keepalived\[123756\]: Starting Healthcheck child process, pid=123757 Jun 27 09:53:33 pay-test Keepalived_healthcheckers\[123757\]: Opening file '/etc/keepalived/keepalived.conf'. Jun 27 09:53:33 pay-test Keepalived\[123756\]: Starting VRRP child process, pid=123758 Jun 27 09:53:33 pay-test Keepalived_vrrp\[123758\]: Registering Kernel netlink reflector Jun 27 09:53:33 pay-test Keepalived_vrrp\[123758\]: Registering Kernel netlink command channel Jun 27 09:53:33 pay-test Keepalived_vrrp\[123758\]: Registering gratuitous ARP shared channel Jun 27 09:53:33 pay-test Keepalived_vrrp\[123758\]: Opening file '/etc/keepalived/keepalived.conf'. Jun 27 09:53:33 pay-test Keepalived_vrrp\[123758\]: Using LinkWatch kernel netlink reflector… Jun 27 09:53:33 pay-test systemd: Started LVS and VRRP High Availability Monitor.

沒有定時執行腳本: keepalived.conf裏少了一個}

參考: http://wosyingjun.iteye.com/blog/2313147

keepalived: command not found 錯誤參考: https://blog.csdn.net/lxn19860201/article/details/51422489

日誌參考: https://www.cnblogs.com/zzzhfo/p/6070575.html

沒執行腳本參考: https://bbs.csdn.net/topics/391844227

配置詳細解釋: http://www.javashuo.com/article/p-gfuxuduw-m.html

相關文章
相關標籤/搜索