溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

在Linux二進(jìn)制安裝keepalived

發(fā)布時(shí)間:2020-07-28 22:35:11 來源:網(wǎng)絡(luò) 閱讀:574 作者:wx5c1cfd6e22842 欄目:系統(tǒng)運(yùn)維

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

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

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

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

首先需要去Keepalived的官網(wǎng)下載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

出現(xiàn)報(bào)錯(cuò):缺少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

再次報(bào)錯(cuò),說這個(gè)版本不支持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

將啟動(dòng)文件放到/etc/init.d下,可以使用service來啟動(dòng)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

啟動(dòng)失敗,查看報(bào)錯(cuò),這里提示 Unable to find configuration file /etc/keepalived/keepalived.conf,沒有發(fā)現(xiàn)我們的配置文件的地址,這里是因?yàn)槲覀冊(cè)诎惭b的時(shí)候指定了自己的安裝路徑

[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二進(jìn)制安裝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啟動(dòng)成功

[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進(jìn)行啟動(dòng)停止

[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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI