keepalived搭建zabbix server雙機高可用

零. 準備工做

最近公司zabbix服務器升級,鑑於之前單機負載壓力較大,宕機以後系統癱瘓,趁機改爲雙機高可用架構。
如下是用keepalived作HA的搭建過程,以備參考!vim

兩臺主機,三個ip,兩臺主機配置zabbix和keepalived用於高可用架設。服務器

IP地址 主機名 備註
192.168.3.141 zabbix_master zabbix/keepalived
192.168.3.141 zabbix_slaver zabbix/keepalived
192.168.3.144 VIP ---

一. keepalived安裝

1. Installation

centOS 7下安裝很簡單,直接yum便可。架構

$ su root 
$ yum install keepalived
$ /usr/sbin/keepalived -D
$ pstree |grep keepalived
        |-keepalived---2*[keepalived]

能夠看到實際啓動了三個進程。oop

2. Base Configuration

製做開機啓動ui

pgrep keepalived |xargs kill -15
 #從新加載
systemctl daemon-reload  
 #設置開機自動啓動
systemctl enable keepalived.service
 #取消開機自動啓動  
systemctl disable keepalived.service 
 #啓動
systemctl start keepalived.service
 #中止
systemctl stop keepalived.service
 #狀態檢查
systemctl status keepalived.service
3. Advance Configuration
su root
chown zabbix:zabbix /usr/sbin/keepalived
chown zabbix:zabbix /lib/systemd/system/keepalived.service
chown zabbix:zabbix /etc/keepalived/keepalived.conf
su zabbix 
vim /lib/systemd/system/keepalived.service
vim /etc/keepalived/keepalived.conf
chmod +x /home/zabbix/keepalived/src/zabbix_status_check2.sh

主要是keepalived.conf和zabbix_status_check2.sh兩個腳本,網上資料不少,這裏不細講。scala

二. 雙機啓動

1. 主機啓動keepalived
Mar 13 17:27:57 vm1184 Keepalived_vrrp[5026]: VRRP_Script(zabbix_status_check) succeeded
Mar 13 17:28:00 vm1184 Keepalived_vrrp[5026]: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 13 17:28:01 vm1184 Keepalived_vrrp[5026]: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 13 17:28:01 vm1184 Keepalived_vrrp[5026]: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 13 17:28:01 vm1184 Keepalived_vrrp[5026]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens160 for 192.168.3.144
Mar 13 17:28:01 vm1184 Keepalived_healthcheckers[5025]: Netlink reflector reports IP 192.168.3.144 added
Mar 13 17:28:01 vm1184 avahi-daemon[637]: Registering new address record for 192.168.3.144 on ens160.IPv4.
Mar 13 17:28:06 vm1184 Keepalived_vrrp[5026]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens160 for 192.168.3.144
2. 主機地址,VIP已經綁定到網卡
$  ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:94:eb:ad brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.141/32 brd 192.168.3.141 scope global ens160
       valid_lft forever preferred_lft forever
    inet 192.168.3.144/24 scope global ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::ff15:1cc9:5bd0:b06e/64 scope link
       valid_lft forever preferred_lft forever
3. 備機日誌,keepalived已啓動。
$  tail -f  /var/log/messages
Mar 14 09:24:02 vm1185 Keepalived_healthcheckers[7588]: Registering Kernel netlink command channel
Mar 14 09:24:02 vm1185 Keepalived_healthcheckers[7588]: Opening file '/etc/keepalived/keepalived.conf'.
Mar 14 09:24:02 vm1185 Keepalived_healthcheckers[7588]: Configuration is using : 8087 Bytes
Mar 14 09:24:02 vm1185 Keepalived_healthcheckers[7588]: Using LinkWatch kernel netlink reflector...
4. 備機ip地址,VIP實際並無綁到網卡上。
$  ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:94:88:c9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.142/32 brd 192.168.3.142 scope global ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::e00c:f722:cd30:ad8b/64 scope link
       valid_lft forever preferred_lft forever

三. 主備切換

