溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在Linux中安裝和部署keepalived

發(fā)布時間:2020-05-27 13:56:19 來源:億速云 閱讀:854 作者:鴿子 欄目:系統(tǒng)運維

keepalived的部署
keepalived在很多高可用的集群都會用到,一般前端放置的會是nginx、ipvs、haproxy

比如我們在使用rabbitmq的時候做了這么一個集群,一般做了集群的話,那肯定就需要這么一個高可用的負載均衡器來實現流量的分發(fā),如果使用的是haproxy,比如一臺rebbitmq的節(jié)點突然宕機或者網卡失效,那么雖然RabbitMQ集群沒有任何故障,但是對于外界的客戶端來說所有的連接都會被斷開,結果將是災難性的。確保負載均衡服務的可靠性同樣顯得十分的重要。這里就引入Keepalived工具,它能夠通過自身健康檢查、資源接管功能做高可用(雙機熱備),實現故障轉移。

Keepalived采用VRRP(Virtual Router Redundancy Protocol,虛擬路由冗余協(xié)議),以軟件的形式實現服務器熱備功能。通常情況下是將兩臺Linux服務器組成一個熱備組(Master和Backup),同一時間熱備組內只有一臺主服務器Master提供服務,同時Master會虛擬出一個公用的虛擬IP地址,簡稱VIP。這個VIP只存在在Master上并對外提供服務。如果Keepalived檢測到Master宕機或者服務故障,備份服務器Backup會自動接管VIP稱為Master,Keepalived并將原Master從熱備組中移除。當原Master恢復后,會自動加入到熱備組,默認再搶占稱為Master,起到故障轉移的功能。

Keepalived工作在OSI模型中的第3層、第4層和第7層。
工作在第3層是指Keepalived會定期向熱備組中的服務器發(fā)送一個ICMP數據包來判斷某臺服務器是否故障,如果故障則將這臺服務器從熱備組移除。
工作在第4層是指Keepalived以TCP端口的狀態(tài)判斷服務器是否故障,比如檢測RabbitMQ的5672端口,如果故障則將這臺服務器從熱備組中移除。
工作在第7層是指Keepalived根據用戶設定的策略(通常是一個自定義的檢測腳本)判斷服務器上的程序是否正常運行,如果故障將這臺服務器從熱備組移除。

首先需要去Keepalived的官網下載Keepalived的安裝文件,目前最新的版本為:keepalived-2.0.20.tar.gz,下載地址為http://www.keepalived.org/download.html

可以找到最新版

[root@VM_0_8_centos ~]# mkdir keepalived
[root@VM_0_8_centos ~]# cd keepalived/
[root@VM_0_8_centos keepalived]# wget https://www.keepalived.org/software/keepalived-2.0.20.tar.gz
--2020-02-27 10:46:37--  https://www.keepalived.org/software/keepalived-2.0.20.tar.gz
Resolving www.keepalived.org (www.keepalived.org)... 37.59.63.157, 2001:41d0:8:7a9d::1
Connecting to www.keepalived.org (www.keepalived.org)|37.59.63.157|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1036063 (1012K) [application/x-gzip]
Saving to: ‘keepalived-2.0.20.tar.gz’

100%[==================================================================================================================================>] 1,036,063   13.7KB/s   in 1m 43s 

2020-02-27 10:48:37 (9.80 KB/s) - ‘keepalived-2.0.20.tar.gz’ saved [1036063/1036063]

解壓

[root@VM_0_8_centos keepalived]# ls
keepalived-2.0.20.tar.gz
[root@VM_0_8_centos keepalived]# tar xf keepalived-2.0.20.tar.gz 
[root@VM_0_8_centos keepalived]# cd keepalived-2.0.20/
[root@VM_0_8_centos keepalived-2.0.20]# ls
aclocal.m4  AUTHOR       build_setup  compile    configure.ac  COPYING  doc      INSTALL     keepalived          lib          Makefile.in  README.md  TODO
ar-lib      bin_install  ChangeLog    configure  CONTRIBUTORS  depcomp  genhash  install-sh  keepalived.spec.in  Makefile.am  missing      snap
[root@VM_0_8_centos keepalived-2.0.20]# ./configure --prefix=/opt/keepalived --with-init=SYSV 

checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether make supports nested variables... (cached) yes
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out

出現報錯:缺少openssl-devel

configure: error: 
  !!! OpenSSL is not properly installed on your system. !!!
  !!! Can not include OpenSSL headers files.            !!!
[root@VM_0_8_centos keepalived-2.0.20]# yum -y install openssl-devel
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
Resolving Dependencies
--> Running transaction check
---> Package openssl-devel.x86_64 1:1.0.2k-19.el7 will be installed

安裝后重新再執(zhí)行
[root@VM_0_8_centos keepalived-2.0.20]# ./configure --prefix=/opt/keepalived --with-init=SYSV

再次報錯,說這個版本不支持ipv6,需要安裝依賴包
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

[root@VM_0_8_centos keepalived-2.0.20]# yum -y install libnl libnl-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package libnl-1.1.4-3.el7.x86_64 already installed and latest version

安裝完依賴包再次執(zhí)行

[root@VM_0_8_centos keepalived-2.0.20]# ./configure --prefix=/opt/keepalived --with-init=SYSV 

編譯

[root@VM_0_8_centos keepalived-2.0.20]# make && make install

將啟動文件放到/etc/init.d下,可以使用service來啟動keepalived

[root@VM_0_8_centos keepalived-2.0.20]# cp /opt/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@VM_0_8_centos keepalived-2.0.20]# cp /opt/keepalived/etc/sysconfig/keepalived /etc/sysconfig
[root@VM_0_8_centos keepalived-2.0.20]# cp /opt/keepalived/sbin/keepalived /usr/sbin
[root@VM_0_8_centos keepalived-2.0.20]# chmod +x /etc/init.d/keepalived 
[root@VM_0_8_centos keepalived-2.0.20]# chkconfig --add keepalived
[root@VM_0_8_centos keepalived-2.0.20]# chkconfig keepalived on

[root@VM_0_8_centos keepalived-2.0.20]# cp /opt/keepalived/etc/keepalived/keepalived.conf /etc/keepalived.conf

啟動失敗,查看報錯,這里提示 Unable to find configuration file /etc/keepalived/keepalived.conf,沒有發(fā)現我們的配置文件的地址,這里是因為我們在安裝的時候指定了自己的安裝路徑

[root@VM_0_8_centos keepalived-2.0.20]# service keepalived start
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]
[root@VM_0_8_centos keepalived-2.0.20]# journalctl -xe

如何在Linux中安裝和部署keepalived

找出放置我們配置文件的地方,修改文件地址

[root@VM_0_8_centos ~]# find / -name keepalived.conf
/opt/keepalived/etc/keepalived/keepalived.conf
/etc/keepalived.conf
/root/keepalived/keepalived-2.0.20/keepalived/etc/keepalived/keepalived.conf

在最后一行添加我們配置文件的地址

[root@VM_0_8_centos ~]# vim /etc/sysconfig/keepalived 
[root@VM_0_8_centos ~]# tail -1 /etc/sysconfig/keepalived 
KEEPALIVED_OPTIONS="-f /etc/keepalived.conf -D -S 0"

使用systemctl啟動成功

[root@VM_0_8_centos ~]# systemctl restart keepalived
[root@VM_0_8_centos ~]# systemctl status keepalived
● keepalived.service - SYSV: Start and stop Keepalived
   Loaded: loaded (/etc/rc.d/init.d/keepalived; bad; vendor preset: disabled)
   Active: active (running) since Thu 2020-02-27 11:02:22 CST; 10s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 30129 ExecStart=/etc/rc.d/init.d/keepalived start (code=exited, status=0/SUCCESS)
 Main PID: 30136 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─30136 keepalived -f /etc/keepalived.conf -D -S 0
           ├─30138 keepalived -f /etc/keepalived.conf -D -S 0
           └─30139 keepalived -f /etc/keepalived.conf -D -S 0

