【Linux运维-集群技术进阶】Keepalived安装配置

时间:2023-01-27 21:54:41

认识Keepalived

Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。

Keepalived实现服务的高可用(HA),应用已经非常广泛,很多软件都会和他搭配使,比如LVS,Nginx,Redis等等。今天先来看看Linux环境中Keepalived是如何安装配置的。

去Keepalived官网(www.keepalived.org)下载软件包,上传至服务器。


开始安装

解压:

[root@localhost ~]# cd /usr/local/software/
[root@localhost software]# tar -zxvf keepalived-1.2.19.tar.gz

安装依赖插件:

yum install -y gcc openssl-devel popt-devel

编译安装:

[root@localhost software]# cd keepalived-1.2.19
[root@localhost keepalived-1.2.19]# ./configure --prefix=/usr/local/keepalived
[root@localhost keepalived-1.2.19]#make && make install 


编译之后的配置

1. 建立服务启动脚本,以便使用service命令控制

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived     /etc/init.d/keepalived  

//获取权限
chmod +x /etc/init.d/keepalived


2. 因为我们使用非默认路径(/usr/local)安装的keepalived, 故需要修改几处路径,以保证keepalived能正常启动, 需要修改的文件如下:

修改/etc/init.d/keepalived, 寻找大约15行左右的. /etc/sysconfig/keepalived, 修改为:
. /usr/local/keepalived/etc/sysconfig/keepalived, 即指向正确的配置文件位置。


3. 配置环境变量(将keepavlied主程序所在路径导入到环境变量PATH中):

KEEPALIVED_HOME=/usr/local/keepalived
PATH=$KEEPALIVED_HOME/sbin:$PATH
export PATH


4. 建立可执行文件链接:

 ln -s /usr/local/keepalived/sbin/keepalived  /usr/sbin/


5. 修改/usr/local/keepalived/etc/sysconfig/keepalived文件,设置正确的服务启动参数

KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"


6. 经过以上修改,keepalived基本安全配置即可完成,启动测试之:

service keepalived restart


7. 将Keepalived服务设置为开机启动

chkconfig keepalived on


启动keepalived

[root@localhost keepalived]# service keepalived start
Starting keepalived (via systemctl):                       [  确定  ]
[root@localhost keepalived]# 



至此Keepalived的安装配置已经完成,后续会介绍Keepalived如何实现HA。