当前位置:   article > 正文

[Openstack] 使用heat模板创建stack_openstack stack 新建栈

openstack stack 新建栈

本例需要准备两个yaml文件,一个是heat模板,一个是传入的参数,然后执行如下命令创建stack

openstack --debug stack create -t <heat_template_yaml> -e <env_yaml> <stack_name> --wait

Note for arguments:

--debug               Show tracebacks on errors.

-t <template>, --template <template>
                      Path to the template

-e <environment>, --environment <environment>
                      Path to the environment. Can be specified multiple times

--dry-run             Do not actually perform the stack create, but show what would be created

--wait                Wait until stack goes to CREATE_COMPLETE or CREATE_FAILED
 

本文包括4个部分

- 资源准备
- 传入参数的yaml文件
- Heat模板文件
- 创建stack

1.资源准备(此处假设资源已齐全,仅需要记录一些参数)
(1) 镜像 

  1. ~$ nova image-list | egrep 'ID|CentOS7-TEST'
  2. | ID | Name | Status | Server |
  3. | 5d6e5666-86c5-4eb1-a937-fcbc41172c04 | CentOS7-TEST | ACTIVE | |
  4. ~$

   (2) flavor 

  1. ~$ nova flavor-list | egrep 'ID|flavor_8vC16M'
  2. | ID | Name | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public |
  3. | 8cb21666-44da-4407-ad8f-0fecb93f93ca | TESTflavor_8vC16M | 16384 | 310 | 0 | | 8 | 1.0 | True |
  4. ~$
  5. ~$ nova flavor-show TESTflavor_8vC16M
  6. +----------------------------+---------------------------------------------------------------+
  7. | Property | Value |
  8. +----------------------------+---------------------------------------------------------------+
  9. | OS-FLV-DISABLED:disabled | False |
  10. | OS-FLV-EXT-DATA:ephemeral | 0 |
  11. | disk | 310 |
  12. | extra_specs | {"hw:cpu_policy": "dedicated", "hw:mem_page_size": "1048576"} |
  13. | id | 8cb21666-44da-4407-ad8f-0fecb93f93ca |
  14. | name | TESTflavor_8vC16M |
  15. | os-flavor-access:is_public | True |
  16. | ram | 16384 |
  17. | rxtx_factor | 1.0 |
  18. | swap | |
  19. | vcpus | 8 |
  20. +----------------------------+---------------------------------------------------------------+
  21. ~$

  (3) 可用区

  1. ~$ openstack availability zone list | egrep 'Zone Name|ZONE'
  2. | Zone Name                 | Zone Status |
  3. | XX-TEST-ZONE              | available   |
  4. ~  

 (4) 安全组

  1. ~$ openstack security group list | egrep 'ID|default'
  2. | ID                                   | Name                                | Description                                     | Project                          |
  3. | 0546b666-9dce-40aa-94b9-0470bcc35e4f | default                             | Default security group                          | 7849b4a666694e666ac52095f38ed666 |
  4. ~$
  5. ~$ openstack security group show default
  6. +-------------+----------------------------------------------------------------------------+
  7. | Field       | Value                                                                      |
  8. +-------------+----------------------------------------------------------------------------+
  9. | description | Default security group                                                     |
  10. | id          | 0546b666-9dce-40aa-94b9-0470bcc35e4f                                       |
  11. | name        | default                                                                    |
  12. | project_id  | 7849b4a666694e666ac52095f38ed666                                           |
  13. | rules       | id='3989d666-9a35-42a6-bd99-9e702f7d9032', remote_security_group='default' |
  14. |             | id='f08deaaa-5d20-4078-979f-69cd95a26f36', remote_security_group='default' |
  15. +-------------+----------------------------------------------------------------------------+
  16. ~$

(5) 网络

===> 查看网络

  1. ~$ neutron net-list | egrep 'id|3a190666'
  2. | id | name | subnets |
  3. | 3a190666-bee3-4e5e-b228-1e32a83ab70b | XX_Net | 3a27faaa-823d-454a-ab4c-24f0ce41e2ad 10.166.166.192/27 |
  4. ~$

