更新时间:2024-10-16 GMT+08:00

创建并管理组件

操作场景

组件是组成应用的某个业务特性实现,以代码或者软件包为载体,可独立部署在环境下运行。

下面介绍如何调用应用中创建组件API创建组件后,先调用获取部署任务详细信息API查询创建组件任务的状态,再调用根据组件ID修改组件信息API升级组件,然后调用根据组件ID下发组件任务API回滚组件版本,最后再次调用根据组件ID下发组件任务API停止组件实例运行。

API的调用方法请参见如何调用API

前提条件

  • 已调用获取用户Token获取IAM用户的Token,并将token值设置为环境变量:export Token={token}。
  • 已获取ServiceStage的终端节点,详细信息请参见地区和终端节点,并用于替换以下操作步骤示例中的{servicestage_endpoint}。

操作步骤

  1. 调用应用中创建组件API在某个应用下创建组件,请求示例如下:

    curl -k -H "Content-Type:application/json;charset=utf8" -H "X-Auth-Token:$token" -X POST "https://{servicestage_endpoint}/v3/{project_id}/cas/applications/{application_id}/components" -d ' 
    {
        "name": "test-component",
        "description": "",
        "labels": [],
        "version": "2023.0712.09204",
        "environment_id": "21a43958-f64f-4077-8ae3-3a03bf0111c7",
        "application_id": "2c458700-c8e0-4651-a73b-5219b70a6572",
        "enterprise_project_id": "0",
        "runtime_stack": {
            "name": "Docker",
            "version": "",
            "type": "Docker",
            "deploy_mode": "container"
        },
        "source": {
            "kind": "image",
            "storage": "swr",
            "url": "swr.ap-southeast-1.myhuaweicloud.com/servicestage-demo/test_02:v1",
            "version": "v1"
        },
        "tomcat_opts": {
            "server_xml": ""
        },
        "refer_resources": [
            {
                "id": "1d3091a2-1ecf-11ee-ae69-0255ac1001ba",
                "type": "cce",
                "parameters": {
                    "namespace": "default",
                    "type": "VirtualMachine"
                }
            }
        ],
        "replica": 2,
        "config_mode": "ui",
        "limit_cpu": 0.25,
        "limit_memory": 0.5,
        "request_cpu": 0.25,
        "request_memory": 0.5,
        "external_accesses": [],
        "storages": [],
        "logs": [],
        "affinity": {
            "az": [],
            "node": [],
            "component": []
        },
        "anti_affinity": {
            "az": [],
            "node": [],
            "component": []
        },
        "update_strategy": {
            "type": "RollingUpdate",
            "max_surge": 0,
            "max_unavailable": 1
        },
        "command": {
            "args": [],
            "command": []
        },
        "custom_metric": {}
    }'

    返回结果:

    {
        "job_id": "JOB870e9ffe-227e-43a9-9c84-20a7ad02e77c",
        "component_id": "f3f6cb89-831d-4579-8bbc-664487889e38"
    }

    记录返回结果中加粗显示的job_id、component_id的值,用于替换后续示例中的{job_id}、{component_id}。

  2. 调用获取部署任务详细信息API查询创建组件任务是否成功,请求示例如下:

    curl -k -H "Content-Type:application/json;charset=utf8" -H "X-Auth-Token:$Token" -X GET "https://{servicestage_endpoint}/v3/{project_id}/cas/jobs/{job_id}"

    返回结果:

    {
        "job": {
            "JOB_ID": "JOB870e9ffe-227e-43a9-9c84-20a7ad02e77c", 
            "JOB_NAME": "provision/provMain", 
            "JOB_TYPE": "PROVISION", 
            "SERVICE_INSTANCE_ID": "429a35a0-dee8-4648-86f6-d1f8bc74ba52", 
            "PROJECT_ID": "063a3158b1a34710b36ad208b9497d00", 
            "ORDER_ID": "", 
            "JOB_DESC": "provision/provMain", 
            "EXECUTION_STATUS": "SUCCEEDED", 
            "CREATED_BY": "585cc5c16cda4954b28d4eb460f06261"
        }, 
        "tasks": [
            {
                "MESSAGES": "{}", 
                "TASK_ID": "Task-bc64b46c-1688-4787-b7ff-87cb53b346ea", 
                "TASK_TYPE": "echo", 
                "TASK_INDEX": 1, 
                "TASK_NAME": "SEPARATOR_PREPARE_RESOURCES", 
                "TASK_STATUS": "SUCCEEDED", 
                "LAST_HEALTH_CHECK": "2020-02-04T04:01:03.344Z", 
                "CREATED_AT": "2020-02-04T04:01:03.344Z", 
                "OWNER_ID": "cas-mgr-69bb87d7c-wpqwb:30114"
            }, 
            {
                "MESSAGES": "{}", 
                "TASK_ID": "Task-15ec252f-bca0-401e-ae5b-798a639df92e", 
                "TASK_TYPE": "echo", 
                "TASK_INDEX": 4, 
                "TASK_NAME": "SEPARATOR_BUILDING", 
                "TASK_STATUS": "SUCCEEDED", 
                "LAST_HEALTH_CHECK": "2020-02-04T04:01:03.346Z", 
                "CREATED_AT": "2020-02-04T04:01:03.345Z", 
                "OWNER_ID": "cas-mgr-69bb87d7c-wpqwb:30114"
            }, 
            {
                "MESSAGES": "{}", 
                "TASK_ID": "Task-52a5d1ae-2c54-46ec-8134-53e7b6a45fb7", 
                "TASK_TYPE": "restful", 
                "TASK_INDEX": 6, 
                "TASK_NAME": "TN00091", 
                "TASK_STATUS": "SUCCEEDED", 
                "LAST_HEALTH_CHECK": "2020-02-04T04:01:08.924Z", 
                "CREATED_AT": "2020-02-04T04:01:03.346Z", 
                "OWNER_ID": "cas-mgr-69bb87d7c-wpqwb:30114"
            }, 
            {
                "MESSAGES": "{}", 
                "TASK_ID": "Task-a149b585-d31b-4c72-a665-d820efcb0bf9", 
                "TASK_TYPE": "restful", 
                "TASK_INDEX": 7, 
                "TASK_NAME": "TN00094", 
                "TASK_STATUS": "SUCCEEDED", 
                "LAST_HEALTH_CHECK": "2020-02-04T04:01:08.961Z", 
                "CREATED_AT": "2020-02-04T04:01:08.925Z", 
                "OWNER_ID": "cas-mgr-69bb87d7c-wpqwb:30114"
            }, 
            {
                "MESSAGES": "", 
                "TASK_ID": "Task-a72fe8d8-8cc1-42b5-a097-0cf9e29692a7", 
                "TASK_TYPE": "restful", 
                "TASK_INDEX": 9, 
                "TASK_NAME": "TN00097", 
                "TASK_STATUS": "SUCCEEDED", 
                "LAST_HEALTH_CHECK": "2020-02-04T04:01:38.975Z", 
                "CREATED_AT": "2020-02-04T04:01:08.962Z", 
                "OWNER_ID": "cas-mgr-69bb87d7c-wpqwb:30114"
            }
        ], 
        "task_count": 5
    }

  3. 调用根据组件ID修改组件信息API升级组件,请求示例如下:

    curl -k -H "Content-Type:application/json;charset=utf8" -H "X-Auth-Token:$token" -X PUT "https://{servicestage_endpoint}/v3/{project_id}/cas/applications/{application_id}/components/{component_id}" -d ' 
    {
        "source": {
            "kind": "image",
            "storage": "swr",
            "url": "swr.ap-southeast-1.myhuaweicloud.com/demo/arm_tomcat8:v1"
        },
        "version": "2023.0717.17160",
        "refer_resources": [
            {
                "id": "1d3091a2-1ecf-11ee-ae69-0255ac1001ba",
                "type": "cce",
                "parameters": {
                    "namespace": "default",
                    "type": "ARM64"
                }
            }
        ],
        "limit_cpu": 0.25,
        "limit_memory": 0.5,
        "request_cpu": 0.25,
        "request_memory": 0.5,
        "mesher": {},
        "affinity": {},
        "anti_affinity": {},
        "liveness_probe": {},
        "readiness_probe": {},
        "command": {
            "command": [],
            "args": []
        },
        "envs": [
            {
                "name": "PAAS_PROJECT_ID",
                "inner": false,
                "value": "063a3158b1a34710b36ad208b9497d00"
            },
            {
                "name": "CAS_APP_ID",
                "inner": false,
                "value": "f3f6cb89-831d-4579-8bbc-664487889e38"
            },
            {
                "name": "PAAS_CLUSTER_ID",
                "inner": false,
                "value": "1d3091a2-1ecf-11ee-ae69-0255ac1001ba"
            },
            {
                "name": "CAS_APPLICATION_ID",
                "inner": false,
                "value": "2c458700-c8e0-4651-a73b-5219b70a6572"
            },
            {
                "name": "CAS_INSTANCE_VERSION",
                "inner": false,
                "value": "2023.0717.17062"
            },
            {
                "name": "CAS_COMPONENT_ID",
                "inner": false,
                "value": "e1c3021b-df40-48e9-bd41-ef033fc77f2b"
            },
            {
                "name": "CAS_INSTANCE_NAME",
                "inner": false,
                "value": "etst1-test-arm-5xdhrd"
            },
            {
                "name": "CAS_COMPONENT_NAME",
                "inner": false,
                "value": "etst1"
            },
            {
                "name": "PAAS_NAMESPACE",
                "inner": false,
                "value": "default"
            },
            {
                "name": "PAAS_APP_NAME",
                "inner": false,
                "value": "etst1"
            },
            {
                "name": "PAAS_PROJECT_NAME",
                "inner": false,
                "value": "cn-north-7"
            },
            {
                "name": "CAS_INSTANCE_ID",
                "inner": false,
                "value": "f3f6cb89-831d-4579-8bbc-664487889e38"
            },
            {
                "name": "CAS_ENVIRONMENT_NAME",
                "inner": false,
                "value": "test_arm"
            },
            {
                "name": "AOM_DISCOVERY_RULE_VERSION",
                "inner": false,
                "value": "V1"
            },
            {
                "name": "CAS_ENVIRONMENT_ID",
                "inner": false,
                "value": "21a43958-f64f-4077-8ae3-3a03bf0111c7"
            },
            {
                "name": "testName",
                "inner": false,
                "value": "testValue"
            },
            {
                "name": "PAAS_REGION_NAME",
                "inner": false,
                "value": "cn-north-7"
            },
            {
                "name": "CAS_APPLICATION_NAME",
                "inner": false,
                "value": "app-test-application"
            }
        ],
        "deploy_strategy": {
            "type": "RollingRelease",
            "rolling_release": {
                "batches": "1"
            }
        },
        "id": "f3f6cb89-831d-4579-8bbc-664487889e38",
        "name": "etst1"
    }'

    返回结果:

    {
        "job_id": "JOB2209202b-617c-4591-b0c6-ef2521a943a2"
    }

    记录返回结果中加粗显示的job_id的值,用于替换后续示例中的{job_id}。

  4. 调用2的接口,用3返回的job_id查询任务状态,等待任务完成。
  5. 调用根据组件ID下发组件任务API回滚组件,请求示例如下:

    curl -k -H "Content-Type:application/json;charset=utf8" -H "X-Auth-Token:$token" -X POST "https://{servicestage_endpoint}/v3/{project_id}/cas/applications/{application_id}/components/{component_id}/action" -d ' 
    {
        "action": "rollback_current"
    }'

    返回结果:

    {
        "job_id": "JOB66eb56db-062e-4462-ba95-8d3c63d5d462"
    }

    记录返回结果中加粗显示的job_id的值,用于替换后续示例中的{job_id}。

  6. 调用2的接口,用5返回的job_id查询任务状态,等待任务完成。
  7. 调用根据组件ID下发组件任务API停止组件实例,请求示例如下:

    curl -k -H "Content-Type:application/json;charset=utf8" -H "X-Auth-Token:$token" -X POST "https://{servicestage_endpoint}/v3/{project_id}/cas/applications/{application_id}/components/{component_id}/action" -d ' 
    {
        "action": "stop"
    }'

    返回结果:

    {
        "job_id": "JOB2790bf9f-7d6e-473d-827f-1bb94e0cf10d"
    }

    记录返回结果中加粗显示的job_id的值,用于替换后续示例中的{job_id}。

  8. 调用2的接口,用7返回的job_id查询任务状态,等待任务完成。