keepalived 1.3.5常見配置以及常見問題解決

你好我是李白發表於2020-05-07

 

1. keepalived配置及說明

1.1 安裝

 [root@mysql01 keepalived] tar -zxvf keepalived-1.3.5.tar.gz

 [root@mysql01 keepalived] cd keepalived-1.3.5/

 [root@mysql01 keepalived] ./configure --help|grep prefix

 [root@mysql01 keepalived] ./configure --prefix=/usr/local/keepalived

  make && make install

 [root@mysql01 keepalived] cd /usr/local/keepalived

 [root@mysql02 keepalived]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

 [root@mysql01 keepalived]# cp etc/sysconfig/keepalived /etc/sysconfig/

如果是Linux 6 還需要複製下面檔案:

 [root@mysql01 keepalived] cp /tmp/keepalived-1.3.2/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/

 

1.2 修改keepalived日誌輸出

vi /usr/lib/systemd/system/keepalived.service

# 確認下面兩個變數指向正確的可執行keepalived

EnvironmentFile

ExecStart

 

vi /etc/sysconfig/keepalived

修改keepalived 日誌輸出裝置為0 號裝置,並修改系統日誌程式0 號輸出裝置輸出檔案,以及新增dump 配置檔案資料。

vi /etc/keepalived/keepalived.conf

KEEPALIVED_OPTIONS="-D -d -S 0"

# 修改keepalived 使用0 號日誌輸出裝置,並單獨輸出日誌到指定檔案。

vi /etc/rsyslog.conf

local0.*                /usr/local/keepalived/log/keepalived.log

1.3 修改keepalived配置檔案

# 編輯keepalived 配置檔案,配置vip 以及服務監測,通知指令碼等模組。

vi /etc/keepalived/keepalived.conf

vrrp_script check_run {

    script "/etc/keepalived/scripts/keepalived_check_mysql.sh"

    interval 30

    weight 0

}

 

vrrp_instance VI_1 {

    state BACKUP

    nopreempt

    interface team0           # 網路卡,根據實際情況填寫

    virtual_router_id 88        # 主備機器id 必須相同,不同vrrp_instance 需要不同。

    priority 101

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.204.88     # 虛擬IP

    }

    track_script {

        check_run

    }

         notify_master /etc/keepalived/scripts/notify_master.sh

  }

1.4 keepalived.conf說明

l   Notify  scripts

that  are  run when a vrrp instance or vrrp group

changes state, or a virtual server quorum changes between up and down.

# global script_user and script_group to MASTER transition

notify_master /path/to_master.sh [username [groupname]]

 

l   vrrp tracking scripts

that will cause vrrp instances to go down  it

they exit a non-zero exist status, or if a weight is specified will add

or subtract the weight to/from the priority of that vrrp instance.

 

l   nopreempt

A higher priority VRRP instance will normally preempt a lower priority instance

when it comes online.  "nopreempt" stops the higher priority machine taking

over the master role, and allows the lower priority machine to remain as

master.

NOTE: For this to work, the initial state must not be MASTER.

1.5 啟動keepalived

啟用keepalived服務,啟動keepalived服務:

systemctl enable keepalived

systemctl start keepalived

2. 問題

2.1 not a regular non-executable file

l   現象

[root@mysql01 ~]# systemctl status keepalived.service
May  7 17:12:51 mysql01 Keepalived[5467]: Configuration file '/etc/keepalived/keepalived.conf' is not a regular non-executable file

l   原因

/etc/keepalived/keepalived.conf檔案許可權不是664。

l   解決

修改/etc/keepalived/keepalived.conf許可權為664即可。

2.2 Cannot find an IP address to use for interface

l   現象

[root@mysql01 ~]# systemctl status keepalived.service
May 07 17:41:14 mysql01 Keepalived_vrrp[9448]: (VI_1): Cannot find an IP address to use for interface enp0s9

l   原因

/etc/keepalived/keepalived.conf配置檔案中interface模組,網路卡上沒有IP地址,

l   解決

可以透過新增臨時IP 來解決,或者在keepalived 版本2.0.5 之後新增dynamic_interfaces

[root@mysql01 ~]# ifconfig eth1 172.16.20.101/16 up
eth1 配置IP ,因為如果介面 沒有IP keepalived 將以FAULT 狀態啟動,keepalived 2.0.5 以上版本, 只需將dynamic_interfaces 新增到global_defs 即可