===> 查看子网,记录网关和子网掩码

  1. ~$ neutron subnet-show 3a27faaa-823d-454a-ab4c-24f0ce41e2ad
  2. +-------------------+------------------------------------------------------+
  3. | Field | Value |
  4. +-------------------+------------------------------------------------------+
  5. | allocation_pools | {"start": "10.166.166.200", "end": "10.166.166.220"} |
  6. | cidr | 10.166.166.192/27 |
  7. | created_at | 2020-12-03T02:26:33 |
  8. | description | |
  9. | dns_nameservers | |
  10. | enable_dhcp | True |
  11. | gateway_ip | 10.166.166.193 |
  12. | host_routes | |
  13. | id | 3a27faaa-823d-454a-ab4c-24f0ce41e2ad |
  14. | ip_version | 4 |
  15. | ipv6_address_mode | |
  16. | ipv6_ra_mode | |
  17. | name | XXNet_SubNet |
  18. | network_id | 3a190666-bee3-4e5e-b228-1e32a83ab70b |
  19. | subnetpool_id | |
  20. | tenant_id | 7849b4a666694e666ac52095f38ed666 |
  21. | updated_at | 2020-12-03T02:26:33 |
  22. +-------------------+------------------------------------------------------+
  23. ~$

===> 创建PORT

确认PORT名和地址未被使用

  1. ~$ neutron port-list | grep TEST-ETH0
  2. ~$ neutron port-list | grep 10.166.166.212
  3. ~$

创建PORT,仅设定PORT名称
neutron port-create [--name NAME] <net_id>

注意:不要加参数[--fixed-ip subnet_id=SUBNET,ip_address=IP_ADDR],之后传入的env会传入地址,否则创建时会有地址冲突 ===> [TEST-STACK]: CREATE_FAILED  Resource CREATE failed: IpAddressInUseClient: resources.VM_Port_1_v4: Unable to complete operation for network 3a1904e1-bee3-4e5e-b228-1e32a83ab70b. The IP address 10.166.166.212 is in use.

  1. ~$ neutron port-create --name TEST-ETH0 3a190666-bee3-4e5e-b228-1e32a83ab70b
  2. Created a new port:
  3. +-----------------------+---------------------------------------------------------------------------------------+
  4. | Field | Value |
  5. +-----------------------+---------------------------------------------------------------------------------------+
  6. | admin_state_up | True |
  7. | allowed_address_pairs | |
  8. | binding:host_id | |
  9. | binding:profile | {} |
  10. | binding:vif_details | {} |
  11. | binding:vif_type | unbound |
  12. | binding:vnic_type | normal |
  13. | created_at | 2021-07-30T07:31:26 |
  14. | description | |
  15. | device_id | |
  16. | device_owner | |
  17. | extra_dhcp_opts | |
  18. | fixed_ips | {"subnet_id": "3a27faaa-823d-454a-ab4c-24f0ce41e2ad", "ip_address": "10.166.166.209"} |
  19. | id | 612ab666-f843-4343-9b0c-a295ae4bd850 |
  20. | mac_address | fa:16:3e:de:30:24 |
  21. | name | TEST-ETH0 |
  22. | network_id | 3a190666-bee3-4e5e-b228-1e32a83ab70b |
  23. | port_security_enabled | True |
  24. | qos_policy_id | |
  25. | security_groups | 0546b666-9dce-40aa-94b9-0470bcc35e4f |
  26. | status | DOWN |
  27. | tenant_id | 7849b4a666694e666ac52095f38ed666 |
  28. | updated_at | 2021-07-30T07:31:26 |
  29. +-----------------------+---------------------------------------------------------------------------------------+
  30. ~$

(6)确认未使用的server,stack名称

  1. ~$ openstack server list --all-projects | grep TEST-VM
  2. ~$ openstack stack list | grep TEST-STACK
  3. ~$

2.传入参数的yaml文件

  1. $ cat IPv4-VM.yaml
  2. parameters:
  3. network_type: IPv4_Only
  4. VM_servername: TEST-VM
  5. VM_instance-name: TEST-VM
  6. root-image-uuid: 5d6e5666-86c5-4eb1-a937-fcbc41172c04
  7. flavor_name: TESTflavor_8vC16M
  8. availability-zone: XX-TEST-ZONE
  9. security-group-uuid: 0546b666-9dce-40aa-94b9-0470bcc35e4f
  10. eth0_ipv4_address: 10.166.166.212
  11. eth0_ipv4_prefix: 27
  12. eth0_ipv4_gateway: 10.166.166.193
  13. eth0_ipv4_network_subnet_uuid: 3a27faaa-823d-454a-ab4c-24f0ce41e2ad
  14. eth0_network_uuid: 3a190666-bee3-4e5e-b228-1e32a83ab70b
  15. eth0_port_name: TEST-ETH0
  16. ~$

说明:

