Openstack多VIP与实例绑定脚本

一:因业务需要在openstack上的两个虚拟机关联两个VIP,而且数量比较大,因此使用如下脚本配置:

1.1:版本1:

手动执行并绑定2个VIP:

# cat /home/www/vip.py

#!/bin/env  python
#coding:utf-8
#Author: ZhangJie
import  sys
import  subprocess
def admin_env(): #导入admin用户环境变量
    obj = subprocess.Popen(("source /root/script/admin-ocata.sh"),shell=True,stdout=subprocess.PIPE)
    data = obj.stdout.read()
    print data

def ip_data():
    VIP1=sys.argv[1]
    VIP2=sys.argv[2]
    VM1_IP=sys.argv[3]
    VM2_IP=sys.argv[4]
    #查询并保存第一台实例的ID,用于后面的VIP 关联
    obj = subprocess.Popen(("openstack port list |  grep -w %s | awk '{print $2}'" % (VM1_IP)),shell=True,stdout=subprocess.PIPE)
    VM1_ID = obj.stdout.read()
    VM1_ID = VM1_ID.split()[0]
    print ("第一台虚拟机的ID 是%s") % (VM1_ID)
    #查询并保存第二台实例的ID,用于后面的VIP 关联
    obj = subprocess.Popen(("openstack port list |  grep -w %s | awk '{print $2}'" % (VM2_IP)),shell=True,stdout=subprocess.PIPE)
    VM2_ID = obj.stdout.read()
    VM2_ID = VM2_ID.split()[0]
    print ("第一台虚拟机的ID 是%s") % (VM2_ID)
    #将第一个VIP 关联至网络
    obj = subprocess.Popen(("neutron port-create --fixed-ip ip_address=%s da8e2dca-aa88-40fa-bfec-7fb02c010068"  % (VIP1)),shell=True,stdout=subprocess.PIPE)    
restful = obj.stdout.read() print restful #将第二个VIP 关联至网络 obj = subprocess.Popen(("neutron port-create --fixed-ip ip_address=%s da8e2dca-aa88-40fa-bfec-7fb02c010068" % (VIP2)),shell=True,stdout=subprocess.PIPE)
restful = obj.stdout.read() print restful #将第一个虚拟机关联至两个VIP obj = subprocess.Popen(("neutron port-update %s --allowed_address_pairs list=true type=dict ip_address={%s,%s}" % (VM1_ID,VIP1,VIP2)),shell=True,stdout=subprocess.PIPE)
restful = obj.stdout.read() print restful print ("第一台虚拟机%s已经关联至VIP: %s和%s") % (VM1_IP,VIP1,VIP2) #将第二个虚拟机关联至两个VIP obj = subprocess.Popen(("neutron port-update %s --allowed_address_pairs list=true type=dict ip_address={%s,%s}" % (VM2_ID,VIP1,VIP2)),shell=True,stdout=subprocess.PIPE)
restful = obj.stdout.read() print restful print ("第二台虚拟机%s已经关联至VIP: %s和%s") % (VM2_IP,VIP1,VIP2) def main(): admin_env() ip_data() if __name__ == '__main__': main()

注意:将VIP关联的网络ID(da8e2dca-aa88-40fa-bfec-7fb02c010068)需要修改为新的网络ID。

执行脚本:

# python  vip.py  192.168.1.248 192.168.1.249 192.168.1.200 192.168.1.201  #前面两个是VIP地址,后面两个是虚拟机IP地址。

1.2:版本2:

如果有很多VIP (几百个)需要与虚拟机进行批量绑定,如果要手动一个个操作实在太慢而且容易出错,因此可以将VIP 和虚拟机的对应关系保存到文本文件,然后再从文本文件读取VIP 传递给脚本做自动做绑定:

保存VIP文本格式如下:

# cat /home/www/vip.txt

VIP1 VIP2 VMIP1 VMIP2
VIP3 VIP4 VMIP3 VMIP3
。。。。。。。。。。。。

绑定脚本内容如下:

# cat vip.py

#!/bin/env  python
#coding:utf-8
#Author: ZhangJie

import sys,subprocess
with open("vip.txt") as f:
    for a in f.readlines():
        VIP1,VIP2,VM1_IP,VM2_IP = a.split()[0],a.split()[1],a.split()[2],a.split()[3]
        obj = subprocess.Popen(("openstack port list |  grep -w %s | awk '{print $2}'" % (VM1_IP)),shell=True,stdout=subprocess.PIPE)
        VM1_ID = obj.stdout.read()
        VM1_ID = VM1_ID.split()[0]
        print ("第一台虚拟机%s的ID 是%s") % (VM1_IP,VM1_ID)
        #查询并保存第二台实例的ID,用于后面的VIP 关联
        obj = subprocess.Popen(("openstack port list |  grep -w %s | awk '{print $2}'" % (VM2_IP)),shell=True,stdout=subprocess.PIPE)
        VM2_ID = obj.stdout.read()
        VM2_ID = VM2_ID.split()[0]
        print ("第一台虚拟机%s的ID 是%s") % (VM2_IP,VM2_ID)
        #将第一个VIP 关联至网络
        obj = subprocess.Popen(("neutron port-create --fixed-ip ip_address=%s da8e2dca-aa88-40fa-bfec-7fb02c010068"  % (VIP1)),shell=True,stdout=subprocess.PIPE)
        restful = obj.stdout.read()
        print restful
        #将第二个VIP 关联至网络
        obj = subprocess.Popen(("neutron port-create --fixed-ip ip_address=%s  da8e2dca-aa88-40fa-bfec-7fb02c010068" % (VIP2)),shell=True,stdout=subprocess.PIPE)
        restful = obj.stdout.read()
        print restful
        #将第一个虚拟机关联至两个VIP
        obj = subprocess.Popen(("neutron  port-update %s  --allowed_address_pairs list=true type=dict ip_address={%s,%s}" % (VM1_ID,VIP1,VIP2)),shell=True,stdout=subprocess.PIPE)
        restful = obj.stdout.read()
        print restful
        print  ("第一台虚拟机%s已经关联至VIP: %s和%s") % (VM1_IP,VIP1,VIP2)
        #将第二个虚拟机关联至两个VIP
        obj = subprocess.Popen(("neutron  port-update %s  --allowed_address_pairs list=true type=dict ip_address={%s,%s}" % (VM2_ID,VIP1,VIP2)),shell=True,stdout=subprocess.PIPE)
        restful = obj.stdout.read()
        print restful
        print  ("第二台虚拟机%s已经关联至VIP: %s和%s") % (VM2_IP,VIP1,VIP2) 

  

  

posted @ 2020-01-08 14:26  一叶知秋~~  阅读(589)  评论(0编辑  收藏  举报