Centos7下安装运行keepalived
master服务器ip地址:192.168.0.182
slave服务器ip地址:192.168.0.189
虚拟ip(VIP,一个尚未占用的内网ip即可)地址:192.168.0.180
确认使用的网卡
使用第2个网卡【enp0s3】
使用wget命令下载,下载位置/usr/local/
keepalived下载地址:http://www.keepalived.org/download.html
wget http://www.keepalived.org/software/keepalived-1.4.2.tar.gz
解压:
tar zxvf keepalived-1.4.2.tar.gz
安装依赖插件:
yum install -y gcc openssl-devel popt-devel
编译安装:
cd keepalived-1.4.2 #指定安装目录 ./configure --prefix=/usr/local/keepalived make && make install
运行前配置
# cp /usr/local/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/ # mkdir /etc/keepalived # cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ # cp /usr/local/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ # cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
修改配置文件:
vim /etc/keepalived/keepalived.conf
具体配置如下:
master服务器配置:
! Configuration File for keepalived global_defs { #一个没重复的名字即可 router_id xxoo_master } # 检测nginx是否运行 vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } vrrp_instance VI_1 { # 此处不设置为MASTER,通过priority来竞争master state BACKUP # 网卡名字,文章下方会给出如何获取网卡名字的方法 interface enp0s3 # 同一个keepalived集群的virtual_router_id相同 virtual_router_id 51 # 权重,master要大于slave priority 100 # 主备通讯时间间隔 advert_int 1 # 如果两节点的上联交换机禁用了组播,则采用vrrp单播通告的方式 # 本机ip unicast_src_ip 192.168.0.182 unicast_peer { # 其他机器ip 192.168.0.189 } # 设置nopreempt防止抢占资源 nopreempt # 主备保持一致 authentication { auth_type PASS auth_pass 1111 } # 与上方nginx运行状况检测呼应 track_script { chk_nginx } virtual_ipaddress { # 虚拟ip地址(VIP,一个尚未占用的内网ip即可) 192.168.0.180 } }
slave服务器配置:
! Configuration File for keepalived global_defs { #一个没重复的名字即可 router_id xxoo_slave } # 检测nginx是否运行 vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } vrrp_instance VI_1 { # 此处不设置为MASTER,通过priority来竞争master state BACKUP # 网卡名字,文章下方会给出如何获取网卡名字的方法 interface enp0s3 # 同一个keepalived集群的virtual_router_id相同 virtual_router_id 51 # 权重,master要大于slave priority 90 # 主备通讯时间间隔 advert_int 1 # 如果两节点的上联交换机禁用了组播,则采用vrrp单播通告的方式 # 本机ip unicast_src_ip 192.168.0.189 unicast_peer { # 其他机器ip 192.168.0.182 } # 设置nopreempt防止抢占资源 nopreempt # 主备保持一致 authentication { auth_type PASS auth_pass 1111 } # 与上方nginx运行状况检测呼应 track_script { chk_nginx } virtual_ipaddress { # 虚拟ip地址(VIP,一个尚未占用的内网ip即可) 192.168.0.180 } }
nginx监听脚本:
#创建nginx检测脚本
#touch nginx_check.sh
#给脚本增加可执行权限
#chmod +x nginx_check.sh
脚本添加如下内容:
#! /bin/bash pidof nginx if [ $? -ne 0 ];then /etc/init.d/keepalived stop fi
防止出现脑裂现象(主备同时获取了VIP地址)
# 指定keepalived配置的网卡:enp0s3,固定的VRRP广播地址:224.0.0.18 firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface enp0s3 --destination 224.0.0.18 --protocol vrrp -j ACCEPT firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 --out-interface enp0s3 --destination 224.0.0.18 --protocol vrrp -j ACCEPT firewall-cmd --reload # 查看配置的规则 firewall-cmd --direct --get-rules ipv4 filter INPUT firewall-cmd --direct --get-rules ipv4 filter OUTPUT
启动服务:
service keepalived start
# 配置开机自启动
systemctl enable keepalived
查看服务启动情况:
ps -aux |grep keepalived
查看启动日志:
journalctl -xe
查看keepalived日志
tail -f /var/log/messages
配置成功后的效果。enp0s3是网卡名字;192.168.0.180是虚拟ip,已经成功绑定到网卡上。
QQ技术交流群:282575808
--------------------------------------
声明: 原创文章,未经允许,禁止转载!
--------------------------------------