参考1(6) VM_instance-name
参考1(1) root-image-uuid
参考1(2) flavor_name
参考1(3) availability-zone
参考1(4) security-group-uuid
参考1(5) eth0_ipv4_address
参考1(5) eth0_ipv4_prefix
参考1(5) eth0_ipv4_gateway
参考1(5) eth0_ipv4_network_subnet_uuid
参考1(5) eth0_network_uuid 
参考1(5) eth0_port_name: TEST-ETH0

3. Heat模板文件

  1. ~$ cat Heat-Orchestration-Template-example.yaml
  2. heat_template_version: "2016-10-14"
  3. parameters:
  4. network_type:
  5. description: "The network deployment type. Supports IPv4 only, IPv6 only and dual stack."
  6. type: string
  7. VM_servername:
  8. description: "The hostname for TEST VM."
  9. type: string
  10. flavor_name:
  11. description: "The flavor name for TEST VM."
  12. type: string
  13. availability-zone:
  14. description: "The availability zone name for TEST VM in Openstack."
  15. type: string
  16. root-image-uuid:
  17. description: "The image UUID for TEST."
  18. type: string
  19. security-group-uuid:
  20. description: "The security group UUID for TEST VM."
  21. type: string
  22. eth0_ipv4_address:
  23. description: "The eth0 network IPv4 address for TEST VM."
  24. type: string
  25. default: ""
  26. eth0_ipv4_prefix:
  27. description: "The eth0 network IPv4 prefix for TEST VM."
  28. type: string
  29. default: ""
  30. eth0_ipv4_gateway:
  31. description: "The eth0 network IPv4 gateway for TEST VM."
  32. type: string
  33. default: ""
  34. eth0_ipv6_address:
  35. description: "The eth0 network IPv6 address for TEST VM."
  36. type: string
  37. default: ""
  38. eth0_ipv6_prefix:
  39. description: "The eth0 network IPv6 prefix for TEST VM."
  40. type: string
  41. default: ""
  42. eth0_ipv6_gateway:
  43. description: "The eth0 network IPv6 gateway for TEST VM."
  44. type: string
  45. default: ""
  46. eth0_network_uuid:
  47. description: "The eth0 network UUID in Openstack."
  48. type: string
  49. eth0_ipv4_network_subnet_uuid:
  50. description: "The eth0 network IPv4 subnet UUID in Openstack."
  51. type: string
  52. default: ""
  53. eth0_ipv6_network_subnet_u:uid:
  54. description: "The eth0 network IPv6 subnet UUID in Openstack."
  55. type: string
  56. default: ""
  57. eth0_port_name:
  58. description: "The eth0 network port name for TEST VM in Openstack."
  59. type: string
  60. VM_instance-name:
  61. description: "The TEST instance name displayed in Openstack."
  62. type: string
  63. conditions:
  64. ipv4_only:
  65. equals:
  66. - get_param: network_type
  67. - "IPv4_Only"
  68. ipv6_only:
  69. equals:
  70. - get_param: network_type
  71. - "IPv6_Only"
  72. dual_stack:
  73. equals:
  74. - get_param: network_type
  75. - "Dual_Stack"
  76. resources:
  77. VM_Port_v4:
  78. type: OS::Neutron::Port
  79. condition: ipv4_only
  80. properties:
  81. fixed_ips: [{"ip_address": { get_param: eth0_ipv4_address }, "subnet": { get_param: eth0_ipv4_network_subnet_uuid }}]
  82. security_groups: [ get_param: security-group-uuid ]
  83. name: { get_param: eth0_port_name }
  84. network: { get_param: eth0_network_uuid }
  85. VM_Port_v6:
  86. type: OS::Neutron::Port
  87. condition: ipv6_only
  88. properties:
  89. fixed_ips: [{"ip_address": { get_param: eth0_ipv6_address }, "subnet": { get_param: eth0_ipv6_network_subnet_uuid }}]
  90. security_groups: [ get_param: security-group-uuid ]
  91. name: { get_param: eth0_port_name }
  92. network: { get_param: eth0_network_uuid }
  93. VM_Port_dual:
  94. type: OS::Neutron::Port
  95. condition: dual_stack
  96. properties:
  97. fixed_ips: [{"ip_address": { get_param: eth0_ipv4_address }, "subnet": { get_param: eth0_ipv4_network_subnet_uuid }}, {"ip_address": { get_param: eth0_ipv6_address }, "subnet": { get_param: eth0_ipv6_network_subnet_uuid }}]
  98. security_groups: [ get_param: security-group-uuid ]
  99. name: { get_param: eth0_port_name }
  100. network: { get_param: eth0_network_uuid }
  101. VM_net_data:
  102. type: OS::Heat::MultipartMime
  103. properties:
  104. parts:
  105. - config:
  106. str_replace:
  107. params:
  108. _ETH0_IPV4_: { get_param: eth0_ipv4_address }
  109. _ETH0_IPV4_PREFIX_: { get_param: eth0_ipv4_prefix }
  110. _ETH0_IPV4_GATEWAY_: { get_param: eth0_ipv4_gateway }
  111. _ETH0_IPV6_: {get_param: eth0_ipv6_address}
  112. _ETH0_IPV6_PREFIX_: { get_param: eth0_ipv6_prefix }
  113. _ETH0_IPV6_GATEWAY_: { get_param: eth0_ipv6_gateway }
  114. _VM_HOSTNAME_: { get_param: VM_servername }
  115. template: |
  116. #cloud-config
  117. merge_how: 'list(append)+dict(recurse_array,no_replace)+str()'
  118. write_files:
  119. - path: /run/cloud-init/update_network.sh
  120. owner: root:root
  121. permissions: '0700'
  122. content : |
  123. #!/bin/bash
  124. sed -i '/^PasswordAuthentication/ s/no/yes/' /etc/ssh/sshd_config
  125. sed -i '/^PermitRootLogin/ s/no/yes/' /etc/ssh/sshd_config
  126. systemctl restart sshd
  127. sed -i 's/DHCPV6C=yes/DHCPV6C=no/g' /etc/sysconfig/network-scripts/ifcfg-eth0
  128. sed -i 's/dhcp/static/g' /etc/sysconfig/network-scripts/ifcfg-eth0
  129. if [ _ETH0_IPV4_ != "" ];then
  130. echo IPADDR=_ETH0_IPV4_ >> /etc/sysconfig/network-scripts/ifcfg-eth0
  131. echo PREFIX=_ETH0_IPV4_PREFIX_ >> /etc/sysconfig/network-scripts/ifcfg-eth0
  132. echo GATEWAY=_ETH0_IPV4_GATEWAY_ >> /etc/sysconfig/network-scripts/ifcfg-eth0
  133. fi
  134. if [ _ETH0_IPV6_ != "" ];then
  135. echo IPV6INIT=yes >> /etc/sysconfig/network-scripts/ifcfg-eth0
  136. echo IPV6ADDR=_ETH0_IPV6_/_ETH0_IPV6_PREFIX_ >> /etc/sysconfig/network-scripts/ifcfg-eth0
  137. echo IPV6_DEFAULTGW=_ETH0_IPV6_GATEWAY_ >> /etc/sysconfig/network-scripts/ifcfg-eth0
  138. echo IPV6_AUTOCONF=no >> /etc/sysconfig/network-scripts/ifcfg-eth0
  139. fi
  140. systemctl restart network
  141. runcmd:
  142. - /run/cloud-init/update_network.sh
  143. - hostnamectl set-hostname _VM_HOSTNAME_
  144. VM_Server:
  145. type: OS::Nova::Server
  146. properties:
  147. networks:
  148. - port: {if: [ipv4_only, {get_resource: VM_Port_v4}, {if: [ipv6_only, {get_resource: VM_Port_v6}, {if: [dual_stack, {get_resource: VM_Port_dual}, ""]}]}]}
  149. name: { get_param: VM_instance-name }
  150. config_drive: true
  151. flavor: { get_param: flavor_name }
  152. availability_zone: { get_param: availability-zone }
  153. image: { get_param: root-image-uuid }
  154. user_data: { get_resource: VM_net_data }
  155. user_data_format: RAW

