小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

TOSCA簡介 | 懶程序員改變世界

 louy2 2019-02-25

TOSCA(Topology and Orchestration Specification for Cloud Applications)是由OASIS組織制定的云應用拓撲編排規(guī)范。通俗地說,就是制定了一個標準,用來描述云平臺上應用的拓撲結(jié)構(gòu)。目前支持XML和YAML,Cloudiy的藍圖就是基于這個規(guī)范而來。這個規(guī)范比較龐大,本文盡量濃縮了TOSCA的YAML版前兩章,以便用盡量少的時間了解盡量多的規(guī)范內(nèi)容。

簡介

TOSCA的基本概念只有兩個:節(jié)點(node)和關(guān)系(relationship)。節(jié)點有許多類型,可以是一臺服務器,一個網(wǎng)絡,一個計算節(jié)點等等。關(guān)系描述了節(jié)點之間是如何連接的。舉個栗子:一個nodejs應用(節(jié)點)部署在(關(guān)系)名為host的主機(節(jié)點)上。節(jié)點和關(guān)系都可以通過程序來擴展和實現(xiàn)。

目前它的開源實現(xiàn)有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的版本tosca_definitions_version和描述信息description以外,就是這個topology_template了。這里我們看到有一個名為my_server的節(jié)點,它的類型是tosca.nodes.Compute。這個類型預置了兩個capabilities信息,一個是host,定義了硬件信息;另一個是os,定義了操作系統(tǒng)信息。

輸入輸出

再看看下面這個描述文件:

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 ] }

這里的inputsoutputs分別定義了輸入和輸出。輸入的cpus是在1,2,4和8中的一個整數(shù),而輸出的server_ip就是my_server這個節(jié)點的private_address也就是私有IP地址。另外一點是TOSCA提供了一些內(nèi)置函數(shù),在上面這個文件中使用了get_inputget_attribute。輸入?yún)?shù)可以通過get_input被使用。

安裝軟件

第三個描述文件如下:

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:
        # 略

我們看到了一個新的節(jié)點類型:tosca.nodes.DBMS.MySQL。這個類型允許接收root_passwordport的參數(shù)。在requirements里定義了mysql這個節(jié)點需要被安裝到db_server這個節(jié)點上,這就是“關(guān)系”。如果只想表明依賴,比如說service_a依賴于service_b,也可以直接用- dependency: service_b來描述。上面文件的拓撲結(jié)構(gòu)如下圖:

初始化數(shù)據(jù)庫

第四個描述文件如下:

  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:
      # 略

這里的tosca.nodes.Database.MySQL表示一個MySQL數(shù)據(jù)庫的實例。在artifactsdb_content里指定了一個文本文件,而這個文件將被interfaces里的Create所用,為db_create.sh腳本提供數(shù)據(jù)。Standard表示生命周期,可能會包含configurestart、stop等各種操作,而db_create.sh本身是對tosca.nodes.Database.MySQL提供的默認create操作的一個重寫。如下圖:

兩層應用

再來看看第五個描述文件:

  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
      # 略

這個文件描述了一個很常見的拓撲結(jié)構(gòu):mysql里有一個wordpress_db,運行在db_server上;apache部署了一個wordpress,運行在web_server上。wordpress需要wordpress_db

關(guān)系定制化

第六個描述文件:

  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)注點是relationship里的my.types.WordpressDbConnection。這是一個自定義的關(guān)系,在文件的下半部分描述了詳細定義。它實際上是一個ConnectsTo類型,為pre_configure_source操作提供了一個自定義腳本。這個定義也可以單獨提出一個文件,就像下面這樣:

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

需要關(guān)注的是node_filter。這里并沒有指定mysql在哪個節(jié)點上啟動,但是指定了一些節(jié)點信息,只有符合的節(jié)點才能夠啟動它。也可以抽出來做個模板:

  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ù)庫的版本。也可以抽出來做個模板:

  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é)點模板替換

再看一個描述文件:

  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:
      # 這是一個抽象節(jié)點
      type: tosca.nodes.Database
      properties:
        user: my_db_user
        password: secret
        name: my_db_name

這里的db是一個抽象節(jié)點,可以被下面的描述文件所替換:

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
      # 略

這里的database_endpoint是由database節(jié)點提供的database_endpoint。兩個文件聯(lián)系起來看,表明了上面的web_app不需要管db是什么樣子的,有什么拓撲結(jié)構(gòu),它關(guān)心的只是database_endpoint。而下面由database、dbmsserver三個節(jié)點組成的模板正好可以提供database_endpoint,從而替換掉db這個抽象節(jié)點。另外,這樣的替換也支持嵌套。

節(jié)點模板組

再看一個描述文件:

  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 ]
        # 可以一起處理

這個例子表明了apacheserver應該是一組的關(guān)系。這樣它們就可以一起被處理,比如說伸縮。

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 ] }

這個例子有兩個inputs,前者指的是為所有操作都聲明一個變量,后者指的是為configure這個操作聲明一個變量。再看下一個文件:

  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 ] }

在這個例子里有兩個get_operation_output,前者指的是將create操作的環(huán)境變量generated_url設置到url里,后者是將data_dir傳遞給configure操作。

取動態(tài)值

最后一個描述文件:

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 ] }
            # 略

這個例子里,類型為ClientNodemy_clientmy_connection關(guān)系的Configure操作上需要notification_port變量。這樣的話,當類型為ServerNodemy_server連接過來時,就能取到它的notification_port變量,并設置到targ_notify_port環(huán)境變量里。有一點值得注意的是,真實的notification_port可能是8000,也可能不是。所以在這種情況下,不用get_property,而用get_attribute函數(shù)。

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多