[bigdata-65] ubuntu 14.04服务器版 keepalived 双机热备ip漂移 python3+flask+gevent的web服务测试

1. 两个节点
192.168.17.112
192.168.17.109

2. 安装配置keepalived

2.1 双机执行 apt-get install keepalived


2.2 在112上配置/etc/keepalived/keepalived.conf,内容如下
------------------------------
global_defs {  
  router_id NodeA  
}
 
vrrp_instance VI_1 {  
  state BACKUP
  interface eth0
  virtual_router_id 51
  priority 150
  advert_int 1

  authentication {  
    auth_type PASS
    auth_pass 1111
  }  

  virtual_ipaddress {  
    192.168.17.113
  }
}
------------------------------

2.3 在109上配置/etc/keepalived/keepalived.conf,内容如下
------------------------------
global_defs {
  router_id NodeA
}

vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 51
  priority 90
  advert_int 1

  authentication {
    auth_type PASS
    auth_pass 1111
  }

  virtual_ipaddress {
    192.168.17.113
  }
}
------------------------------


2.4 两个节点都配置为Backup,但priority不同,高priority的自动成为master。


2.5 在两个节点上都启动keepalived

/etc/init.d/keepalived start

2.6 在两个节点上都执行ip a,那么,其中,112的优先级高,在112上能看到有两个ip地址
    inet 192.168.17.112/24 brd 192.168.17.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.17.113/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe0d:2112/64 scope link
       valid_lft forever preferred_lft forever
而在109上只有一个ip地址
link/ether 00:0c:29:8c:ef:e6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.17.109/24 brd 192.168.17.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe8c:efe6/64 scope link
       valid_lft forever preferred_lft forever

2.7 如果在112上执行/etc/init.d/keepalived stop,那么,此时ip a,能看到112上只有一个ip地址了,然后立刻在109上执行ip a,在109上就能看到两个ip地址,此时,ip就漂移到了109上。

2.8 在112上再次启动keepalived,/etc/init.d/keepalived start,此时能发现113这个ip又漂移到了112上。

3. 以web服务进行测试
3.1 双节点都安装
apt-get install python34 python3-pip
pip install gevent flask

3.2 在112上部署如下web程序
--------------------------
#!/usr/bin/env python3  
#! -*- coding:utf-8 -*-  
 
from flask import Flask, jsonify  
from gevent.wsgi import WSGIServer  
 
app = Flask(__name__)  
 
#这里的json使用中文key  
@app.route("/", methods=['GET', 'POST'])  
def index():  
    return jsonify({'ret':'1'})  
 
WSGIServer(('0.0.0.0', 8888), app).serve_forever()
--------------------------

3.3 在109上部署如下web程序
--------------------------
#!/usr/bin/env python3  
#! -*- coding:utf-8 -*-  
 
from flask import Flask, jsonify  
from gevent.wsgi import WSGIServer  
 
app = Flask(__name__)  
 
#这里的json使用中文key  
@app.route("/", methods=['GET', 'POST'])  
def index():  
    return jsonify({'ret':'2'})  
 
WSGIServer(('0.0.0.0', 8888), app).serve_forever()
--------------------------

3.4 在两个节点上都启动keepalived
然后,在第三个节点的浏览器访问
http://192.168.17.113:8888
此时,能看到"1"
关闭112上的keepavlied,然后再次访问
http://192.168.17.113:8888
此时,能看到"2"
然后,打开112上的keepalived,然后再次访问
http://192.168.17.113:8888
此时,能看到"1"
这个过程就是,113这个ip先指向112,再漂移到109,然后再漂移回112的过程
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值