说明: 以下为个人理解,具体参考官方文档[2]

line2 parameters部分定义参数,这些参数由"-e"后的文件传入,其中network_type支持IPv4、IPv6和双栈

line62 conditions部分定义条件,network_type等于IPv4_Only(第2部分IPv4-VM.yaml中network_type的值),符合条件ipv4_only

line75 resources部分前三个资源(type: OS::Neutron::Port)中选择满足条件condition: ipv4_only的PORT,即VM_Port_v4

line100 resources部分的资源VM_net_data, line106 params传入参数; line114 template 使用cloud-init初始化VM(参考官方文档[1]),修改ssh配置文件使root可以ssh登录,添加了网卡信息,修改主机名,这样就不用等创建好VM再去console里配置网络

line143 resources部分的资源VM_Server,网络有一个网卡,如果需要多个网卡可以添加多个port字段,同时添加OS::Neutron::Port资源以及传入的参数

4.创建stack

使用dry run测试yaml文件正确配置,返回参数为0

  1. ~$ openstack --debug stack create -t Heat-Orchestration-Template-example.yaml -e IPv4-VM.yaml TEST-STACK --dry-run
  2. ……
  3. END return value: 0

执行如下命令创建stack,成功创建的话返回参数为0

  1. ~$ openstack --debug stack create -t Heat-Orchestration-Template-example.yaml -e IPv4-VM.yaml TEST-STACK --wait
  2. ……
  3. +---------------------+--------------------------------------+
  4. | Field | Value |
  5. +---------------------+--------------------------------------+
  6. | id | 7aba7666-e05a-4f6d-a7f1-93935284c212 |
  7. | stack_name | TEST-STACK |
  8. | description | No description |
  9. | creation_time | 2021-07-30T08:30:34Z |
  10. | updated_time | None |
  11. | stack_status | CREATE_COMPLETE |
  12. | stack_status_reason | Stack CREATE completed successfully |
  13. +---------------------+--------------------------------------+
  14. clean_up CreateStack:
  15. END return value: 0
  16. ~$

