keepalived2.0.16部署及配置

服务版本说明

服务 版本号 备注
Keepalived keepalived-2.0.16

部署环境

环境centos7.5
Keepalived-2.0.16
主机名:haproxy1、haproxy2
IP:192.168.43.88、192.168.43.89

下载路径

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

安装依赖项

[root@ haproxy1 /]# yum install -y openssl* popt* kernel*
[root@ haproxy1 /]# ln -s /usr/src/kernels/3.10.0-957.21.3.el7.x86_64/ /usr/src/linux #软连接

创建程序包存放路径

[root@ haproxy1 /]# mkdir -p /data/software/keepalived

上传keepalived-2.0.16.tar.gz

[root@haproxy1 /]# cd /data/software/keepalived
[root@haproxy1 haproxy]# rz -E
rz waiting to receive.
[root@haproxy1 keepalived]# tar -zxvf keepalived-2.0.16.tar.gz

编译安装

[root@haproxy1 keepalived]# cd keepalived-2.0.16
[root@haproxy1 keepalived-2.0.16]#./configure --prefix=/opt/keepalived #prefix指定安装目录
[root@haproxy1 keepalived-2.0.16]# make && make install

设置启动脚本

[root@haproxy1 keepalived-2.0.16]# cd /opt/keepalived/etc/keepalived
[root@haproxy1 keepalived-2.0.16]# cp /opt/keepalived/sbin/keepalived /usr/bin #将keepalived主程序加入到环境变量
[root@haproxy1 sbin]# find / -name keepalived*init
/data/software/keepalived/keepalived-2.0.16/keepalived/etc/init.d/keepalived.rh.init
[root@haproxy1 sbin]# cp -r /data/software/keepalived/keepalived-2.0.16/keepalived/etc/init.d/keepalived.rh.init /etc/init.d/keepalived #keepalived启动脚本,放到/etc/init.d/目录下就可以使用service命令便捷调用

cp /opt/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ #keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以直接修改启动脚本中文件路径
[root@haproxy1 sysconfig]# cat /opt/keepalived/etc/sysconfig/keepalived |grep “KEEPALIVED_OPTIONS”
KEEPALIVED_OPTIONS="-D -f /opt/keepalived/etc/keepalived/keepalived.conf -p /opt/keepalived/run/keepalived.pid " #指定keepalived配置文件路径、pid路径

vi /usr/lib/systemd/system/keepalived.service #修改pid路径为/opt/keepalived/run/keepalived.pid

chkconfig --add keepalived
systemctl enable keepalived.service #设置开机自启
#或者把启动命令/etc/init.d/keepalived start写入/etc/rc.local

配置文件

cat /opt/keepalived/etc/keepalived/keepalived.conf #修改后的配置文件,具体按需配置
global_defs { #这里关于邮箱的都删掉了现在用不到。
smtp_connect_timeout 30 #连接超时时间
router_id haproxy_01 #相当于给这个服务器起个昵称
}
vrrp_instance VI_1 {
state MASTER #定义为主服务器
interface ens33 #承载漂移ip的网卡 7的系统 ens开头,具体看自己的网卡
virtual_router_id 51 #定义一个热备组,可以认为这是51号热备组
priority 150 #主服务器优先级要比备服务器高,建议相差50以上
advert_int 1 #1秒互相通告一次,检查对方死了没。
authentication {
auth_type PASS #认证类型
auth_pass 1111 #认证密码 这些相当于暗号
}
virtual_ipaddress {
192.168.43.100 #漂移ip
}
}

#备机按如上安装,配置文件参考如下:
global_defs { #这里关于邮箱可以都删掉
#notification_email { #指定Keepalived在发生事情的时候,发送邮件通知,每行一个地址,本次未配置
#}
#notification_email_from *** #指定发件人,本次未配置
#smtp_server 127.0.0.1 #发送email的smtp地址
smtp_connect_timeout 30 #连接超时时间
router_id haproxy_02 #相当于给这个服务器起个昵称
}
vrrp_instance VI_1 {
state BACKUP #定义为主服务器 备机修改为BACKUP
interface ens33 #承载漂移ip的网卡 7的系统 ens开头,以实际情况为准
virtual_router_id 51 #定义一个热备组,可以认为这是51号热备组
priority 100 #备服务器要比主服务器优先级低
advert_int 1 #1秒互相通告一次,检查对方死了没。
authentication {
auth_type PASS #认证类型
auth_pass 1111 #认证密码 这些相当于暗号
}
virtual_ipaddress {
192.168.100.100 #漂移ip,跟主机配置一致
}

启动并测试

Ping 通配置文件keepalived.cfg里配置的虚拟IP
在这里插入图片描述测试主备切换
keepalived2.0.16部署及配置_第1张图片keepalived2.0.16部署及配置_第2张图片
#keepalive参数还有很多需了解的,比如设置不抢占模式(权重与优先级等如何设置)、比如配置健康检查脚本。。。最终使用还需按实际项目来配置。

心不死,土不埋,学无止境

你可能感兴趣的:(linux应用服务部署)