OpenStack Heat 是一个基于模板的编排服务,用于自动化部署和管理基础设施资源。它允许用户通过编写模板文件来描述所需的基础设施资源和配置,然后使用 Heat 引擎来解析和执行这些模板,自动创建、配置和管理云环境中的资源。
例如,假设我们有一个Web应用程序,它需要一个虚拟机作为Web服务器、一个存储卷用于存储数据、一个网络用于连接虚拟机和存储卷。我们可以使用Heat模板来描述这些资源和它们之间的关系。在模板中,我们可以指定虚拟机的镜像、网络的子网、存储卷的大小等属性,并定义资源之间的依赖关系,比如虚拟机需要在存储卷创建完成后才能启动。一旦我们创建了这个模板,就可以使用Heat服务来部署和管理这个应用程序。Heat会根据模板中的描述,自动创建和配置虚拟机、存储卷和网络等资源。而当我们需要更新或删除这些资源时,只需要修改模板并重新执行Heat命令,它会根据模板中的变化来自动更新或删除相应的资源。
更多关于云计算服务 Openstack 系列的学习文章,请参阅:企业云计算平台 Openstack ,本系列持续更新中。
更快更有效的管理 OpenStack 的资源:云平台系统在相对比较稳定的情况下,管理成本逐渐变成首要的解决问题。云上自动化能力是一个云平台的刚需,可以有效降低维护难度。Heat 采用了模板方式来设计或者定义编排,为方便用户使用,Heat 还提供了大量的模板例子,使用户能够方便地得到想要的编排。
更小的研发成本:引入 Heat,对于不了解 OpenStack 的研发者来说,可以更快的接入现有的业务系统。开发者更关心的是授权认证和对虚拟资源的增删改,而对于底层的状态并不用太多了解。
配置Heat服务组件之前,需要先创建数据库、服务证书和API endpoint。更多关于云计算服务 Openstack 系列的学习文章,请参阅:企业云计算平台 Openstack ,本系列持续更新中。
①在操作系统终端连接数据库
[root@controller ~]# mysql -uroot -p000000
②创建heat
数据库
MariaDB [(none)]> CREATE DATABASE heat;
③Heat数据库访问权限设置
MariaDB [(none)]> GRANT ALL PRIVILEGES ON heat.* TO 'heat'@'%' IDENTIFIED BY '000000';
④退出数据库
[root@controller ~]# source admin-openrc.sh
①创建Heat用户
[root@controller ~]# openstack user create --domain default --password 000000 heat
+---------------------+----------------------------------+
| Field | Value |
+---------------------+----------------------------------+
| domain_id | default |
| enabled | True |
| id | c2774323d7e04a529d1fcdc18d6549f9 |
| name | heat |
| options | {} |
| password_expires_at | None |
+---------------------+----------------------------------+
②admin为用户添加角色
heat
[root@controller ~]# openstack role add --project service --user heat admin
③创建Heat和heat-cfn service entity
[root@controller ~]# openstack service create --name heat --description "Orchestration" orchestration
+-------------+----------------------------------+
| Field | Value |
+-------------+----------------------------------+
| description | Orchestration |
| enabled | True |
| id | 7ce739d29f3841ad9bed4d286f546bd7 |
| name | heat |
| type | orchestration |
+-------------+----------------------------------+
[root@controller ~]# openstack service create --name heat-cfn --description "Orchestration" cloudformation
+-------------+----------------------------------+
| Field | Value |
+-------------+----------------------------------+
| description | Orchestration |
| enabled | True |
| id | f1c9a2b146b94d75b19a13f1b806e4d8 |
| name | heat-cfn |
| type | cloudformation |
+-------------+----------------------------------+
[root@controller ~]# openstack endpoint create --region RegionOne orchestration public http://controller:8004/v1/%\(tenant_id\)s
+--------------+-----------------------------------------+
| Field | Value |
+--------------+-----------------------------------------+
| enabled | True |
| id | 56d9afe212be479394529d2e5b33e9bc |
| interface | public |
| region | RegionOne |
| region_id | RegionOne |
| service_id | 7ce739d29f3841ad9bed4d286f546bd7 |
| service_name | heat |
| service_type | orchestration |
| url | http://controller:8004/v1/%(tenant_id)s |
+--------------+-----------------------------------------+
[root@controller ~]# openstack endpoint create --region RegionOne orchestration internal http://controller:8004/v1/%\(tenant_id\)s
+--------------+-----------------------------------------+
| Field | Value |
+--------------+-----------------------------------------+
| enabled | True |
| id | b77211d055ac468094a1834318a24341 |
| interface | internal |
| region | RegionOne |
| region_id | RegionOne |
| service_id | 7ce739d29f3841ad9bed4d286f546bd7 |
| service_name | heat |
| service_type | orchestration |
| url | http://controller:8004/v1/%(tenant_id)s |
+--------------+-----------------------------------------+
[root@controller ~]# openstack endpoint create --region RegionOne orchestration admin http://controller:8004/v1/%\(tenant_id\)s
+--------------+-----------------------------------------+
| Field | Value |
+--------------+-----------------------------------------+
| enabled | True |
| id | 02f7ab4296a94dc1ac909cf7e83b91bc |
| interface | admin |
| region | RegionOne |
| region_id | RegionOne |
| service_id | 7ce739d29f3841ad9bed4d286f546bd7 |
| service_name | heat |
| service_type | orchestration |
| url | http://controller:8004/v1/%(tenant_id)s |
+--------------+-----------------------------------------+
[root@controller ~]# openstack endpoint create --region RegionOne cloudformation public http://controller:8000/v1
+--------------+----------------------------------+
| Field | Value |
+--------------+----------------------------------+
| enabled | True |
| id | 26264981dd3a414aa18fd64e4ec2ba84 |
| interface | public |
| region | RegionOne |
| region_id | RegionOne |
| service_id | f1c9a2b146b94d75b19a13f1b806e4d8 |
| service_name | heat-cfn |
| service_type | cloudformation |
| url | http://controller:8000/v1 |
+--------------+----------------------------------+
[root@controller ~]# openstack endpoint create --region RegionOne cloudformation internal http://controller:8000/v1
+--------------+----------------------------------+
| Field | Value |
+--------------+----------------------------------+
| enabled | True |
| id | 9b4f604bb0984b63ad050655762a7d41 |
| interface | internal |
| region | RegionOne |
| region_id | RegionOne |
| service_id | f1c9a2b146b94d75b19a13f1b806e4d8 |
| service_name | heat-cfn |
| service_type | cloudformation |
| url | http://controller:8000/v1 |
+--------------+----------------------------------+
[root@controller ~]# openstack endpoint create --region RegionOne cloudformation admin http://controller:8000/v1
+--------------+----------------------------------+
| Field | Value |
+--------------+----------------------------------+
| enabled | True |
| id | 9c8c76a1b03a44a8a7728fa47670410e |
| interface | admin |
| region | RegionOne |
| region_id | RegionOne |
| service_id | f1c9a2b146b94d75b19a13f1b806e4d8 |
| service_name | heat-cfn |
| service_type | cloudformation |
| url | http://controller:8000/v1 |
+--------------+----------------------------------+
①创建Heat域
[root@controller ~]# openstack domain create --description "Stack projects and users" heat
+-------------+----------------------------------+
| Field | Value |
+-------------+----------------------------------+
| description | Stack projects and users |
| enabled | True |
| id | 3fb118462aa344ba8c0fb36fc0296fa7 |
| name | heat |
| options | {} |
| tags | [] |
+-------------+----------------------------------+
②创建heat_domain_admin用户,管理Heat域中的project和user
[root@controller ~]# openstack user create --domain heat --password-prompt heat_domain_admin
User Password:
Repeat User Password:
+---------------------+----------------------------------+
| Field | Value |
+---------------------+----------------------------------+
| domain_id | 3fb118462aa344ba8c0fb36fc0296fa7 |
| enabled | True |
| id | 2632b29ed7a746e4bf01466c475fcf57 |
| name | heat_domain_admin |
| options | {} |
| password_expires_at | None |
+---------------------+----------------------------------+
③将admin
角色添加到heat_domain_admin
域中的用户 heat
以启用heat_domain_admin
用户的管理堆栈管理权限
[root@controller ~]# openstack role add --domain heat --user-domain heat --user heat_domain_admin admin
④创建 heat_stack_owner 角色
[root@controller ~]# openstack role create heat_stack_owner
+-------------+----------------------------------+
| Field | Value |
+-------------+----------------------------------+
| description | None |
| domain_id | None |
| id | 816798fab0bf4825a4a71d36ade47f44 |
| name | heat_stack_owner |
| options | {} |
+-------------+----------------------------------+
⑤将heat_stack_owner
角色添加到demo
project和user,使demo user 可以管理 Stack
[root@controller ~]# openstack role add --project demo --user demo heat_stack_owner
⑥创建heat_stack_user_role
[root@controller ~]# openstack role create heat_stack_user
+-------------+----------------------------------+
| Field | Value |
+-------------+----------------------------------+
| description | None |
| domain_id | None |
| id | a1b8a79460f04f28ba5c12bc372304d3 |
| name | heat_stack_user |
| options | {} |
+-------------+----------------------------------+
更多关于云计算服务 Openstack 系列的学习文章,请参阅:企业云计算平台 Openstack ,本系列持续更新中。
[root@controller ~]# yum install -y openstack-heat-api openstack-heat-api-cfn openstack-heat-engine openstack-heat-ui
编辑/etc/heat/heat.conf
文件并完成以下操作。
①在[database]
部分中,配置数据库访问
[database]
connection = mysql+pymysql://heat:000000@controller/heat
②在[DEFAULT]
部分中,配置RabbitMQ
消息队列访问
[DEFAULT]
transport_url = rabbit://openstack:000000@controller
③在[keystone_authtoken]
、[trustee]
、 和[clients_keystone]
部分中,配置身份服务访问
[keystone_authtoken]
www_authenticate_uri = http://controller:5000
auth_url = http://controller:5000
memcached_servers = controller:11211
auth_type = password
project_domain_name = default
user_domain_name = default
project_name = service
username = heat
password = 000000
[trustee]
auth_type = password
auth_url = http://controller:5000
username = heat
password = 000000
user_domain_name = default
[clients_keystone]
auth_uri = http://controller:5000
④在[DEFAULT]
部分中,配置元数据和等待条件 URL
[DEFAULT]
heat_metadata_server_url = http://controller:8000
heat_waitcondition_server_url = http://controller:8000/v1/waitcondition
⑤在[DEFAULT]
部分中,配置堆栈域和管理凭据
[DEFAULT]
stack_domain_admin = heat_domain_admin
stack_domain_admin_password = 000000
stack_user_domain_name = heat
[root@controller ~]# su -s /bin/sh -c "heat-manage db_sync" heat
启动 Orchestration 服务并将其配置为开机自启。
[root@controller ~]# systemctl start openstack-heat-api.service openstack-heat-api-cfn.service openstack-heat-engine.service && systemctl enable openstack-heat-api.service openstack-heat-api-cfn.service openstack-heat-engine.service
# 自行查看log文件是否报错
更多关于云计算服务 Openstack 系列的学习文章,请参阅:企业云计算平台 Openstack ,本系列持续更新中。
(1)加载环境变量
[root@controller ~]# source admin-openrc.sh
(2)列出Heat编排服务组件进程是否成功启动和注册
[root@controller ~]# openstack orchestration service list
+------------+-------------+--------------------------------------+------------+--------+----------------------------+--------+
| Hostname | Binary | Engine ID | Host | Topic | Updated At | Status |
+------------+-------------+--------------------------------------+------------+--------+----------------------------+--------+
| controller | heat-engine | 4c0919cf-a36b-449c-8fa6-4dad9f355a43 | controller | engine | 2023-07-01T01:53:55.000000 | up |
| controller | heat-engine | acc70835-f179-44c7-9ae6-e900cdda1fa8 | controller | engine | 2023-07-01T01:53:55.000000 | up |
| controller | heat-engine | f9dc47f6-06ad-4dbb-afc5-7f4138b58d3b | controller | engine | 2023-07-01T01:53:55.000000 | up |
| controller | heat-engine | 7c95fd77-74f8-4cd9-ac8e-984c36554ad8 | controller | engine | 2023-07-01T01:53:55.000000 | up |
+------------+-------------+--------------------------------------+------------+--------+----------------------------+--------+
编写 Heat 模板执行 yaml 文件可以创建名为 heat-swift 的容器
[root@controller ~]# vim swift.yaml
heat_template_version: 2014-10-16
description: "创建容器"
resources:
user:
type: OS::Swift::Container
properties:
name: heat-swift
[root@controller ~]# openstack stack create csqstack -t create_container.yaml
+---------------------+--------------------------------------+
| Field | Value |
+---------------------+--------------------------------------+
| id | 82f6fda4-fa1c-4cc7-b8f1-340f6e13d266 |
| stack_name | csqstack |
| description | 创建容器 |
| creation_time | 2023-07-01T11:49:09Z |
| updated_time | None |
| stack_status | CREATE_IN_PROGRESS |
| stack_status_reason | Stack CREATE started |
+---------------------+--------------------------------------+
[root@controller ~]# swift list
heat-swift
Heat是一个基于模板来编排复合云应用的服务。Heat 目前支持两种格式的模板,一种是基于 JSON 格式的 CFN 模板,另外一种是基于 YAML 格式的 HOT 模板。CFN 模板主要是为了保持对 AWS 的兼容性。HOT 模板是 Heat 自有的,资源类型更加丰富,更能体现出 Heat 特点的模板。
heat_template_version: 2013-05-23 //版本信息
parameters: //自定义的变量
ImageID:
type: string
resources: //描述的资源,如网络、实例
server1:
type: OS::Nova::Server
outputs: //返回值
server1_private_ip:
value: { get_attr: [ server1, first_address ] }
编排:按照一定的目的依次排列。
一个完整的编排包括设置服务器上机器、安装 CPU、内存、硬盘、通电、插入网络接口、安装操作系统、配置操作系统、安装中间件、配置中间件、安装应用程序、配置应用发布程序。对于复杂的需要部署在多台服务器上的应用,需要重复这个过程,而且需要协调各个应用模块的配置,下图显示了一个典型应用需要编排的项目。
Heat:Heat 在这种情况下应运而生,它采用了业界流行的模板方式进行设计和定义编排。用户只需要打开文本编辑器,编写一段基于 Key-Value 的模板,就能够方便地得到想要的编排。为了方便用户的使用,Heat 提供了大量的模板例子,通常用户只需要选择想要的编排,通过拷贝、粘贴的方式来完成模板的编写。
#使用栈模板test-stack.yml创建一个名为Orchestration的栈。
heat stack-create orchestration -f test-stack1.yml --parameters "ImageID=centos6.5;NetID=sharednet1"
#查询栈列表
heat stack-list
#查看栈的详细信息
heat stack-show orchestration
#删除栈
heat stack-delete orchestration
#查看栈资源列表
heat resource-list orchestration
#查看栈资源
heat resource-show orchestration server1
#查看输出列表
heat output-list orchestration
#查看输出值
heat output-show orchestration server1_private_ip
#查看事件列表
heat event-list orchestration
#查看资源事件详细信息
heat event-show orchestration server1 10c307a5-1732-4f10-8b91-d9b23402661d
任务要求:为了使用编配服务实现自动化部署云主机,需要执行三个步骤:
基于xiandian,首先都需要生效环境变量,获取令牌(source /etc/keystone/admin-openrc.sh)。
第一步:获取基础资源信息(如果没有,则需要重新创建),包括认证、镜像、网络、计算、存储等资源,同时还需要设置密钥对和安全组策略。
#查看当前的部门信息,即租户列表
[root@xiandian ~]#openstack project list
#创建新部门
[root@xiandian ~]#openstack project create cookbook --domain xiandian
[root@xiandian ~]#openstack project list
#创建新角色
[root@xiandian ~]#openstack role create manager
[root@xiandian ~]#openstack role list
#创建新用户cook001
[root@xiandian ~]#openstack user create cook001 --password 000000 --domain xiandian --email cook001@xiandian.com
[root@xiandian ~]#openstack user list
#为新用户分配部门,安排角色,就是把三者绑定
[root@xiandian ~]# openstack role add manager --user cook001 --project cookbook
#镜像服务glance
[root@xiandian ~]#glance image-list
#获取源文件
[root@xiandian ~]#wget ftp://172.16.102.20/cirros-0.3.4-x86_64-disk.img
#使用当前源文件创建镜像
[root@xiandian ~]#glance image-create --name cirros50 --disk-format qcow2 --container-format bare --progress<cirros-0.3.4-x86_64-disk.img
[root@xiandian ~]#glance image-list
[root@xiandian ~]#glance image-create --name cirros150 --disk-format vmdk --container-format bare --progress<cirros-0.3.4-x86_64-disk.img
[root@xiandian ~]#glance image-list
#设置镜像的最小启动硬盘大小和内存大小
[root@xiandian ~]#glance image-update --min-disk=20 --min-ram=2048 镜像id号
# 查看当前可用网络
[root@xiandian ~]#neutron net-list
#创建子网subnet50
[root@xiandian ~]#neutron subnet-create sharednet1 --name subnet50 --gateway 192.168.50.1 192.168.50.0/24
[root@xiandian ~]#neutron net-list
[root@xiandian ~]#neutron subnet-list
#计算服务(确定云主机的主机类型flavor)
[root@xiandian ~]#nova flavor-list
#创建自定义主机类型
[root@xiandian ~]#nova flavor-create cook.small 6 4096 60 4
[root@xiandian ~]#nova flavor-list
#生成密钥对keypair
[root@xiandian ~]#nova keypair-list
[root@xiandian ~]#nova keypair-add mykey > mykey
[root@xiandian ~]#nova keypair-list
#设置安全组secgroup策略
[root@xiandian ~]#nova secgroup-list
#自定义安全组cookbook
[root@xiandian ~]#nova secgroup-create cookbook-rules 'create a secgroup for cookbook'
#为安全组添加规则
[root@xiandian ~]#nova secgroup-add-rule cookbook-rules ICMP -1 -1 0.0.0.0/0
[root@xiandian ~]#nova secgroup-add-rule cookbook-rules UDP 83 83 192.168.50.0/24
[root@xiandian ~]#nova secgroup-list-rules cookbook-rules
第二步:编写编配脚本文件test50.yml
[root@xiandian ~]#vi test50.yml #见图一
[root@xiandian ~]#chmod -X test50.yml
第三步:使用test50.yml这个文件创建栈资源,启动云主机
[root@xiandian ~]#heat stack-create test50-1 -f test50.yml
#查看栈列表
[root@xiandian ~]#heat stack-list
[root@xiandian ~]#nova list
[root@xiandian ~]#heat stack-create test50-2 -f test50.yml
#编写完整地heat模板文件
[root@xiandian ~]#vi test50-3.yml #见图二
[root@xiandian ~]#heat stack-create test50-3 -f test50-3.yml --parameterts " Name=myinstance;Image=cirros50"
[root@xiandian ~]#heat stack-list
图一:
图二:
参考链接:https://blog.csdn.net/qq_52089863/ article/details/131487982 https://blog.csdn.net /Mediocre_person/article/details/122093479