查看新创建的stack

  1. ~$ openstack stack show TEST-STACK
  2. +-----------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  3. | Field | Value |
  4. +-----------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  5. | id | 7aba7666-e05a-4f6d-a7f1-93935284c212 |
  6. | stack_name | TEST-STACK |
  7. | description | No description |
  8. | creation_time | 2021-07-30T08:30:34Z |
  9. | updated_time | None |
  10. | stack_status | CREATE_COMPLETE |
  11. | stack_status_reason | Stack CREATE completed successfully |
  12. | parameters | OS::project_id: 7849b4a666694e666ac52095f38ed666 |
  13. | | OS::stack_id: 7aba7666-e05a-4f6d-a7f1-93935284c212 |
  14. | | OS::stack_name: TEST-STACK |
  15. | | VM_instance-name: TEST-VM |
  16. | | VM_servername: TEST-VM |
  17. | | availability-zone: XX-TEST-ZONE |
  18. | | eth0_ipv4_address: 10.166.166.212 |
  19. | | eth0_ipv4_gateway: 10.166.166.193 |
  20. | | eth0_ipv4_network_subnet_uuid: 3a27faaa-823d-454a-ab4c-24f0ce41e2ad |
  21. | | eth0_ipv4_prefix: '27' |
  22. | | eth0_ipv6_address: '' |
  23. | | eth0_ipv6_gateway: '' |
  24. | | eth0_ipv6_network_subnet_u:uid: '' |
  25. | | eth0_ipv6_prefix: '' |
  26. | | eth0_network_uuid: 3a190666-bee3-4e5e-b228-1e32a83ab70b |
  27. | | eth0_port_name: TEST-ETH0 |
  28. | | flavor_name: TESTflavor_8vC16M |
  29. | | network_type: IPv4_Only |
  30. | | root-image-uuid: 5d6e5666-86c5-4eb1-a937-fcbc41172c04 |
  31. | | security-group-uuid: 0546b666-9dce-40aa-94b9-0470bcc35e4f |
  32. | | |
  33. | outputs | [] |
  34. | | |
  35. | links | - href: https://[2120:0:0:bfe::3]:8004/v1/7849b4a666694e666ac52095f38ed666/stacks/TEST-STACK/7aba7666-e05a-4f6d-a7f1-93935284c212 |
  36. | | rel: self |
  37. | | |
  38. | parent | None |
  39. | disable_rollback | True |
  40. | deletion_time | None |
  41. | stack_user_project_id | aaac4c521ec042e88588469126034d2c |
  42. | capabilities | [] |
  43. | notification_topics | [] |
  44. | stack_owner | None |
  45. | timeout_mins | None |
  46. | tags | null |
  47. | | ... |
  48. | | |
  49. +-----------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  50. ~$

查看stack中的VM

  1. $ nova list | egrep 'ID|TEST-VM'
  2. | ID | Name | Status | Task State | Power State | Networks |
  3. | dab59666-0762-4a1b-989f-4ddf94239679 | TEST-VM | ACTIVE | - | Running | XX_Net=10.166.166.212
  4. $ nova show TEST-VM
  5. ……
  6. $

(END)

官方文档参考:
[1] cloud-init => https://cloudinit.readthedocs.io/en/latest/
[2] openstack => https://docs.openstack.org/heat/latest/template_guide/index.html


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/70541
推荐阅读
相关标签
  

闽ICP备14008679号