黑马程序员技术交流社区

标题: 【成都校区*网关高可用之keepavlived全流程】 [打印本页]

作者: 小蜀哥哥    时间: 2019-9-19 14:35
标题: 【成都校区*网关高可用之keepavlived全流程】
1.场景描述
因为要做网关的高可用,用到了keepalived+nginx,来保证nginx的高可用。
2. 解决方案2.1 安装keepalived2.1.1 安装相关依赖包
[Bash shell] 纯文本查看 复制代码
[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
[AppleScript] 纯文本查看 复制代码
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为系统服务
[AppleScript] 纯文本查看 复制代码
[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 设置开机启动
[AppleScript] 纯文本查看 复制代码
# 设置开机启动
[root@t-ruanjianlaowang ~]# chkconfig keepalived on

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

到这里仅仅是keepalived安装完成,接下来还要根据项目项目配置keepalived.conf信息。
2.2 配置文件修改2.2.1 master的conf配置文
[AppleScript] 纯文本查看 复制代码
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,也上传下
[AppleScript] 纯文本查看 复制代码
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文件
[AppleScript] 纯文本查看 复制代码
/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)配置完成后开始启动
[AppleScript] 纯文本查看 复制代码
[root@t-ruanjianlaowang ~]# service keepalived start
(2)启动过程中,打开日志看一下(刚开始可能会有问题,看下日志啥都明白了)
[AppleScript] 纯文本查看 复制代码
[root@t-ruanjianlaowang ~]# tail -f  /var/log/messages
(3)假如启动不了,可以尝试以下方式
[AppleScript] 纯文本查看 复制代码
[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 验证
[AppleScript] 纯文本查看 复制代码
[root@t-ruanjianlaowang ~]#ip addr show ens666
[root@t-ruanjianlaowang ~]#ip addr show ens168
ens666、ens168为网卡名称








欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2