2020国产成人精品视频,性做久久久久久久久,亚洲国产成人久久综合一区,亚洲影院天堂中文av色

分享

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

 louy2 2019-02-25

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

輸入輸出

再看看下面這個(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 ] }

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

安裝軟件

第三個(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)類型:tosca.nodes.DBMS.MySQL。這個(gè)類型允許接收root_passwordport的參數(shù)。在requirements里定義了mysql這個(gè)節(jié)點(diǎn)需要被安裝到db_server這個(gè)節(jié)點(diǎn)上,這就是“關(guān)系”。如果只想表明依賴,比如說service_a依賴于service_b,也可以直接用- dependency: service_b來描述。上面文件的拓?fù)浣Y(jié)構(gòu)如下圖:

初始化數(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:
      # 略

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

兩層應(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):mysql里有一個(gè)wordpress_db,運(yùn)行在db_server上;apache部署了一個(gè)wordpress,運(yùn)行在web_server上。wordpress需要wordpress_db。

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

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_filter。這里并沒有指定mysql在哪個(gè)節(jié)點(diǎn)上啟動,但是指定了一些節(jié)點(diǎn)信息,只有符合的節(jié)點(diǎn)才能夠啟動它。也可以抽出來做個(gè)模板:

  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

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

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é)點(diǎn)提供的database_endpoint。兩個(gè)文件聯(lián)系起來看,表明了上面的web_app不需要管db是什么樣子的,有什么拓?fù)浣Y(jié)構(gòu),它關(guān)心的只是database_endpoint。而下面由databasedbmsserver三個(gè)節(jié)點(diǎn)組成的模板正好可以提供database_endpoint,從而替換掉db這個(gè)抽象節(jié)點(diǎn)。另外,這樣的替換也支持嵌套。

節(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è)例子表明了apacheserver應(yīng)該是一組的關(guān)系。這樣它們就可以一起被處理,比如說伸縮。

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è)inputs,前者指的是為所有操作都聲明一個(gè)變量,后者指的是為configure這個(gè)操作聲明一個(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è)get_operation_output,前者指的是將create操作的環(huán)境變量generated_url設(shè)置到url里,后者是將data_dir傳遞給configure操作。

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

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多