1. 殺掉主機上的keepalived以後,備機如今自動切換成主機了。
Mar 14 09:29:04 vm1185 Keepalived_vrrp[7589]: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 14 09:29:05 vm1185 Keepalived_vrrp[7589]: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 14 09:29:05 vm1185 Keepalived_vrrp[7589]: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 14 09:29:05 vm1185 Keepalived_vrrp[7589]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens160 for 192.168.3.144
Mar 14 09:29:05 vm1185 Keepalived_healthcheckers[7588]: Netlink reflector reports IP 192.168.3.144 added
Mar 14 09:29:05 vm1185 avahi-daemon[674]: Registering new address record for 192.168.3.144 on ens160.IPv4.
Mar 14 09:29:10 vm1185 Keepalived_vrrp[7589]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens160 for 192.168.3.144
2. 現主機(原備機)如今的網卡狀況,VIP已經綁定上去了。
$  ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:94:88:c9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.142/32 brd 192.168.3.142 scope global ens160
       valid_lft forever preferred_lft forever
    inet 192.168.3.144/24 scope global ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::e00c:f722:cd30:ad8b/64 scope link
       valid_lft forever preferred_lft forever
3. 再啓動原主機上的keepalived,原主機變備機,VIP綁定被清除了。
$  tail -f /var/log/messages
Mar 14 09:30:12 vm1184 Keepalived_vrrp[11857]: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 14 09:30:12 vm1184 Keepalived_vrrp[11857]: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 14 09:30:12 vm1184 Keepalived_vrrp[11857]: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens160 for 10.18      .8.144
Mar 14 09:30:12 vm1184 avahi-daemon[637]: Registering new address record for 192.168.3.144 on ens160.IPv4.
Mar 14 09:30:12 vm1184 Keepalived_healthcheckers[11856]: Netlink reflector reports IP 192.168.3.144 added
Mar 14 09:30:12 vm1184 Keepalived_vrrp[11857]: VRRP_Instance(VI_1) Received higher prio advert
Mar 14 09:30:12 vm1184 Keepalived_vrrp[11857]: VRRP_Instance(VI_1) Entering BACKUP STATE
Mar 14 09:30:12 vm1184 Keepalived_vrrp[11857]: VRRP_Instance(VI_1) removing protocol VIPs.
Mar 14 09:30:12 vm1184 Keepalived_healthcheckers[11856]: Netlink reflector reports IP 192.168.3.144 removed
Mar 14 09:30:12 vm1184 avahi-daemon[637]: Withdrawing address record for 192.168.3.144 on ens160.
4. 原主機變備機,VIP綁定確實被清除了。
$  ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:94:eb:ad brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.141/32 brd 192.168.3.141 scope global ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::ff15:1cc9:5bd0:b06e/64 scope link
       valid_lft forever preferred_lft forever

四. zabbix_server 宕機實驗

# 原server啓動方式
$  ps -ef |grep zabbix_server

zabbix   31668 31643  0 Mar13 ?        00:00:11 ./sbin/zabbix_server: escalator #1 [processed 0 escalations in 0.001148 sec, idle 3 sec]
zabbix   31669 31643  0 Mar13 ?        00:00:03 ./sbin/zabbix_server: proxy poller #1 [exchanged data with 0 proxies in 0.000066 sec, idle 5 sec]
zabbix   31670 31643  0 Mar13 ?        00:00:09 ./sbin/zabbix_server: self-monitoring [processed data in 0.000046 sec, idle 1 sec]
zabbix   31671 31643  0 Mar13 ?        00:00:03 ./sbin/zabbix_server: task manager [processed 0 task(s) in 0.000289 sec, idle 5 sec]

  # 殺掉進程,操做宕機
$  pgrep zabbix_server |xargs kill -15

  # 檢查
$  ps -ef |grep zabbix_server

zabbix   14053 14028  0 09:59 ?        00:00:00 /home/zabbix/zabbix/sbin/zabbix_server: history syncer #4 [synced 0 items in 0.000043 sec, idle 1 sec]
zabbix   14054 14028  0 09:59 ?        00:00:00 /home/zabbix/zabbix/sbin/zabbix_server: escalator #1 [processed 0 escalations in 0.001202 sec, idle 3 sec]
zabbix   14055 14028  0 09:59 ?        00:00:00 /home/zabbix/zabbix/sbin/zabbix_server: proxy poller #1 [exchanged data with 0 proxies in 0.000057 sec, idle 5 sec]
zabbix   14056 14028  0 09:59 ?        00:00:00 /home/zabbix/zabbix/sbin/zabbix_server: self-monitoring [processed data in 0.000040 sec, idle 1 sec]
zabbix   14057 14028  0 09:59 ?        00:00:00 /home/zabbix/zabbix/sbin/zabbix_server: task manager [processed 0 task(s) in 0.000433 sec, idle 5 sec]

說明keepalived的健康檢查腳本確實執行了,檢查不到zabbix_server進程的狀況下會自動重啓。日誌

相關文章
相關標籤/搜索