简介
Keepalived是为实现服务的高可用(HA)而存在的,它的作用是检测Web服务器的状态,当主服务器down机或出现故障时,Keepalived会将主服务器的IP切换到其他运行正常的服务器,以达到服务的稳定。如今很多软件都会和他搭配使用,比如LVS,Nginx,Redis等。
原理
Keepalived是以VRRP(Virtual Router Redundancy Protocol,虚拟路由冗余协议)协议为实现基础的,这个协议可以认为是实现了路由器高可用的协议,将多台提供相同功能的路由器组成一个路由器组,这里面有一个MASTER和多个BACKUP,MASTER上面有一个对外提供服务的VIP,MASTER会发组播,当BACKUP收不到VRRP包时就认为MASTERdown机,这时需要根据VRRP优先级来选举一个BACKUP为MASTER,这样就保证路由器的正常使用了。
下载与安装(文章使用的是CentOS7x86-64-1611和Keepalived1.3.4)
访问Keepalived官网(http://www.keepalived.org)下载软件包,并上传到服务器上。
或者直接用命令下载并解压:
[root@localhost ~]# wget http://124.205.69.132/files/90630000053A2BB4/www.keepalived.org/software/keepalived-1.3.4.tar.gz
[root@localhost ~]# tar zxvf keepalived-1.3.4.tar.gz
安装依赖插件:
[root@localhost ~]# yum install -y gcc openssl-devel popt-devel
编译安装:
[root@localhost ~]# cd keepalived-1.3.4
[root@localhost keepalived-1.3.4]# ./configure --prefix=/usr/local/keepalived
[root@localhost keepalived-1.3.4]# make && make install
配置:(后面部分会详细介绍,这里就用默认配置)
启动:
首先拷贝几个文件到CentOS7环境中:
[root@localhost ~]# cp keepalived-1.3.4/keepalived/etc/init.d/keepalived /etc/init.d/
[root@localhost ~]# mkdir /etc/keepalived
[root@localhost ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@localhost ~]# cp keepalived-1.3.4/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
执行启动服务:
-
[root@localhost ~]# service keepalived start
-
Starting keepalived (via systemctl): [ 确定 ]
查看服务启动情况,发现没有启动成功:
-
[root@localhost ~]# ps -aux |grep keepalived
-
root 16370 0.0 0.0 112664 936 pts/0 S+ 21:50 0:00 grep --color=auto keepalived
为什么呢?通过执行如下命令发现:
-
[root@localhost ~]# journalctl -xe
-
6月 13 21:49:09 localhost.localdomain Keepalived[16356]: Unable to resolve default script username 'keepalived_script' - ignoring
-
6月 13 21:49:09 localhost.localdomain Keepalived[16356]: Opening file '/etc/keepalived/keepalived.conf'.
-
6月 13 21:49:09 localhost.localdomain systemd[1]: PID file /var/run/keepalived.pid not readable (yet?) after start.
-
6月 13 21:49:09 localhost.localdomain Keepalived[16357]: Starting Healthcheck child process, pid=16358
-
6月 13 21:49:09 localhost.localdomain Keepalived[16357]: Starting VRRP child process, pid=16359
-
6月 13 21:49:09 localhost.localdomain systemd[1]: Started LVS and VRRP High Availability Monitor.
-
-- Subject: Unit keepalived.service has finished start-up
-
-- Defined-By: systemd
-
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-
--
-
-- Unit keepalived.service has finished starting up.
-
--
-
-- The start-up result is done.
-
6月 13 21:49:09 localhost.localdomain polkitd[593]: Unregistered Authentication Agent for unix-process:16350:328706 (system bus name :1.44, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) (disconnected from bus)
-
6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Initializing ipvs
-
6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Netlink reflector reports IP 192.168.0.11 added
-
6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Netlink reflector reports IP fe80::20c:29ff:fee3:8c45 added
-
6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Registering Kernel netlink reflector
-
6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Registering Kernel netlink command channel
-
6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Registering gratuitous ARP shared channel
-
6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Opening file '/etc/keepalived/keepalived.conf'.
-
6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Cant find interface eth0 for vrrp_instance VI_1 !!!
-
6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Default interface eth0 does not exist and no interface specified. Skipping static address 192.168.200.16.
-
6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Default interface eth0 does not exist and no interface specified. Skipping static address 192.168.200.17.
-
6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Default interface eth0 does not exist and no interface specified. Skipping static address 192.168.200.18.
-
6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: (VI_1): No VIP specified; at least one is required
-
6月 13 21:49:09 localhost.localdomain kernel: IPVS: Registered protocols (TCP, UDP, SCTP, AH, ESP)
-
6月 13 21:49:09 localhost.localdomain kernel: IPVS: Connection hash table configured (size=4096, memory=64Kbytes)
-
6月 13 21:49:09 localhost.localdomain kernel: IPVS: Creating netns size=2040 id=0
-
6月 13 21:49:09 localhost.localdomain kernel: IPVS: ipvs loaded.
-
6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Netlink reflector reports IP 192.168.0.11 added
-
6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Netlink reflector reports IP fe80::20c:29ff:fee3:8c45 added
-
6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Registering Kernel netlink reflector
-
6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Registering Kernel netlink command channel
-
6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Opening file '/etc/keepalived/keepalived.conf'.
-
6月 13 21:49:09 localhost.localdomain kernel: IPVS: [rr] scheduler registered.
-
6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Using LinkWatch kernel netlink reflector...
-
6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Activating healthchecker for service [192.168.201.100]:443
-
6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Activating healthchecker for service [192.168.200.2]:1358
-
6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Activating healthchecker for service [192.168.200.3]:1358
-
6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Activating healthchecker for service [192.168.200.4]:1358
-
6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Activating healthchecker for service [192.168.200.5]:1358
-
6月 13 21:49:10 localhost.localdomain kernel: IPVS: __ip_vs_del_service: enter
-
6月 13 21:49:10 localhost.localdomain kernel: IPVS: __ip_vs_del_service: enter
-
6月 13 21:49:10 localhost.localdomain kernel: IPVS: __ip_vs_del_service: enter
-
6月 13 21:49:10 localhost.localdomain Keepalived_vrrp[16359]: Stopped
-
6月 13 21:49:10 localhost.localdomain Keepalived[16357]: Keepalived_vrrp exited with permanent error CONFIG. Terminating
-
6月 13 21:49:10 localhost.localdomain Keepalived[16357]: Stopping
-
6月 13 21:49:10 localhost.localdomain Keepalived_healthcheckers[16358]: Removing service [192.168.201.100]:443 from VS [192.168.200.100]:443
-
6月 13 21:49:10 localhost.localdomain Keepalived_healthcheckers[16358]: Removing service [192.168.200.4]:1358 from VS [10.10.10.3]:1358
-
6月 13 21:49:10 localhost.localdomain Keepalived_healthcheckers[16358]: Removing service [192.168.200.5]:1358 from VS [10.10.10.3]:1358
-
6月 13 21:49:10 localhost.localdomain Keepalived_healthcheckers[16358]: Stopped
-
6月 13 21:49:15 localhost.localdomain Keepalived[16357]: Stopped Keepalived v1.3.4 (02/18,2017)
这是默认配置文件的配置情况和实际的环境不相符,需要修改配置文件的内容,只要配置正确是可以启动成功的,我下一篇文章会给出示例。
再说一下如何设置开机启动:
[root@localhost ~]# chkconfig keepalived on