一、实验背景
keepalived作为一款高可用软件,经常和LVS等负载均衡配合使用。
keepalived可以实现vip的自动漂移,并且会检测后端服务器的状态,当主服务器工作出现故障时会将其剔除,并将备用服务器上线;当主服务器修复后,又会自动将备用服务器下线,让主服务器上线。这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障服务器。
联网yum安装的keepalived版本为1.x,keepalived最新版本已经到了2.x,那么我们如何编译安装最新版本的keepalived?
二、实验环境
操作系统: CentOS7.5
ip: 192.168.1.104
测试用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软件包下载
官网: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/
相关配置文件模板
# 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/
相关配置文件模板
# mkdir /etc/keepalived
编译后,生成的keepalived.service文件路径
# ll /usr/lib/systemd/system/keepalived.service
###########################################
如果不用编译后的keepalived.service默认文件,可以定义为 /etc/systemd/system/keepalived.service
# cp keepalived-2.0.12/keepalived/keepalived.service /etc/systemd/system/
# cat /etc/systemd/system/keepalived.service
实际上 keepalived.service 放在 /etc/systemd/system/ 优先级会高于放在 /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启动脚本启动,反之亦然,只是需要自己写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精简安装后使用发现没有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