openstack python-heatclient使用

前言

heat是对openstack资源的编排工具,编排可以理解为自动化。
在heat中,将一次编排任务称之为stack。创建编排就是创建一个stack。

演示使用 python-heatclient 创建特定需求的虚拟机。
涉及非常多的新知识,这篇文章只能至简的介绍使用。

环境

python2
openstack版本:queens
模板格式:HOT

正文

这里可以拆分为3个部分:

  1. python-heatclient:主体运行程序
  2. yaml文件:定义资源的规格,可以编写简单的逻辑。
  3. 虚拟机执行脚本:虚拟机开机时执行的脚本,这个可以整合在yaml文件中,也可以独立出来。

总体流程:

  1. 准备好配置文件,参数
  2. 创建stack

运行一个实例

  1. 准备yaml文件。
    yaml文件直接引用官方给出的例子,不做任何修改。
    地址:
    https://github.com/openstack/heat-templates/blob/master/hot/servers_in_existing_neutron_net.yaml
    贴出其中的内容:
heat_template_version: 2013-05-23

description: >
  HOT template to deploy two servers into an existing neutron tenant network and
  assign floating IP addresses to each server so they are routable from the
  public network.
parameters:
  key_name:
    type: string
    description: Name of keypair to assign to servers
  image:
    type: string
    description: Name of image to use for servers
  flavor:
    type: string
    description: Flavor to use for servers
  public_net_id:
    type: string
    description: >
      ID of public network for which floating IP addresses will be allocated
  private_net_id:
    type: string
    description: ID of private network into which servers get deployed
  private_subnet_id:
    type: string
    description: ID of private sub network into which servers get deployed

resources:
  server1:
    type: OS::Nova::Server
    properties:
      name: Server1
      image: { get_param: image }
      flavor: { get_param: flavor }
      key_name: { get_param: key_name }
      networks:
        - port: { get_resource: server1_port }

  server1_port:
    type: OS::Neutron::Port
    properties:
      network_id: { get_param: private_net_id }
      fixed_ips:
        - subnet_id: { get_param: private_subnet_id }
      security_groups: [{ get_resource: server_security_group }]

  server1_floating_ip:
    type: OS::Neutron::FloatingIP
    properties:
      floating_network_id: { get_param: public_net_id }
      port_id: { get_resource: server1_port }

  server2:
    type: OS::Nova::Server
    properties:
      name: Server2
      image: { get_param: image }
      flavor: { get_param: flavor }
      key_name: { get_param: key_name }
      networks:
        - port: { get_resource: server2_port }

  server2_port:
    type: OS::Neutron::Port
    properties:
      network_id: { get_param: private_net_id }
      fixed_ips:
        - subnet_id: { get_param: private_subnet_id }
      security_groups: [{ get_resource: server_security_group }]

  server2_floating_ip:
    type: OS::Neutron::FloatingIP
    properties:
      floating_network_id: { get_param: public_net_id }
      port_id: { get_resource: server2_port }

  server_security_group:
    type: OS::Neutron::SecurityGroup
    properties:
      description: Add security group rules for server
      name: security-group
      rules:
        - remote_ip_prefix: 0.0.0.0/0
          protocol: tcp
          port_range_min: 22
          port_range_max: 22
        - remote_ip_prefix: 0.0.0.0/0
          protocol: icmp

outputs:
  server1_private_ip:
    description: IP address of server1 in private network
    value: { get_attr: [ server1, first_address ] }
  server1_public_ip:
    description: Floating IP address of server1 in public network
    value: { get_attr: [ server1_floating_ip, floating_ip_address ] }
  server2_private_ip:
    description: IP address of server2 in private network
    value: { get_attr: [ server2, first_address ] }
  server2_public_ip:
    description: Floating IP address of server2 in public network
    value: { get_attr: [ server2_floating_ip, floating_ip_address ] }

创建一个template文件,将内容完整的复制进去:

vim servers_in_existing_neutron_net.yaml
  1. 创建stack
    这个template文件大体是创建了一个带浮动ip的虚拟机,并且创建了一个安全组绑定到虚拟机端口上。

创建python 程序:
vim client.py

# coding:utf8

from keystoneauth1 import session
from keystoneauth1.identity import v3
from heatclient import client as heat_client

# 注意替换认证信息
auth = v3.Password(
            auth_url="http://192.168.110.10:35357/v3",
            username="admin",
            password="qwe",
            project_id="97eaa64704bd4f549f3abf99a6decdc1",
            user_domain_id='default')
sess = session.Session(auth=auth)


hc = heat_client.Client('1', session=sess)

# 注意替换模板路径
template_path = "./servers_in_existing_neutron_net.yaml"

# 注意替换 镜像等信息。这里的 parameters 就是模板中定义的参数
parameters = {
        "key_name": "be8ee056ea274d45a1fd97bfd36c2775_lzp",
        "image": "cirros",
        "flavor": "cc00d88f-9ed5-4012-9840-faf8add2873d",
        "public_net_id": "4955888e-1622-4a2b-88d7-2c58e092e7fa",
        "private_net_id": "77844f4d-0dd8-4532-bbaa-d17efd5a1895",
        "private_subnet_id": "9a7bfada-fd99-4ac4-a4a0-66e872019bc1"
}

template = open(template_path).read()
stack_name = "test1"

with open(template_path, "r") as f:
        stack = hc.stacks.create(
                stack_name=stack_name,
                template=f.read(),
                parameters=parameters
        )

print(stack)

运行即可。
可以通过命令行查看进度:

openstack stack list
image.png

扩展

outputs

在template中尾部定义了 outputs。用于保留这个stack的某些重要信息。不需要可以不定义。
代码参考:

stack_id = "4d05cb2d-76a0-4ca4-a484-494ddcdcd243"

# 列出output列表
outputs = hc.stacks.output_list(stack_id=stack_id)
print(outputs)

# 显示output中某个键值对
server1_public_ip = hc.stacks.output_show(stack_id=stack_id, output_key="server1_public_ip")
print(server1_public_ip)

参考

模板例子参考:
https://github.com/openstack/heat-templates

模板配置详情参考:
https://docs.openstack.org/heat/rocky/template_guide/hot_spec.html#hot-spec

python-heatclient 参考:
https://github.com/openstack/python-heatclient

heat api 参考:
https://developer.openstack.org/api-ref/orchestration/v1/index.html?expanded=create-stack-detail#stacks

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,525评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,203评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,862评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,728评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,743评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,590评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,330评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,244评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,693评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,885评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,001评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,723评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,343评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,919评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,042评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,191评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,955评论 2 355

推荐阅读更多精彩内容