Keepalived(一)在CentOS7下安装和运行Keepalived

来源:互联网 发布:nginx pagespeed 配置 编辑:程序博客网 时间:2024/06/01 12:03

简介

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 startStarting keepalived (via systemctl):                       [  确定  ]

查看服务启动情况,发现没有启动成功:
[root@localhost ~]# ps -aux |grep keepalivedroot      16370  0.0  0.0 112664   936 pts/0    S+   21:50   0:00 grep --color=auto keepalived

为什么呢?通过执行如下命令发现:
[root@localhost ~]# journalctl -xe6月 13 21:49:09 localhost.localdomain Keepalived[16356]: Unable to resolve default script username 'keepalived_script' - ignoring6月 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=163586月 13 21:49:09 localhost.localdomain Keepalived[16357]: Starting VRRP child process, pid=163596月 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 ipvs6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Netlink reflector reports IP 192.168.0.11 added6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Netlink reflector reports IP fe80::20c:29ff:fee3:8c45 added6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Registering Kernel netlink reflector6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Registering Kernel netlink command channel6月 13 21:49:09 localhost.localdomain Keepalived_vrrp[16359]: Registering gratuitous ARP shared channel6月 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 required6月 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=06月 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 added6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Netlink reflector reports IP fe80::20c:29ff:fee3:8c45 added6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Registering Kernel netlink reflector6月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Registering Kernel netlink command channel6月 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]:4436月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Activating healthchecker for service [192.168.200.2]:13586月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Activating healthchecker for service [192.168.200.3]:13586月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Activating healthchecker for service [192.168.200.4]:13586月 13 21:49:09 localhost.localdomain Keepalived_healthcheckers[16358]: Activating healthchecker for service [192.168.200.5]:13586月 13 21:49:10 localhost.localdomain kernel: IPVS: __ip_vs_del_service: enter6月 13 21:49:10 localhost.localdomain kernel: IPVS: __ip_vs_del_service: enter6月 13 21:49:10 localhost.localdomain kernel: IPVS: __ip_vs_del_service: enter6月 13 21:49:10 localhost.localdomain Keepalived_vrrp[16359]: Stopped6月 13 21:49:10 localhost.localdomain Keepalived[16357]: Keepalived_vrrp exited with permanent error CONFIG. Terminating6月 13 21:49:10 localhost.localdomain Keepalived[16357]: Stopping6月 13 21:49:10 localhost.localdomain Keepalived_healthcheckers[16358]: Removing service [192.168.201.100]:443 from VS [192.168.200.100]:4436月 13 21:49:10 localhost.localdomain Keepalived_healthcheckers[16358]: Removing service [192.168.200.4]:1358 from VS [10.10.10.3]:13586月 13 21:49:10 localhost.localdomain Keepalived_healthcheckers[16358]: Removing service [192.168.200.5]:1358 from VS [10.10.10.3]:13586月 13 21:49:10 localhost.localdomain Keepalived_healthcheckers[16358]: Stopped6月 13 21:49:15 localhost.localdomain Keepalived[16357]: Stopped Keepalived v1.3.4 (02/18,2017)

这是默认配置文件的配置情况和实际的环境不相符,需要修改配置文件的内容,只要配置正确是可以启动成功的,我下一篇文章会给出示例。

再说一下如何设置开机启动:
[root@localhost ~]# chkconfig keepalived on



原创粉丝点击