美文网首页
Keepalived+flask

Keepalived+flask

作者: frankie_cheung | 来源:发表于2020-09-22 09:51 被阅读0次

1.机器信息

机器 ip 角色
node1 10.12.0.150 master
node2 10.12.0.169 backup
虚拟IP 10.12.0.155 虚拟ip

2.虚拟机信息

[root@node1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)

3.安装Keepalived
yum install -y keepalived
4.编辑keepalived配置文件
master


global_defs {
    router_id NodeA      # 节点标识
}

vrrp_script chk_webapp {
    script "/data/python_test/failover.sh"    # 只要遵从“正常返回0,失败返回1”的规则即可
    interval 1     # 检测间隔
    weight -20      # 失败时权重降低数
}

vrrp_instance VI_1 {
    state       MASTER      # MASTER 或BACKUP
    interface   enp0s3            # 绑定的网络接口。具体主机具体配置
    virtual_router_id   51   # 各节点一致
    priority    100                # 节点权重
    advert_int  1                # 广播间隔
    authentication {
        auth_type PASS      # 节点间验证模式
        auth_pass 1234      # 主备口令相同
    }

    virtual_ipaddress {
        10.12.0.155/24   # 可不必给出掩码位数
    }
    track_script {
        chk_webapp            # 检查服务状态的vrrp_script
    }
}

backup

global_defs {
    router_id NodeB      # 节点标识

   script_user root
   enable_script_security

}

vrrp_script chk_webapp {
    script "data/python_test/failover.sh"    # 只要遵从“正常返回0,失败返回1”的规则即可
    interval 1     # 检测间隔
    weight -20      # 失败时权重降低数
}

vrrp_instance VI_1 {
    state       BACKUP      # MASTER 或BACKUP
    interface   enp0s3            # 绑定的网络接口。具体主机具体配置
    virtual_router_id   51   # 各节点一致
    priority    100                # 节点权重
    advert_int  1                # 广播间隔
    authentication {
        auth_type PASS      # 节点间验证模式
        auth_pass 1234      # 主备口令相同
    }

    virtual_ipaddress {
        10.12.0.155/24   # 可不必给出掩码位数
    }
    track_script {
        chk_webapp            # 检查服务状态的vrrp_script
    }

5.web服务检查脚本

#!/bin/bash

SERVICE='flask_api.py'

STATUS=$(ps aux | grep $SERVICE  | grep -v grep)
# 状态正常返回0,不正常就返回1。
if [ "$STATUS"x != ""x ]; then
    exit 0
else
    exit 1
fi

6.python flask代码

from flask import  Flask
import socket
host_name = socket.gethostname()
app = Flask(__name__)

@app.route('/')
def index():
    return '<h1>Hello World! this {} 1</h1>'.format(host_name)
if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0',port=5001)

7.启动keepalived
systemctl start keepalived
8.测试


image.png

把node1的web服务关闭


image.png

相关文章

  • Keepalived+flask

    1.机器信息 机器ip角色node110.12.0.150masternode210.12.0.169backup...

网友评论

      本文标题:Keepalived+flask

      本文链接:https://www.haomeiwen.com/subject/uhgiyktx.html