【Keepalived】Keepalived安装教程

时间:2022-12-19 09:37:34

一、基本安装

1、基本环境:

 CentOS7

 keepalived-1.3.4.tar.gz

2、安装前需要的环境:

wget、gcc、pcre、openssl、zlib

在之前的Nginx安装教程中已指明怎么安装这些环境。在此不在赘述。

3、正式安装

进入下载目录:cd /usr/local/src

①下载keepalived:

wget http://www.keepalived.org/software/keepalived-1.3.4.tar.gz 

②解压缩:

tar -zxvf keepalived-1.3.4.tar.gz 

进行configure配置,指定安装目录: 

cd keepalived-1.3.4 && ./configure-prefix=/usr/local/keepalived-1.3.4 

④编译安装:

make && make install

二、环境配置

    安装完成后,进入安装目录的etc目录,将keepalived相应的配置文件拷贝到系统默认的目录中。keepalived启动时会从/etc/keepalived目录下查找keepalived.conf配置文件,但/etc目录下默认是没有keepalived目录的。因此需要手动创建。

①在/etc目录下创建keepalived目录:

cd /etc/ && mkdir keepalived

②将路径为/usr/local/src/keepalived-1.3.4/keepalived/etc/init.d的文件keepalived拷贝到/etc/init.d下:(解压缩路径)

cp /usr/local/src/keepalived-1.3.4/keepalived/etc/init.d/keepalived /etc/init.d/keepalived

③将路径为/usr/local/keepalived-1.3.4/etc/sysconfig/keepalived的文件拷贝到 /etc/sysconfig/keepalived(安装路径)

cp/usr/local/keepalived-1.3.4/etc/sysconfig/keepalived /etc/sysconfig/keepalived

④修改网卡接口以及vip(查看网卡接口命令为ip addr)

vim /usr/local/keepalived-1.3.4/etc/keepalived/keepalived.conf 

【Keepalived】Keepalived安装教程

⑤将路径为/usr/local/keepalived-1.3.4/etc/keepalived/keepalived.conf 的文件拷贝到/etc/keepalived/keepalived.conf

cp/usr/local/keepalived-1.3.4/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf

⑥去除执行权限:

cd/etc/init.d/ && chmod +x keepalived

⑦建立可执行文件链接:

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

⑧启动keepalived

service keepalived start

启动过程中可能会出现下列错误:

【Keepalived】Keepalived安装教程

执行命令:

systemctl daemon-reload

再次执行service keepalived start。keepalived成功启动

⑨查看执行状态

service keepalived status

可以看到keepalived运行正常,并且已经成功绑定VIP到网卡ens33

【Keepalived】Keepalived安装教程

 查看网卡是否绑定了VIP

ip a  

 【Keepalived】Keepalived安装教程

⑩设置开机自启:

chkconfig keepalived on

常用命令总结:

启动:service keepalived start

查看状态:service keepalived status

停止:service keepalived stop

重启:service keepalived restart