文章目錄
TOSCA(Topology and Orchestration Specification for Cloud Applications)是由OASIS組織制定的云應(yīng)用拓?fù)渚幣乓?guī)范。通俗地說,就是制定了一個(gè)標(biāo)準(zhǔn),用來描述云平臺上應(yīng)用的拓?fù)浣Y(jié)構(gòu)。目前支持XML和YAML,Cloudiy的藍(lán)圖就是基于這個(gè)規(guī)范而來。這個(gè)規(guī)范比較龐大,本文盡量濃縮了TOSCA的YAML版前兩章,以便用盡量少的時(shí)間了解盡量多的規(guī)范內(nèi)容。 簡介TOSCA的基本概念只有兩個(gè):節(jié)點(diǎn)(node)和關(guān)系(relationship)。節(jié)點(diǎn)有許多類型,可以是一臺服務(wù)器,一個(gè)網(wǎng)絡(luò),一個(gè)計(jì)算節(jié)點(diǎn)等等。關(guān)系描述了節(jié)點(diǎn)之間是如何連接的。舉個(gè)栗子:一個(gè)nodejs應(yīng)用(節(jié)點(diǎn))部署在(關(guān)系)名為host的主機(jī)(節(jié)點(diǎn))上。節(jié)點(diǎn)和關(guān)系都可以通過程序來擴(kuò)展和實(shí)現(xiàn)。 目前它的開源實(shí)現(xiàn)有OpenStack (Heat-Translator,Tacker,Senlin),Alien4Cloud,Cloudify等。 示例Hello World首先登場的是廣大程序猿和攻城獅們都喜聞樂見的Hello World,但是其實(shí)里面并沒有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的版本 輸入輸出再看看下面這個(gè)描述文件: 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 ] } 這里的 安裝軟件第三個(gè)描述文件如下: 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: # 略 我們看到了一個(gè)新的節(jié)點(diǎn)類型: 初始化數(shù)據(jù)庫第四個(gè)描述文件如下: 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: # 略 這里的 兩層應(yīng)用再來看看第五個(gè)描述文件: 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 # 略 這個(gè)文件描述了一個(gè)很常見的拓?fù)浣Y(jié)構(gòu): 關(guān)系定制化第六個(gè)描述文件: 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 這里的關(guān)注點(diǎn)是 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 限定需求資源再看一個(gè)描述文件: 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 需要關(guān)注的是 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 數(shù)據(jù)庫也可以使用: 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 } 上面指定了數(shù)據(jù)庫的版本。也可以抽出來做個(gè)模板: 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 } 節(jié)點(diǎn)模板替換再看一個(gè)描述文件: 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: # 這是一個(gè)抽象節(jié)點(diǎn) 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 # 略 這里的 節(jié)點(diǎn)模板組再看一個(gè)描述文件: 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 ] # 可以一起處理 這個(gè)例子表明了 YAML宏下面這個(gè)描述文件使用了宏來避免重復(fù): 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 傳參先看一個(gè)描述文件: 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 ] } 這個(gè)例子有兩個(gè) 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 ] } 在這個(gè)例子里有兩個(gè) 取動態(tài)值最后一個(gè)描述文件: 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 ] } # 略 這個(gè)例子里,類型為 |
|