Feb 27 11:02:31 VM_0_8_centos Keepalived_vrrp[30139]: Sending gratuitous ARP on eth0 for 192.168.200.18
Feb 27 11:02:31 VM_0_8_centos Keepalived_vrrp[30139]: Sending gratuitous ARP on eth0 for 192.168.200.16
Feb 27 11:02:31 VM_0_8_centos Keepalived_vrrp[30139]: Sending gratuitous ARP on eth0 for 192.168.200.17
Feb 27 11:02:31 VM_0_8_centos Keepalived_vrrp[30139]: Sending gratuitous ARP on eth0 for 192.168.200.18
Feb 27 11:02:31 VM_0_8_centos Keepalived_healthcheckers[30138]: HTTP_CHECK on service [192.168.200.3]:tcp:1358 failed after 3 retry.
Feb 27 11:02:31 VM_0_8_centos Keepalived_healthcheckers[30138]: Removing service [192.168.200.3]:tcp:1358 to VS [10.10.10.2]:tcp:1358
Feb 27 11:02:31 VM_0_8_centos Keepalived_healthcheckers[30138]: Lost quorum 1-0=1 > 0 for VS [10.10.10.2]:tcp:1358
Feb 27 11:02:31 VM_0_8_centos Keepalived_healthcheckers[30138]: Adding sorry server [192.168.200.200]:tcp:1358 to VS [10.10.10.2]:tcp:1358
Feb 27 11:02:31 VM_0_8_centos Keepalived_healthcheckers[30138]: Removing alive servers from the pool for VS [10.10.10.2]:tcp:1358
Feb 27 11:02:31 VM_0_8_centos Keepalived_healthcheckers[30138]: Remote SMTP server [192.168.200.1]:25 connected.

使用service進行啟動停止

[root@VM_0_8_centos ~]# service keepalived stop
Stopping keepalived (via systemctl):                       [  OK  ]
[root@VM_0_8_centos ~]# service keepalived start
Starting keepalived (via systemctl):                       [  OK  ]
[root@VM_0_8_centos ~]# service keepalived status
● keepalived.service - SYSV: Start and stop Keepalived
   Loaded: loaded (/etc/rc.d/init.d/keepalived; bad; vendor preset: disabled)
   Active: active (running) since Thu 2020-02-27 11:03:27 CST; 10s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 30394 ExecStop=/etc/rc.d/init.d/keepalived stop (code=exited, status=0/SUCCESS)
  Process: 30469 ExecStart=/etc/rc.d/init.d/keepalived start (code=exited, status=0/SUCCESS)
 Main PID: 30476 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─30476 keepalived -f /etc/keepalived.conf -D -S 0
           ├─30478 keepalived -f /etc/keepalived.conf -D -S 0
           └─30479 keepalived -f /etc/keepalived.conf -D -S 0

Feb 27 11:03:36 VM_0_8_centos Keepalived_vrrp[30479]: Sending gratuitous ARP on eth0 for 192.168.200.18
Feb 27 11:03:36 VM_0_8_centos Keepalived_vrrp[30479]: Sending gratuitous ARP on eth0 for 192.168.200.16
Feb 27 11:03:36 VM_0_8_centos Keepalived_vrrp[30479]: Sending gratuitous ARP on eth0 for 192.168.200.17
Feb 27 11:03:36 VM_0_8_centos Keepalived_vrrp[30479]: Sending gratuitous ARP on eth0 for 192.168.200.18
Feb 27 11:03:37 VM_0_8_centos Keepalived_healthcheckers[30478]: HTTP_CHECK on service [192.168.200.3]:tcp:1358 failed after 3 retry.
Feb 27 11:03:37 VM_0_8_centos Keepalived_healthcheckers[30478]: Removing service [192.168.200.3]:tcp:1358 to VS [10.10.10.2]:tcp:1358
Feb 27 11:03:37 VM_0_8_centos Keepalived_healthcheckers[30478]: Lost quorum 1-0=1 > 0 for VS [10.10.10.2]:tcp:1358
Feb 27 11:03:37 VM_0_8_centos Keepalived_healthcheckers[30478]: Adding sorry server [192.168.200.200]:tcp:1358 to VS [10.10.10.2]:tcp:1358
Feb 27 11:03:37 VM_0_8_centos Keepalived_healthcheckers[30478]: Removing alive servers from the pool for VS [10.10.10.2]:tcp:1358
Feb 27 11:03:37 VM_0_8_centos Keepalived_healthcheckers[30478]: Remote SMTP server [192.168.200.1]:25 connected.

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI