微服务实战系列(十)-网关高可用之中间件Keepalived

1.场景描述

因为要做网关的高可用,用到了keepalived+nginx,来保证nginx的高可用,如下图:

安装了keepavlived,走了一些弯路,记录下吧,nginx的安装就不多说了,博客已经介绍了好几篇了。

2. 解决方案

2.1 安装keepalived

2.1.1 安装相关依赖包
[root@t-ruanjianlaowang ~]# yum install -y libnl*
[root@t-ruanjianlaowang ~]#  yum install -y libnfnetlink-devel zlib zlib-devel gcc gcc-c++ openssl openssl-devel openssh

假如连不了,网上也有这些包的下载,下载后tar解压也行。

2.1.2 下载并安装keepalived

官网地址:https://www.keepalived.org/download.html

我们使用的最新版本:keepalived-2.0.18.tar.gz,993k

tar -zxvf keepalived-2.0.18.tar.gz
cd keepalived-2.0.18/
./configure --prefix=/usr/local/keepalived
make
make install
2.1.3 添加keepavlived为系统服务
[root@t-ruanjianlaowang ~]#  cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/keepalived 

[root@t-ruanjianlaowang ~]#  cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived

[root@t-ruanjianlaowang ~]# cp /root/tools/keepalived-2.0.18/keepalived/etc/init.d/keepalived   /etc/init.d/keepalived

#/etc/keepalived下好像有了keepalived文件夹,有了就不用mkdir了,软件老王
[root@t-ruanjianlaowang ~]# mkdir /etc/keepalived  
[root@t-ruanjianlaowang ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
2.1.4 设置开机启动
# 设置开机启动
[root@t-ruanjianlaowang ~]# chkconfig keepalived on

[root@t-ruanjianlaowang ~]# service keepalived start|stop|restart

到这里仅仅是keepalived安装完成,接下来还要根据项目项目配置keepalived.conf信息。

如果你觉得文章对你有些帮助,欢迎微信搜索「软件老王」第一时间阅读或交流!

2.2 配置文件修改

2.2.1 master的conf配置文件
vi cd /etc/keepalived/keepalived.conf
!Configuration File for keepalived
global_defs {
   router_id RUANJIANLAOWANG_MASTER
}

vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}

vrrp_instance VI_1 {
state MASTER
interface ens666    #这里必须非常注意,软件老王刚开始没太注意,这里是你的网卡名称
virtual_router_id 81  #这个会在backup机器上用到
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}

track_script {
chk_nginx
}
virtual_ipaddress {
10.192.168.21
}
}
2.2.2 还有个检查nginx是否挂掉的sh,也上传下
vi nginx_check.sh 
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
    /usr/sbin/nginx
    sleep 2
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
        /etc/init.d/keepalived stop
    fi
fi
2.2.3 backup机器上conf文件
/bin/bash: Configuration: command not found
bal_defs {
 router_id RUANJIANLAOWANG2
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
interval 2 ## 检测时间间隔
weight -20 ## 如果条件成立,权重-20
}

vrrp_instance VI_1 {
state BACKUP
interface ens168
virtual_router_id 81

priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 111track_script {
           chk_nginx ## 执行 Nginx 监控的服务,软件老王
 }
virtual_ipaddress {
10.192.168.21
    }
}

这里记着也把检查nginx的sh文件上传上,跟master上的一样。

2.3 启动

(1)配置完成后开始启动

[root@t-ruanjianlaowang ~]# service keepalived start

(2)启动过程中,打开日志看一下(刚开始可能会有问题,看下日志啥都明白了)

[root@t-ruanjianlaowang ~]# tail -f  /var/log/messages

(3)假如启动不了,可以尝试以下方式。

[root@t-ruanjianlaowang ~]# systemctl daemon-reload

#上面试过后,不行再重新注册服务
[root@t-ruanjianlaowang ~]#chkconfig --del keepalived
[root@t-ruanjianlaowang ~]# cd /usr/lib/systemd/system/
[root@t-ruanjianlaowang ~]#rm -rf keepalived.service
[root@t-ruanjianlaowang ~]# chkconfig --add keepalived

2.4 验证

[root@t-ruanjianlaowang ~]#ip addr show ens666
[root@t-ruanjianlaowang ~]#ip addr show ens168
ens666、ens168为网卡名称

假如绑定成功,软件老王机器里显示的信息里面会有:10.192.168.21的显示

更多知识请关注公众号:「软件老王」,IT技术与相关干货分享,回复关键字获取对应干货,java,送必看的10本“武功秘籍”;图片,送100多万张可商用高清图片;面试,送刚毕业就能月薪“20k”的java面试题,软考,送官方pdf书籍与通关论文,后续会不断更新,比如“工具”,“视频“等,已经在整理中。

posted @ 2020-09-30 09:49  软件老王  阅读(400)  评论(0编辑  收藏  举报