安装前准备
基础系统:CentOS 7
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
1.下载keepalived
方法一:
yum -y install keepalived
方法二:
下载二进制源码包:http://www.keepalived.org/download.html
#下载安装文件
cd /usr/local
wget http://www.keepalived.org/software/keepalived-2.0.18.tar.gz
#解压文件
tar -zxvf keepalived-2.0.18.tar.gz
#编译
cd keepalived-2.0.18/
#--prefix 指定安装地址
#/usr/local/keepalived/ 安装的目录,不要和自己安装文件一个目录,不然报错
./configure --prefix=/usr/local/keepalived/
#编译并安装
make && make install
2.配置
复制文件:
[root@localhost ~]# cp /usr/local/keepalived-2.0.18/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 /usr/local/keepalived-2.0.18/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
编辑:
vi /etc/keepalived/keepalived.conf
/etc/keepalived/keepalived.conf内容如下
global_defs {
notification_email {
#mr@mruse.cn # 指定keepalived在发生切换时需要发送email到的对象,一行一个
#sysadmin@firewall.loc
}
notification_email_from xxx@163.com # 指定发件人
smtp_server smtp@163.com # smtp 服务器地址
smtp_connect_timeout 30 # smtp 服务器连接超时时间
router_id LVS_1 # 必填,标识本节点的字符串,通常为hostname,但不一定非得是hostname,故障发生时,邮件通知会用到
}
vrrp_script chk_tomcat { #详细看下面
script "/etc/keepalived/tomcat_check.sh" #检测服务shell
interval 2 #每个多长时间探测一次
weight -20 #每个多长时间探测一次
}
_instance VI_1 { # 实例名称
state MASTER # 必填,可以是MASTER或BACKUP,不过当其他节点keepalived启动时会将priority比较大的节点选举为MASTER
interface ens33 # 必填, 节点固有IP(非VIP)的网卡,用来发VRRP包做心跳检测
mcast_src_ip 192.168.2.89 #本机的ip,需要修改
virtual_router_id 101 # 必填,虚拟路由ID,取值在0-255之间,用来区分多个instance的VRRP组播,同一网段内ID不能重复;主备必须为一样;
priority 100 # 必填,用来选举master的,要成为master那么这个选项的值最好高于其他机器50个点,该项取值范围是1-255(在此范围之外会被识别成默认值100)
advert_int 1 # 必填,检查间隔默认为1秒,即1秒进行一次master选举(可以认为是健康查检时间间隔)
authentication { # 必填,认证区域,认证类型有PASS和HA(IPSEC),推荐使用PASS(密码只识别前8位)
auth_type PASS # 默认是PASS认证
auth_pass 1111 # PASS认证密码
}
virtual_ipaddress {
192.168.2.90 # 必填,虚拟VIP地址,允许多个
}
track_script { # 检测shell
chk_tomcat
}
}
vrrp_script 里的script返回值为0时认为检测成功,其它值都会当成检测失败;
- weight 为正时,脚本检测成功时此weight会加到priority上,检测失败时不加;
A.主失败:主 priority < 从 priority + weight 时会切换。
B.主成功:主 priority + weight > 从 priority + weight 时,主依然为主 - weight 为负时,脚本检测成功时此weight不影响priority,检测失败时priority – abs(weight)
A.主失败:主 priority – abs(weight) < 从priority 时会切换主从
B.主成功:主 priority > 从priority 主依然为主
检测shell:/etc/keepalived/tomcat_check.sh 可以根据 tomcat 服务检测结果返回不同的值,成功执行 exit 0 返回 0,失败执行 exit 1 返回 1,vrrp_script 可以结合返回值来对当前keepalived优先级做处理
3.开启服务
设置 keepalived 服务开机启动
# chkconfig keepalived on
# 启动keepalived 服务
# service keepalived start
查看keepalived进程,出现如下三个进程即为启动成功
[root@localhost ~]# ps -ef | grep keep
root 7925 1 0 11:27 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D
root 7926 1 0 11:27 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D
root 7927 7926 0 11:27 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D
root 7956 7888 0 12:02 pts/1 00:00:00 grep --color=auto keep
4.指定日志的输出文件
修改 /etc/sysconfig
vim /etc/sysconfig
#指定位置修改为如下内容
KEEPALIVED_OPTIONS="-D -d -S 0"
修改 /etc/rsyslog.conf
vim /etc/rsyslog.conf
#在最后一行添加如下内容
local0.* /var/log/keepalived.log
重启服务测试
service rsyslog restart
service rsyslog restart
#如下命令有日志内容输出即为成功
tailf /var/log/keepalived.log
#centos 基本可以成功,但是 debain 可能失败,失败上面 2 个文件可以改为如下内容,再重启服务测试
KEEPALIVED_OPTIONS="-D -d -S 1"
local1.* /var/log/keepalived.log
参考:https://blog.csdn.net/weixin_33845881/article/details/86016142
如果有哪些不对的地方烦请指认,先行感谢