Heat 相关,Cloud-init & Cloud-config

毛正浩
2023-12-01

cloud-init

在虚拟机启动后执行编排任务, nova boot时指定 user-data 参数

Nova USER DATA

数据获取方式:
- 169.254.169.254
- config drive(In Heat set config_drive: true)

常见使用方式如下:

#nova boot \
    --image ubuntu-trusty-server-amd64
    --key_name testkey \
    --flavor m1.small \
    --user-data userdata.txt \
    --nic net-id=443434-dsx3-2323-dssx-43343434 \
    test

And, userdata.txt

#!/bin/sh -e
#Frobincate a newly booted box
initialize_box
for foo in forbnications; do
    frobincate_machine $foo || break
done

exit $?

But, stop doing this, and use cloud-config below which is better

cloud-config

Enables you to bootstrap a newly bootd VM. OpenStack’s most underrated feature, 100% YAML.

常用功能如下:

更新软件(apt-get update/yum update)

Update system on first boot

#cloud-config
package_update: true
package_upgrade: true

users

Configure users and groups

users:
- default
- name: foobar
  gecos: ""
  groups: users,adm
  lock-passwd: false
  passwd: 23lkjsflkas0923Da
  shell: /bin/bash
  sudo: "ALL=(ALL) NOPASSWD:ALL"

ssh_pwauth

Enable/disable SSH password authentication

ssh_pwauth: true

write_files

Write arbitrary files

write_files:
- path: /etc/demo
  permissions: '0644'
  content: |
    This is a demo

puppet

Config VM’s puppet client

chef

Config VM’s chef client

packages

Install packages

packages:
  - ansible
  - git

Running arbitrary commands

bootcmd

Run commands early in the boot sequence

bootcmd:
  - ntpdate pool.ntp.org
runcmd

Run commands late in boot sequence

runcmd:
  - echo "hello world"
  - ls -hS

使用方式

#nova boot \
    --image ubuntu-trusty-server-amd64
    --key_name testkey \
    --flavor m1.small \
    --user-data config.yaml \
    --nic net-id=443434-dsx3-2323-dssx-43343434 \
    test

cloud-init 日志输出到 /var/log/cloud-init.log

HEAT

Enables you deploy complete virtual environments
- CFN

Amazon CLoudFormation compatible template
- HOT

Heat Orchestration Template, 100% YAML

格式

  • version
  • parameters
  • resources
  • outputs

概念

  • resources (type)
  • outputs:输出

常用函数

  • get_param: 获取动态参数
  • get_resource: 获取依赖的其他resource
  • get_attr: 获取 resource 属性

创建虚拟机

type: “OS::Nova::Server”

resources:
  mybox:
    type: "OS::Nova::Server"
    properties:
      name: mybox
      image: ubuntu-trusty-amd64
      flavor: m1.small
      key_name: testkey

创建heat stack

#heat stack-create -f stack.yaml mystack

get_param 使用

parameters:
   name:
     type: string
     default: 
   flavor:
     type: string
     default: m1.small
   image:
     type: string
     default: ubuntu-trusty-amd64
   key_name:
     type: string
resources:
  mybox
    type:
      properties
        name: { get_param: name }
        image: { get_param: image }
        flavor: { get_param: flavor }
        key_name: { get_param: key_name }

使用方式

#heat stack-create -f stack.yaml \
    -P key_name testkey mystack

get_resource 使用

创建网络

mynet:
  type: "OS::Neutron::Net"
  properties:
    name: management-net
mysub_net
  type: "OS::Neutron::Subnet"
  properties:
    name: management-subnet
    network: { get_resource: management-net }
    cidr: 192.168.101.0/24
    gateway_ip: 192.168.101.1
    enable_dhcp: true
    allocation_pools:
      - start: "192..168.101.2"
        end: "192.168.101.100"

outputs

Return stack values or attributes

outputs:
  public_ip:
    description:
    value: { get_attr: [ myfloating_ip, floating_ip_address ]}

查看方式

#heat output-show mystack public_ip

Integrating HEAT and cloud-init

常用方式

mybox:
  type: "OS::Nova::Server"
  properties:
    name: deploy
    image: { get_param: image }
    flavor: { get_param: flavor }
    key_name: { get_param: key_name }
    networks:
      - port: { get_resource: mybox_management_port }
    user_data: {get_file: cloud-config.yaml }
    user_data_format: RAW

but, there is a better way

OS::Heat::CloudConfig

Manages cloud-config directly from Heat

resources:
  myconfig:
    type: "OS::Heat::CloudConfig"
    properties:
      cloud_config:
        package_update: true
        package_upgrade: true
  mybox:
    type: "OS::Nova::Server"
    properties:
      name: deploy
      image: { get_param: image }
      flavor: { get_param: flavor }
      key_name: { get_param: key_name }
      networks:
        - port: { get_resource: mybox_management_port }
      user_data: {get_resource: myconfig }
      user_data_format: RAW

Also, you can SET cloud-config parameters directly from HEAT, which is nice

parameters:
  # [...]
  username:
    type: string
    description: Additional login name
    default: foobar
  myconfig:
    type: "OS::Heat::CloudConfig"
    properties:
      cloud_config:
        package_update: true
        package_upgrade: true
        users:
        - default
        - name: { get_param: username }
          shell: "/bin/bash"
        ssh_pwauth: true

参考

 类似资料: