文章目录
TOSCA(Topology and Orchestration Specification for Cloud Applications)是由OASIS组织制定的云应用拓扑编排规范。通俗地说,就是制定了一个标准,用来描述云平台上应用的拓扑结构。目前支持XML和YAML,Cloudiy的蓝图就是基于这个规范而来。这个规范比较庞大,本文尽量浓缩了TOSCA的YAML版前两章,以便用尽量少的时间了解尽量多的规范内容。 简介TOSCA的基本概念只有两个:节点(node)和关系(relationship)。节点有许多类型,可以是一台服务器,一个网络,一个计算节点等等。关系描述了节点之间是如何连接的。举个栗子:一个nodejs应用(节点)部署在(关系)名为host的主机(节点)上。节点和关系都可以通过程序来扩展和实现。 目前它的开源实现有OpenStack (Heat-Translator,Tacker,Senlin),Alien4Cloud,Cloudify等。 示例Hello World首先登场的是广大程序猿和攻城狮们都喜闻乐见的Hello World,但是其实里面并没有Hello World,只是比较简单而已。先看下面这段描述文件: tosca_definitions_version: tosca_simple_yaml_1_0 description: Template for deploying a single server with predefined properties. topology_template: node_templates: my_server: type: tosca.nodes.Compute capabilities: host: properties: num_cpus: 1 disk_size: 10 GB mem_size: 4096 MB os: properties: architecture: x86_64 type: linux distribution: rhel version: 6.5 除了TOSCA的版本 输入输出再看看下面这个描述文件: topology_template: inputs: cpus: type: integer description: Number of CPUs for the server. constraints: - valid_values: [ 1, 2, 4, 8 ] node_templates: my_server: type: tosca.nodes.Compute capabilities: host: properties: num_cpus: { get_input: cpus } mem_size: 2048 MB disk_size: 10 GB outputs: server_ip: description: The private IP address of the provisioned server. value: { get_attribute: [ my_server, private_address ] } 这里的 安装软件第三个描述文件如下: topology_template: inputs: # 略 node_templates: mysql: type: tosca.nodes.DBMS.MySQL properties: root_password: { get_input: my_mysql_rootpw } port: { get_input: my_mysql_port } requirements: - host: db_server db_server: type: tosca.nodes.Compute capabilities: # 略 我们看到了一个新的节点类型: 初始化数据库第四个描述文件如下: node_templates: my_db: type: tosca.nodes.Database.MySQL properties: name: { get_input: database_name } user: { get_input: database_user } password: { get_input: database_password } port: { get_input: database_port } artifacts: db_content: file: files/my_db_content.txt type: tosca.artifacts.File requirements: - host: mysql interfaces: Standard: create: implementation: db_create.sh inputs: db_data: { get_artifact: [ SELF, db_content ] } mysql: type: tosca.nodes.DBMS.MySQL properties: root_password: { get_input: mysql_rootpw } port: { get_input: mysql_port } requirements: - host: db_server db_server: # 略 这里的 两层应用再来看看第五个描述文件: node_templates: wordpress: type: tosca.nodes.WebApplication.WordPress properties: context_root: { get_input: context_root } admin_user: { get_input: wp_admin_username } admin_password: { get_input: wp_admin_password } db_host: { get_attribute: [ db_server, private_address ] } requirements: - host: apache - database_endpoint: wordpress_db interfaces: Standard: inputs: db_host: { get_attribute: [ db_server, private_address ] } db_port: { get_property: [ wordpress_db, port ] } db_name: { get_property: [ wordpress_db, name ] } db_user: { get_property: [ wordpress_db, user ] } db_password: { get_property: [ wordpress_db, password ] } apache: type: tosca.nodes.WebServer.Apache properties: # 略 requirements: - host: web_server web_server: type: tosca.nodes.Compute # 略 wordpress_db: type: tosca.nodes.Database.MySQL # 略 mysql: type: tosca.nodes.DBMS.MySQL # 略 db_server: type: tosca.nodes.Compute # 略 这个文件描述了一个很常见的拓扑结构: 关系定制化第六个描述文件: node_templates: wordpress: type: tosca.nodes.WebApplication.WordPress properties: # 略 requirements: - host: apache - database_endpoint: node: wordpress_db relationship: my.types.WordpressDbConnection wordpress_db: type: tosca.nodes.Database.MySQL properties: # 略 requirements: - host: mysql relationship_templates: my.types.WordpressDbConnection: type: ConnectsTo interfaces: Configure: pre_configure_source: scripts/wp_db_configure.sh 这里的关注点是 tosca_definitions_version: tosca_simple_yaml_1_0 description: Definition of custom WordpressDbConnection relationship type relationship_types: my.types.WordpressDbConnection: derived_from: tosca.relationships.ConnectsTo interfaces: Configure: pre_configure_source: scripts/wp_db_configure.sh 限定需求资源再看一个描述文件: node_templates: mysql: type: tosca.nodes.DBMS.MySQL properties: # 略 requirements: - host: node_filter: capabilities: - host: properties: - num_cpus: { in_range: [ 1, 4 ] } - mem_size: { greater_or_equal: 2 GB } - os: properties: - architecture: { equal: x86_64 } - type: linux - distribution: ubuntu 需要关注的是 node_templates: mysql: type: tosca.nodes.DBMS.MySQL properties: # 略 requirements: - host: mysql_compute mysql_compute: type: Compute node_filter: capabilities: - host: properties: num_cpus: { equal: 2 } mem_size: { greater_or_equal: 2 GB } - os: properties: architecture: { equal: x86_64 } type: linux distribution: ubuntu 数据库也可以使用: node_templates: my_app: type: my.types.MyApplication properties: admin_user: { get_input: admin_username } admin_password: { get_input: admin_password } db_endpoint_url: { get_property: [SELF, database_endpoint, url_path ] } requirements: - database_endpoint: node: my.types.nodes.MyDatabase node_filter: properties: - db_version: { greater_or_equal: 5.5 } 上面指定了数据库的版本。也可以抽出来做个模板: node_templates: my_app: type: my.types.MyApplication properties: admin_user: { get_input: admin_username } admin_password: { get_input: admin_password } db_endpoint_url: { get_property: [SELF, database_endpoint, url_path ] } requirements: - database_endpoint: my_abstract_database my_abstract_database: type: my.types.nodes.MyDatabase properties: - db_version: { greater_or_equal: 5.5 } 节点模板替换再看一个描述文件: node_templates: web_app: type: tosca.nodes.WebApplication.MyWebApp requirements: - host: web_server - database_endpoint: db web_server: type: tosca.nodes.WebServer requirements: - host: server server: type: tosca.nodes.Compute # 略 db: # 这是一个抽象节点 type: tosca.nodes.Database properties: user: my_db_user password: secret name: my_db_name 这里的 topology_template: inputs: db_user: type: string # 略 substitution_mappings: node_type: tosca.nodes.Database capabilities: database_endpoint: [ database, database_endpoint ] node_templates: database: type: tosca.nodes.Database properties: user: { get_input: db_user } # 略 requirements: - host: dbms dbms: type: tosca.nodes.DBMS # 略 server: type: tosca.nodes.Compute # 略 这里的 节点模板组再看一个描述文件: node_templates: apache: type: tosca.nodes.WebServer.Apache properties: # 略 requirements: - host: server server: type: tosca.nodes.Compute # 略 groups: webserver_group: type: tosca.groups.Root members: [ apache, server ] policies: - my_anti_collocation_policy: type: my.policies.anticolocateion targets: [ webserver_group ] # 可以一起处理 这个例子表明了 YAML宏下面这个描述文件使用了宏来避免重复: dsl_definitions: my_compute_node_props: &my_compute_node_props disk_size: 10 GB num_cpus: 1 mem_size: 2 GB topology_template: node_templates: my_server: type: Compute capabilities: - host: properties: *my_compute_node_props my_database: type: Compute capabilities: - host: properties: *my_compute_node_props 传参先看一个描述文件: node_templates: wordpress: type: tosca.nodes.WebApplication.WordPress requirements: - database_endpoint: mysql_database interfaces: Standard: inputs: wp_db_port: { get_property: [ SELF, database_endpoint, port ] } configure: implementation: wordpress_configure.sh inputs: wp_db_port: { get_property: [ SELF, database_endpoint, port ] } 这个例子有两个 node_templates: frontend: type: MyTypes.SomeNodeType attributes: url: { get_operation_output: [ SELF, Standard, create, generated_url ] } interfaces: Standard: create: implementation: scripts/frontend/create.sh configure: implementation: scripts/frontend/configure.sh inputs: data_dir: { get_operation_output: [ SELF, Standard, create, data_dir ] } 在这个例子里有两个 取动态值最后一个描述文件: node_types: ServerNode: derived_from: SoftwareComponent properties: notification_port: type: integer capabilities: # 略 ClientNode: derived_from: SoftwareComponent properties: # 略 requirements: - server: capability: Endpoint node: ServerNode relationship: ConnectsTo topology_template: node_templates: my_server: type: ServerNode properties: notification_port: 8000 my_client: type: ClientNode requirements: - server: node: my_server relationship: my_connection relationship_templates: my_connection: type: ConnectsTo interfaces: Configure: inputs: targ_notify_port: { get_attribute: [ TARGET, notification_port ] } # 略 这个例子里,类型为 |
|