2.3 NetworkManager 導致keepalived無法啟動

l   現象

[root@mysql01 ~]# tail -f /var/log/messages

May  7 11:07:15 mysql01 Keepalived[8755]: daemon is already running

May  7 11:07:15 mysql01 systemd: PID file /usr/local/var/run/keepalived.pid not readable (yet?) after start.

May  7 11:08:45 mysql01 systemd: keepalived.service start operation timed out. Terminating.

May  7 11:08:45 mysql01 systemd: Failed to start LVS and VRRP High Availability Monitor.

May  7 11:08:45 mysql01 systemd: Unit keepalived.service entered failed state.

May  7 11:08:45 mysql01 systemd: keepalived.service failed.

l   原因

NetworkManager 自動管理網路,keepalived 使用虛擬IP 無法繫結到指定介面,日誌中報錯又無法定位原因,提前關閉NetworkManager ,手工設定網路卡ip 地址即可。

l   解決

[root@mysql01 ~]# systemctl disable NetworkManager

[root@mysql01 ~]# systemctl stop NetworkManager

2.4 VRRP_Instance(VI_1) Now in FAULT state

l   現象

[mysql@mysql01 65mysql]$ sudo systemctl status keepalived

 

May 07 19:06:08 mysql01 Keepalived_vrrp[16144]: VRRP_Instance(VI_1) Entering BACKUP STATE

May 07 19:06:08 mysql01 Keepalived_vrrp[16144]: VRRP sockpool: [ifindex(4), proto(112), unicast(0), fd(10,11)]

May 07 19:06:12 mysql01 Keepalived_vrrp[16144]: VRRP_Instance(VI_1) Now in FAULT state

l   原因

/etc/keepalived/keepalived.conf 配置檔案vrrp_script 模組指令碼未檢查,返回總是非0 錯誤值,導致keepalived 狀態一直無法完成初始化,無法繫結vip

l   解決

修正指令碼錯誤,重啟keepalived

2.5 failed because a timeout was exceeded

●現象

[mysql@mysql02 keepalived]$ sudo systemctl status keepalived

 

keepalived.service - LVS and VRRP High Availability Monitor

   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)

   Active: failed (Result: timeout) since Thu 2020-05-07 20:44:08 CST; 14min ago

  Process: 31073 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)

    Tasks: 6

   CGroup: /system.slice/keepalived.service

           ├─ 2687 /usr/local/keepalived/sbin/keepalived -D -d -S 0

           ├─ 2688 /bin/bash /etc/keepalived/scripts/keepalived_check_mysql.sh

           ├─ 2815 sleep 2

           ├─25921 /usr/local/keepalived/sbin/keepalived -D -d -S 0

           ├─25922 /usr/local/keepalived/sbin/keepalived -D -d -S 0

           └─25923 /usr/local/keepalived/sbin/keepalived -D -d -S 0

 

May 07 20:42:38 mysql02 systemd[1]: Starting LVS and VRRP High Availability Monitor...

May 07 20:42:38 mysql02 systemd[1]: PID file /var/run/keepalived.pid not readable (yet?) after start.

May 07 20:44:08 mysql02 systemd[1]: keepalived.service start operation timed out. Terminating.

May 07 20:44:08 mysql02 systemd[1]: Failed to start LVS and VRRP High Availability Monitor.

May 07 20:44:08 mysql02 systemd[1]: Unit keepalived.service entered failed state.

May 07 20:44:08 mysql02 systemd[1]: keepalived.service failed.

[mysql@mysql02 keepalived]$ sudo systemctl start keepalived

Job for keepalived.service failed because a timeout was exceeded. See "systemctl status keepalived.service" and "journalctl -xe" for details.

l   原因

原來keepalived 程式執行不正常,無法繼續,systemctl restart keepalived 已經無法正確重啟keepalived

l   解決

[mysql@mysql02 ~]$ ps -ef|grep keepalived

kill 掉所有keepalived 程式,重啟keepalived

[mysql@mysql02 ~]$ sudo systemctl restart keepalived.service

[mysql@mysql02 ~]$ sudo systemctl status keepalived.service


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31439444/viewspace-2690474/,如需轉載,請註明出處,否則將追究法律責任。

相關文章