一毅待、實(shí)驗(yàn)背景
keepalived作為一款高可用軟件拂募,經(jīng)常和LVS等負(fù)載均衡配合使用。
keepalived可以實(shí)現(xiàn)vip的自動(dòng)漂移狞山,并且會(huì)檢測(cè)后端服務(wù)器的狀態(tài),當(dāng)主服務(wù)器工作出現(xiàn)故障時(shí)會(huì)將其剔除叉寂,并將備用服務(wù)器上線萍启;當(dāng)主服務(wù)器修復(fù)后,又會(huì)自動(dòng)將備用服務(wù)器下線,讓主服務(wù)器上線勘纯。這些工作全部自動(dòng)完成局服,不需要人工干涉,需要人工做的只是修復(fù)故障服務(wù)器驳遵。
聯(lián)網(wǎng)yum安裝的keepalived版本為1.x淫奔,keepalived最新版本已經(jīng)到了2.x,那么我們?nèi)绾尉幾g安裝最新版本的keepalived?
二、實(shí)驗(yàn)環(huán)境
操作系統(tǒng): CentOS7.5
ip: 192.168.1.104
測(cè)試用vip: 192.168.1.110
三堤结、編譯工具及編譯依賴安裝
# yum? -y install epel-release? vim? wget?
# yum -y install? psmisc
# yum -y install libnl libnl-devel libnfnetlink-devel popt-devel? gcc make? openssl? openssl-devel
四搏讶、keepalived軟件包下載
官網(wǎng):http://www.keepalived.org/download.html
# wget http://www.keepalived.org/software/keepalived-2.0.12.tar.gz
五、編譯keepalived
# tar -zxf keepalived-2.0.12.tar.gz
# cd? keepalived-2.0.12
# ./configure? --help
方式一霍殴、用 --with-init=SYSV 的方式編譯
# ./configure --prefix=/usr/local/keepalived? --with-init=SYSV
# make && make install
# ll /usr/local/keepalived/
相關(guān)配置文件模板
#? mkdir /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf? /etc/keepalived/
# cp /usr/local/keepalived/etc/sysconfig/keepalived? ? /etc/sysconfig/
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived? /etc/init.d/
# chmod 700 /etc/init.d/keepalived
# ln -s /usr/local/keepalived/bin/genhash /usr/bin/
# ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
# vim /etc/keepalived/keepalived.conf
###############################
! Configuration File for keepalived
global_defs {
? router_id LVS_DEVEL
}
vrrp_script check_sshd {
? ? ? ? script "killall -0 sshd"
? ? ? ? interval 2
? ? ? ? fall 3
? ? ? ? rise 3
}
vrrp_instance VI_1 {
? ? state MASTER
? ? interface ens33
? ? virtual_router_id 120
? ? priority 100
? ? advert_int 1
? ? nopreempt
? ? authentication {
? ? ? ? auth_type PASS
? ? ? ? auth_pass 1111
? ? }
? ? track_script {
? ? ? ? check_sshd
? ? }
? ? virtual_ipaddress {
? ? ? ? 192.168.1.110
? ? }
}
##################################
# chkconfig --add keepalived
# chkconfig keepalived on
# service keepalived start
# service keepalived status
方式二媒惕、用--with-init=systemd 的方式編譯
# ./configure? --prefix=/usr/local/keepalived? --with-init=systemd
# make && make install
# ll /usr/local/keepalived/
相關(guān)配置文件模板
#? mkdir /etc/keepalived
編譯后,生成的keepalived.service文件路徑
#? ll? /usr/lib/systemd/system/keepalived.service
###########################################
如果不用編譯后的keepalived.service默認(rèn)文件来庭,可以定義為? /etc/systemd/system/keepalived.service
# cp keepalived-2.0.12/keepalived/keepalived.service? /etc/systemd/system/
# cat /etc/systemd/system/keepalived.service
實(shí)際上 keepalived.service 放在? /etc/systemd/system/ 優(yōu)先級(jí)會(huì)高于放在 /usr/lib/systemd/system/ 下
#############################################
# cat /usr/lib/systemd/system/keepalived.service
# cp? /usr/local/keepalived/etc/keepalived/keepalived.conf? /etc/keepalived/
# vim? /etc/keepalived/keepalived.conf
###############################
! Configuration File for keepalived
global_defs {
? router_id LVS_DEVEL
}
vrrp_script check_sshd {
? ? ? ? script "killall -0 sshd"
? ? ? ? interval 2
? ? ? ? fall 3
? ? ? ? rise 3
}
vrrp_instance VI_1 {
? ? state MASTER
? ? interface ens33
? ? virtual_router_id 120
? ? priority 100
? ? advert_int 1
? ? nopreempt
? ? authentication {
? ? ? ? auth_type PASS
? ? ? ? auth_pass 1111
? ? }
? ? track_script {
? ? ? ? check_sshd
? ? }
? ? virtual_ipaddress {
? ? ? ? 192.168.1.110
? ? }
}
##################################
# systemctl daemon-reload
# systemctl? start keepalived
# systemctl enable keepalived
# systemctl? status? keepalived
用--with-init=systemd 的方式編譯妒蔚,可以不用systemd的keepalived.service,而用SYSV的 keepalived啟動(dòng)腳本啟動(dòng)月弛,反之亦然肴盏,只是需要自己寫(xiě)keepalived.service文件了!
#? mkdir /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf? /etc/keepalived/
# cp /usr/local/keepalived/etc/sysconfig/keepalived? ? /etc/sysconfig/
# cp? keepalived-2.0.12/keepalived/etc/init.d/keepalived? /etc/init.d/
# chmod 700? /etc/init.d/keepalived
# ln -s /usr/local/keepalived/bin/genhash /usr/bin/
# ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
# chkconfig --add keepalived
# chkconfig keepalived on
# service keepalived start
# service keepalived status
六帽衙、參考
Keepalived 2.0.6編譯安裝
http://blog.51cto.com/lajifeiwomoshu/2152667
Keepalived 2.07編譯安裝與配置教程
http://www.linuxe.cn/post-266.html
Keepalived系列一:安裝Keepalived
https://www.cnblogs.com/xiaoit/p/4499703.html
kill -0 pid是做什么用的菜皂?
http://www.zhixing123.cn/stdq/57281.html
http://blog.51cto.com/dzm911/1940295
https://bbs.csdn.net/topics/391890814
https://stackoverflow.com/questions/11012527/what-does-kill-0-pid-in-a-shell-script-do
CentOS7精簡(jiǎn)安裝后使用發(fā)現(xiàn)沒(méi)有killall命令
https://my.oschina.net/zhangxu0512/blog/383297
How To Set Up Highly Available Web Servers with Keepalived and Floating IPs on Ubuntu 16.04
https://vexxhost.com/resources/tutorials/highly-available-web-servers-keepalived-floating-ips-ubuntu-16-04