微服务实战系列-网关高可用之中间件Keepalived
Posted 软件老王
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微服务实战系列-网关高可用之中间件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
我们使用的版本: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的显示
1.
2.
3.
4.
5
原创不易,如果觉得有点用的话,请毫不留情地素质三连吧,分享、点赞、在看,我不挑,因为这将是我写作更多优质文章的最强动力。
以上是关于微服务实战系列-网关高可用之中间件Keepalived的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud Alibaba微服务实战三十四 - 隐私接口禁止外部访问