Keepalived安装

Keepalived安装,第1张

Keepalived安装

下载地址:http://www.keepalived.org/download.html

1.安装所需软件包
yum install -y openssl openssl-devel
2.下载
wget https://www.keepalived.org/software/keepalived-2.2.4.tar.gz --no-check-certificate
3.解压,编译,安装
tar -zxvf keepalived-2.2.4.tar.gz -C /usr/local/
cd /usr/local/keepalived-2.2.4/ && ./configure --prefix=/usr/local/keepalived
make && make install
配置keepalived

将keepalived安装成Linux系统服务,因为没有使用keepalived默认的安装路径(默认路径:/usr/local),安装之后需要做一些修改工作
首先创建文件夹,将keepalived配置文件进行复制

mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

然后复制keepalived脚本文件

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/

如果存在则进行删除

rm /sbin/keepalived
ln -s /usr/keepalived/sbin/keepalived /sbin/
可以设置开机启动
chkconfig keepalived on
配置keepalived
vim /etc/keepalived/keepalived.conf

主节点

global_defs {
	router_id bhz157
}
vrrp_script chk_haproxy {
	script "/etc/keepalived/haproxy_check.sh"
	interval 2
	weight -20
}
vrrp_instance VI_1 {
	state MASTER
	interface eth0
	virtual_router_id 157
	mcast_src_ip 192.168.171.157
	priority 100
	nopreempt
	advert_int 1
	authentication {
		auth_type PASS
		auth_pass bhz
	}
	track_script {
		chk_haproxy
	}
	virtual_iptaddress {
		192.168.171.70
	}
}

从节点

global_defs {
	router_id bhz158
}
vrrp_script chk_haproxy {
	script "/etc/keepalived/haproxy_check.sh"
	interval 2
	weight -20
}
vrrp_instance VI_1 {
	state BACKUP
	interface eth0
	virtual_router_id 157
	mcast_src_ip 192.168.171.158
	priority 90
	nopreempt
	advert_int 1
	authentication {
		auth_type PASS
		auth_pass bhz
	}
	track_script {
		chk_haproxy
	}
	virtual_iptaddress {
		192.168.171.70
	}
}

执行脚本编写

位置:/etc/keepalived/haproxy_check.sh

#!/bin/bash
COUNT=`ps -C haproxy --no-header |wc -l`
if [ $COUNT -eq 0 ];then
	/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
	sleep 2
	if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
		killall keepalived
	fi
fi
执行脚本赋权
chmod +x /etc/keepalived/haproxy_check.sh
启动keepalived

如果没有启动haproxy先执行启动脚本

/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg

启动keepalived

service keepalived start | stop | status | restart

查看状态

ps -ef | grep keepalived

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4666238.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存