Linux 下 Keepalived 安装与卸载
keepalived 是一个类似于 layer3, 4 & 7 交换机制的软件,也就是我们平时说的第 3 层、第 4 层和第 7 层交换。Keepalived 的作用是检测 web 服务器的状态,如果有一台 web服务器死机,或工作出现故障,keepalived 将检测到,并将有故障的 web 服务器从系统中剔除,当web服务器工作正常后 keepalived 自动将 web 服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的 web 服务器。
安装
方法一:yum 方式
yum 安装
#安装依赖包
[root@localhost ~]# yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
[root@localhost ~]# yum install -y keepalived
初始化及启动
[root@localhost ~]# systemctl start keepalived //启动 keepalived
[root@localhost ~]# systemctl enable keepalived //加入开机启动 keepalived
[root@localhost ~]# systemctl restart keepalived //重新启动 keepalived
[root@localhost ~]# systemctl status keepalived //查看 keepalived 状态
方法二:源码包
官网下载keepalived的最新版本,解压并安装
[root@master local]# pwd
/usr/local
[root@master src]# wget http://www.keepalived.org/software/keepalived-2.0.7.tar.gz
[root@master local]# tar xvf keepalived-2.0.7.tar.gz
[root@master local]# cd keepalived-2.0.7
[root@master keepalived-2.0.7]# ./configure --prefix=/usr/local/keepalived
[root@master keepalived-2.0.7]# make && make install
完成后会在以下路径生成:
/usr/local/etc/keepalived/keepalived.conf
/usr/local/etc/sysconfig/keepalived
/usr/local/sbin/keepalived
初始化及启动
# keepalived 启动脚本变量引用文件,默认文件路径是 /etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)
[root@localhost /]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
# 将keepalived 主程序加入到环境变量(安装目录下)
[root@localhost /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
# keepalived 启动脚本(源码目录下),放到 /etc/init.d/ 目录下就可以使用 service 命令便捷调用
[root@localhost /]# cp /usr/local/src/keepalived-2.0.7/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
# 将配置文件放到默认路径下
[root@localhost /]# mkdir /etc/keepalived
[root@localhost /]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
加为系统服务:chkconfig –add keepalived开机启动:chkconfig keepalived on查看开机启动的服务:chkconfig –list启动、关闭、重启 service keepalived start|stop|restart
卸载
卸载keeplived工作路径
[root@localhost /]# cd /usr/local/src/keepalived #源码所在目录(根据实际情况进入对应目录)
[root@localhost keepalived]#make uninstall
删除相关文件
#删除keepalived
[root@localhost /]# rm -f /usr/local/sbin/keepalived
[root@localhost /]# rm -f /usr/local/etc/rc.d/init.d/keepalived
[root@localhost /]# rm -f /usr/local/etc/sysconfig/keepalived
[root@localhost /]# rm -rf /usr/local/etc/keepalived
[root@localhost /]# rm -f /usr/local/bin/genhash
[root@localhost /]# rm -rf /usr/local/keepalived
[root@localhost /]# rm -rf /etc/keepalived
[root@localhost /]# rm -f /etc/rc.d/init.d/keepalived
[root@localhost /]# rm -f /usr/sbin/keepalived
[root@localhost /]# rm -f /etc/sysconfig/keepalived
[root@localhost /]# rm -f /etc/systemd/system/multi-user.target.wants/keepalived.service
测试是否卸载成功
[root@localhost /]# serive keepalived satrt
[root@localhost /]# systemctl start keepalived
当命令无效时,证明卸载成功。
命令
centOs
[root@localhost /]# systemctl start keepalived -- 启动
[root@localhost /]# systemctl stop keepalived --关闭
[root@localhost /]# ps aux|grep keepalived --查看进程
linux
[root@localhost /]# service keepalived start -- 启动
[root@localhost /]# service keepalived stop --关闭
[root@localhost /]# ps aux|grep keepalived --查看进程