[Openstack] 使用heat模板创建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) 鏡像?
? ?(2) flavor?
~$ nova flavor-list | egrep 'ID|flavor_8vC16M' | ID | Name | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public | | 8cb21666-44da-4407-ad8f-0fecb93f93ca | TESTflavor_8vC16M | 16384 | 310 | 0 | | 8 | 1.0 | True | ~$ ~$ nova flavor-show TESTflavor_8vC16M +----------------------------+---------------------------------------------------------------+ | Property | Value | +----------------------------+---------------------------------------------------------------+ | OS-FLV-DISABLED:disabled | False | | OS-FLV-EXT-DATA:ephemeral | 0 | | disk | 310 | | extra_specs | {"hw:cpu_policy": "dedicated", "hw:mem_page_size": "1048576"} | | id | 8cb21666-44da-4407-ad8f-0fecb93f93ca | | name | TESTflavor_8vC16M | | os-flavor-access:is_public | True | | ram | 16384 | | rxtx_factor | 1.0 | | swap | | | vcpus | 8 | +----------------------------+---------------------------------------------------------------+ ~$? (3) 可用區
~$ openstack availability zone list | egrep 'Zone Name|ZONE' | Zone Name ? ? ? ? ? ? ? ? | Zone Status | | XX-TEST-ZONE ? ? ? ? ? ? ?| available ? | ~$? ??(4) 安全組
~$ openstack security group list | egrep 'ID|default' | ID ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | Name ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| Description ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | Project ? ? ? ? ? ? ? ? ? ? ? ? ?| | 0546b666-9dce-40aa-94b9-0470bcc35e4f | default ? ? ? ? ? ? ? ? ? ? ? ? ? ? | Default security group ? ? ? ? ? ? ? ? ? ? ? ? ?| 7849b4a666694e666ac52095f38ed666 | ~$ ~$ openstack security group show default +-------------+----------------------------------------------------------------------------+ | Field ? ? ? | Value ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| +-------------+----------------------------------------------------------------------------+ | description | Default security group ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | | id ? ? ? ? ?| 0546b666-9dce-40aa-94b9-0470bcc35e4f ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | | name ? ? ? ?| default ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| | project_id ?| 7849b4a666694e666ac52095f38ed666 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | | rules ? ? ? | id='3989d666-9a35-42a6-bd99-9e702f7d9032', remote_security_group='default' | | ? ? ? ? ? ? | id='f08deaaa-5d20-4078-979f-69cd95a26f36', remote_security_group='default' | +-------------+----------------------------------------------------------------------------+ ~$(5) 網絡
===> 查看網絡
~$ neutron net-list | egrep 'id|3a190666' | id | name | subnets | | 3a190666-bee3-4e5e-b228-1e32a83ab70b | XX_Net | 3a27faaa-823d-454a-ab4c-24f0ce41e2ad 10.166.166.192/27 | ~$===> 查看子網,記錄網關和子網掩碼
~$ neutron subnet-show 3a27faaa-823d-454a-ab4c-24f0ce41e2ad +-------------------+------------------------------------------------------+ | Field | Value | +-------------------+------------------------------------------------------+ | allocation_pools | {"start": "10.166.166.200", "end": "10.166.166.220"} | | cidr | 10.166.166.192/27 | | created_at | 2020-12-03T02:26:33 | | description | | | dns_nameservers | | | enable_dhcp | True | | gateway_ip | 10.166.166.193 | | host_routes | | | id | 3a27faaa-823d-454a-ab4c-24f0ce41e2ad | | ip_version | 4 | | ipv6_address_mode | | | ipv6_ra_mode | | | name | XXNet_SubNet | | network_id | 3a190666-bee3-4e5e-b228-1e32a83ab70b | | subnetpool_id | | | tenant_id | 7849b4a666694e666ac52095f38ed666 | | updated_at | 2020-12-03T02:26:33 | +-------------------+------------------------------------------------------+ ~$===> 創建PORT
確認PORT名和地址未被使用
~$ neutron port-list | grep TEST-ETH0 ~$ neutron port-list | grep 10.166.166.212 ~$創建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.
~$ neutron port-create --name TEST-ETH0 3a190666-bee3-4e5e-b228-1e32a83ab70b Created a new port: +-----------------------+---------------------------------------------------------------------------------------+ | Field | Value | +-----------------------+---------------------------------------------------------------------------------------+ | admin_state_up | True | | allowed_address_pairs | | | binding:host_id | | | binding:profile | {} | | binding:vif_details | {} | | binding:vif_type | unbound | | binding:vnic_type | normal | | created_at | 2021-07-30T07:31:26 | | description | | | device_id | | | device_owner | | | extra_dhcp_opts | | | fixed_ips | {"subnet_id": "3a27faaa-823d-454a-ab4c-24f0ce41e2ad", "ip_address": "10.166.166.209"} | | id | 612ab666-f843-4343-9b0c-a295ae4bd850 | | mac_address | fa:16:3e:de:30:24 | | name | TEST-ETH0 | | network_id | 3a190666-bee3-4e5e-b228-1e32a83ab70b | | port_security_enabled | True | | qos_policy_id | | | security_groups | 0546b666-9dce-40aa-94b9-0470bcc35e4f | | status | DOWN | | tenant_id | 7849b4a666694e666ac52095f38ed666 | | updated_at | 2021-07-30T07:31:26 | +-----------------------+---------------------------------------------------------------------------------------+ ~$(6)確認未使用的server,stack名稱
~$ openstack server list --all-projects | grep TEST-VM ~$ openstack stack list | grep TEST-STACK ~$2.傳入參數的yaml文件
$ cat IPv4-VM.yaml parameters:network_type: IPv4_OnlyVM_servername: TEST-VMVM_instance-name: TEST-VMroot-image-uuid: 5d6e5666-86c5-4eb1-a937-fcbc41172c04flavor_name: TESTflavor_8vC16Mavailability-zone: XX-TEST-ZONEsecurity-group-uuid: 0546b666-9dce-40aa-94b9-0470bcc35e4feth0_ipv4_address: 10.166.166.212eth0_ipv4_prefix: 27eth0_ipv4_gateway: 10.166.166.193eth0_ipv4_network_subnet_uuid: 3a27faaa-823d-454a-ab4c-24f0ce41e2adeth0_network_uuid: 3a190666-bee3-4e5e-b228-1e32a83ab70beth0_port_name: TEST-ETH0 ~$說明:
參考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模板文件
~$ cat Heat-Orchestration-Template-example.yaml heat_template_version: "2016-10-14" parameters:network_type:description: "The network deployment type. Supports IPv4 only, IPv6 only and dual stack."type: stringVM_servername:description: "The hostname for TEST VM."type: stringflavor_name:description: "The flavor name for TEST VM."type: stringavailability-zone:description: "The availability zone name for TEST VM in Openstack."type: stringroot-image-uuid:description: "The image UUID for TEST."type: stringsecurity-group-uuid:description: "The security group UUID for TEST VM."type: stringeth0_ipv4_address:description: "The eth0 network IPv4 address for TEST VM."type: stringdefault: ""eth0_ipv4_prefix:description: "The eth0 network IPv4 prefix for TEST VM."type: stringdefault: ""eth0_ipv4_gateway:description: "The eth0 network IPv4 gateway for TEST VM."type: stringdefault: ""eth0_ipv6_address:description: "The eth0 network IPv6 address for TEST VM."type: stringdefault: ""eth0_ipv6_prefix:description: "The eth0 network IPv6 prefix for TEST VM."type: stringdefault: ""eth0_ipv6_gateway:description: "The eth0 network IPv6 gateway for TEST VM."type: stringdefault: ""eth0_network_uuid:description: "The eth0 network UUID in Openstack."type: stringeth0_ipv4_network_subnet_uuid:description: "The eth0 network IPv4 subnet UUID in Openstack."type: stringdefault: ""eth0_ipv6_network_subnet_u:uid:description: "The eth0 network IPv6 subnet UUID in Openstack."type: stringdefault: ""eth0_port_name:description: "The eth0 network port name for TEST VM in Openstack."type: stringVM_instance-name:description: "The TEST instance name displayed in Openstack."type: string conditions:ipv4_only:equals:- get_param: network_type- "IPv4_Only"ipv6_only:equals:- get_param: network_type- "IPv6_Only"dual_stack:equals:- get_param: network_type- "Dual_Stack" resources:VM_Port_v4:type: OS::Neutron::Portcondition: ipv4_onlyproperties:fixed_ips: [{"ip_address": { get_param: eth0_ipv4_address }, "subnet": { get_param: eth0_ipv4_network_subnet_uuid }}]security_groups: [ get_param: security-group-uuid ]name: { get_param: eth0_port_name }network: { get_param: eth0_network_uuid }VM_Port_v6:type: OS::Neutron::Portcondition: ipv6_onlyproperties:fixed_ips: [{"ip_address": { get_param: eth0_ipv6_address }, "subnet": { get_param: eth0_ipv6_network_subnet_uuid }}]security_groups: [ get_param: security-group-uuid ]name: { get_param: eth0_port_name }network: { get_param: eth0_network_uuid }VM_Port_dual:type: OS::Neutron::Portcondition: dual_stackproperties: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 }}]security_groups: [ get_param: security-group-uuid ]name: { get_param: eth0_port_name }network: { get_param: eth0_network_uuid }VM_net_data:type: OS::Heat::MultipartMimeproperties:parts:- config:str_replace:params:_ETH0_IPV4_: { get_param: eth0_ipv4_address }_ETH0_IPV4_PREFIX_: { get_param: eth0_ipv4_prefix }_ETH0_IPV4_GATEWAY_: { get_param: eth0_ipv4_gateway }_ETH0_IPV6_: {get_param: eth0_ipv6_address}_ETH0_IPV6_PREFIX_: { get_param: eth0_ipv6_prefix }_ETH0_IPV6_GATEWAY_: { get_param: eth0_ipv6_gateway }_VM_HOSTNAME_: { get_param: VM_servername }template: |#cloud-configmerge_how: 'list(append)+dict(recurse_array,no_replace)+str()'write_files:- path: /run/cloud-init/update_network.showner: root:rootpermissions: '0700'content : |#!/bin/bashsed -i '/^PasswordAuthentication/ s/no/yes/' /etc/ssh/sshd_configsed -i '/^PermitRootLogin/ s/no/yes/' /etc/ssh/sshd_configsystemctl restart sshdsed -i 's/DHCPV6C=yes/DHCPV6C=no/g' /etc/sysconfig/network-scripts/ifcfg-eth0sed -i 's/dhcp/static/g' /etc/sysconfig/network-scripts/ifcfg-eth0if [ _ETH0_IPV4_ != "" ];thenecho IPADDR=_ETH0_IPV4_ >> /etc/sysconfig/network-scripts/ifcfg-eth0echo PREFIX=_ETH0_IPV4_PREFIX_ >> /etc/sysconfig/network-scripts/ifcfg-eth0echo GATEWAY=_ETH0_IPV4_GATEWAY_ >> /etc/sysconfig/network-scripts/ifcfg-eth0fiif [ _ETH0_IPV6_ != "" ];thenecho IPV6INIT=yes >> /etc/sysconfig/network-scripts/ifcfg-eth0echo IPV6ADDR=_ETH0_IPV6_/_ETH0_IPV6_PREFIX_ >> /etc/sysconfig/network-scripts/ifcfg-eth0echo IPV6_DEFAULTGW=_ETH0_IPV6_GATEWAY_ >> /etc/sysconfig/network-scripts/ifcfg-eth0echo IPV6_AUTOCONF=no >> /etc/sysconfig/network-scripts/ifcfg-eth0fisystemctl restart networkruncmd:- /run/cloud-init/update_network.sh- hostnamectl set-hostname _VM_HOSTNAME_VM_Server:type: OS::Nova::Serverproperties:networks:- 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}, ""]}]}]}name: { get_param: VM_instance-name }config_drive: trueflavor: { get_param: flavor_name }availability_zone: { get_param: availability-zone }image: { get_param: root-image-uuid }user_data: { get_resource: VM_net_data }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
~$ openstack --debug stack create -t Heat-Orchestration-Template-example.yaml -e IPv4-VM.yaml TEST-STACK --dry-run …… END return value: 0執行如下命令創建stack,成功創建的話返回參數為0
~$ openstack --debug stack create -t Heat-Orchestration-Template-example.yaml -e IPv4-VM.yaml TEST-STACK --wait …… +---------------------+--------------------------------------+ | Field | Value | +---------------------+--------------------------------------+ | id | 7aba7666-e05a-4f6d-a7f1-93935284c212 | | stack_name | TEST-STACK | | description | No description | | creation_time | 2021-07-30T08:30:34Z | | updated_time | None | | stack_status | CREATE_COMPLETE | | stack_status_reason | Stack CREATE completed successfully | +---------------------+--------------------------------------+ clean_up CreateStack: END return value: 0 ~$查看新創建的stack
~$ openstack stack show TEST-STACK +-----------------------+-----------------------------------------------------------------------------------------------------------------------------------+ | Field | Value | +-----------------------+-----------------------------------------------------------------------------------------------------------------------------------+ | id | 7aba7666-e05a-4f6d-a7f1-93935284c212 | | stack_name | TEST-STACK | | description | No description | | creation_time | 2021-07-30T08:30:34Z | | updated_time | None | | stack_status | CREATE_COMPLETE | | stack_status_reason | Stack CREATE completed successfully | | parameters | OS::project_id: 7849b4a666694e666ac52095f38ed666 | | | OS::stack_id: 7aba7666-e05a-4f6d-a7f1-93935284c212 | | | OS::stack_name: TEST-STACK | | | VM_instance-name: TEST-VM | | | VM_servername: TEST-VM | | | availability-zone: XX-TEST-ZONE | | | eth0_ipv4_address: 10.166.166.212 | | | eth0_ipv4_gateway: 10.166.166.193 | | | eth0_ipv4_network_subnet_uuid: 3a27faaa-823d-454a-ab4c-24f0ce41e2ad | | | eth0_ipv4_prefix: '27' | | | eth0_ipv6_address: '' | | | eth0_ipv6_gateway: '' | | | eth0_ipv6_network_subnet_u:uid: '' | | | eth0_ipv6_prefix: '' | | | eth0_network_uuid: 3a190666-bee3-4e5e-b228-1e32a83ab70b | | | eth0_port_name: TEST-ETH0 | | | flavor_name: TESTflavor_8vC16M | | | network_type: IPv4_Only | | | root-image-uuid: 5d6e5666-86c5-4eb1-a937-fcbc41172c04 | | | security-group-uuid: 0546b666-9dce-40aa-94b9-0470bcc35e4f | | | | | outputs | [] | | | | | links | - href: https://[2120:0:0:bfe::3]:8004/v1/7849b4a666694e666ac52095f38ed666/stacks/TEST-STACK/7aba7666-e05a-4f6d-a7f1-93935284c212 | | | rel: self | | | | | parent | None | | disable_rollback | True | | deletion_time | None | | stack_user_project_id | aaac4c521ec042e88588469126034d2c | | capabilities | [] | | notification_topics | [] | | stack_owner | None | | timeout_mins | None | | tags | null | | | ... | | | | +-----------------------+-----------------------------------------------------------------------------------------------------------------------------------+ ~$查看stack中的VM
$ nova list | egrep 'ID|TEST-VM' | ID | Name | Status | Task State | Power State | Networks | | dab59666-0762-4a1b-989f-4ddf94239679 | TEST-VM | ACTIVE | - | Running | XX_Net=10.166.166.212 $ nova show TEST-VM …… $(END)
官方文檔參考:
 [1] cloud-init =>?https://cloudinit.readthedocs.io/en/latest/
 [2] openstack =>?https://docs.openstack.org/heat/latest/template_guide/index.html
 ?
總結
以上是生活随笔為你收集整理的[Openstack] 使用heat模板创建stack的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 【python】Flask视图
- 下一篇: 计算机打印服务总是自动关闭,XP系统Pr
