You need to enable JavaScript to run this app.
导航
Python SDK
最近更新时间:2023.10.17 16:55:32首次发布时间:2022.08.02 22:04:40

本文主要描述容器服务 Python 语言 SDK 的安装和配置,及使用 Python SDK 调用云端 API 的示例。

使用说明

仅支持 Python 2.7 及以上版本。

安装配置

请通过 volcengine-python-sdk 安装配置。

SDK 示例

创建集群(CreateCluster)

下方示例中参数的详细说明及配置方法,请参见 CreateCluster

# Example Code generated by Beijing Volcanoengine Technology.
from __future__ import print_function
import volcenginesdkcore
import volcenginesdkvke
from volcenginesdkcore.rest import ApiException

if __name__ == '__main__':
    # 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "Your AK"
    configuration.sk = "Your SK"
    configuration.region = "Your Region"
    # set default configuration
    volcenginesdkcore.Configuration.set_default(configuration)

    # use global default configuration
    api_instance = volcenginesdkvke.VKEApi()
    req_public_access_network_config = volcenginesdkvke.PublicAccessNetworkConfigForCreateClusterInput(
        bandwidth=10,
        billing_type=3,
    )
    req_api_server_public_access_config = volcenginesdkvke.ApiServerPublicAccessConfigForCreateClusterInput(
        public_access_network_config=req_public_access_network_config,
    )
    req_cluster_config = volcenginesdkvke.ClusterConfigForCreateClusterInput(
        api_server_public_access_config=req_api_server_public_access_config,
        api_server_public_access_enabled=True,
        resource_public_access_default_enabled=False,
        subnet_ids=["subnet-id"],
    )
    req_flannel_config = volcenginesdkvke.FlannelConfigForCreateClusterInput(
        max_pods_per_node=64,
        pod_cidrs=["172.17.**.**/19"],
    )
    req_pods_config = volcenginesdkvke.PodsConfigForCreateClusterInput(
        flannel_config=req_flannel_config,
        pod_network_mode="Flannel",
    )
    req_services_config = volcenginesdkvke.ServicesConfigForCreateClusterInput(
        service_cidrsv4=["172.25.**.**/17"],
    )
    req_tags = volcenginesdkvke.TagForCreateClusterInput(
        key="tag-key",
        value="tag-value",
    )
    create_cluster_request = volcenginesdkvke.CreateClusterRequest(
        client_token="client-token",
        cluster_config=req_cluster_config,
        delete_protection_enabled=False,
        description="cluster-description",
        kubernetes_version="1.24",
        name="cluster-name",
        pods_config=req_pods_config,
        services_config=req_services_config,
        tags=[req_tags],
    )
    
    try:
        # 复制代码运行示例,请自行打印 API 返回值。
        api_instance.create_cluster(create_cluster_request)
    except ApiException as e:
        # 复制代码运行示例,请自行打印 API 错误信息。
        # print("Exception when calling api: %s\n" % e)
        pass

更新集群配置(UpdateClusterConfig)

下方示例中参数的详细说明及配置方法,请参见 UpdateClusterConfig

# Example Code generated by Beijing Volcanoengine Technology.
from __future__ import print_function
import volcenginesdkcore
import volcenginesdkvke
from volcenginesdkcore.rest import ApiException

if __name__ == '__main__':
    # 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "Your AK"
    configuration.sk = "Your SK"
    configuration.region = "Your Region"
    # set default configuration
    volcenginesdkcore.Configuration.set_default(configuration)

    # use global default configuration
    api_instance = volcenginesdkvke.VKEApi()
    req_public_access_network_config = volcenginesdkvke.PublicAccessNetworkConfigForUpdateClusterConfigInput(
        bandwidth=100,
        billing_type=3,
    )
    req_api_server_public_access_config = volcenginesdkvke.ApiServerPublicAccessConfigForUpdateClusterConfigInput(
        public_access_network_config=req_public_access_network_config,
    )
    req_cluster_config = volcenginesdkvke.ClusterConfigForUpdateClusterConfigInput(
        api_server_public_access_config=req_api_server_public_access_config,
        api_server_public_access_enabled=True,
    )
    update_cluster_config_request = volcenginesdkvke.UpdateClusterConfigRequest(
        client_token="client-token",
        cluster_config=req_cluster_config,
        description="cluster-description",
        id="cluster-id",
        name="cluster-name",
    )
    
    try:
        # 复制代码运行示例,请自行打印 API 返回值。
        api_instance.update_cluster_config(update_cluster_config_request)
    except ApiException as e:
        # 复制代码运行示例,请自行打印 API 错误信息。
        # print("Exception when calling api: %s\n" % e)
        pass

获取集群列表及详情(ListClusters)

下方示例中参数的详细说明及配置方法,请参见 ListClusters

# Example Code generated by Beijing Volcanoengine Technology.
from __future__ import print_function
import volcenginesdkcore
import volcenginesdkvke
from volcenginesdkcore.rest import ApiException

if __name__ == '__main__':
    # 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "Your AK"
    configuration.sk = "Your SK"
    configuration.region = "Your Region"
    # set default configuration
    volcenginesdkcore.Configuration.set_default(configuration)

    # use global default configuration
    api_instance = volcenginesdkvke.VKEApi()
    req_filter = volcenginesdkvke.FilterForListClustersInput(
        ids=["cluster-id"],
    )
    list_clusters_request = volcenginesdkvke.ListClustersRequest(
        filter=req_filter,
        page_number=1,
        page_size=10,
    )
    
    try:
        # 复制代码运行示例,请自行打印 API 返回值。
        api_instance.list_clusters(list_clusters_request)
    except ApiException as e:
        # 复制代码运行示例,请自行打印 API 错误信息。
        # print("Exception when calling api: %s\n" % e)
        pass

创建节点池(CreateNodePool)

下方示例中参数的详细说明及配置方法,请参见 CreateNodePool

# Example Code generated by Beijing Volcanoengine Technology.
from __future__ import print_function
import volcenginesdkcore
import volcenginesdkvke
from volcenginesdkcore.rest import ApiException

if __name__ == '__main__':
    # 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "Your AK"
    configuration.sk = "Your SK"
    configuration.region = "Your Region"
    # set default configuration
    volcenginesdkcore.Configuration.set_default(configuration)

    # use global default configuration
    api_instance = volcenginesdkvke.VKEApi()
    req_auto_scaling = volcenginesdkvke.AutoScalingForCreateNodePoolInput(
        desired_replicas=1,
        enabled=True,
        max_replicas=100,
        min_replicas=0,
        priority=10,
    )
    req_labels = volcenginesdkvke.LabelForCreateNodePoolInput(
        key="label-key",
        value="label-value",
    )
    req_taints = volcenginesdkvke.TaintForCreateNodePoolInput(
        effect="NoSchedule",
        key="taint-key",
        value="taint-value",
    )
    req_kubernetes_config = volcenginesdkvke.KubernetesConfigForCreateNodePoolInput(
        cordon=False,
        labels=[req_labels],
        taints=[req_taints],
    )
    req_data_volumes = volcenginesdkvke.DataVolumeForCreateNodePoolInput(
        size=20,
        type="ESSD_PL0",
    )
    req_login = volcenginesdkvke.LoginForCreateNodePoolInput(
        password="***",
    )
    req_security = volcenginesdkvke.SecurityForCreateNodePoolInput(
        login=req_login,
        security_strategies=["Hids"],
    )
    req_system_volume = volcenginesdkvke.SystemVolumeForCreateNodePoolInput(
        size=40,
        type="ESSD_PL0",
    )
    req_tags = volcenginesdkvke.TagForCreateNodePoolInput(
        key="tag-key",
        value="tag-value",
    )
    req_node_config = volcenginesdkvke.NodeConfigForCreateNodePoolInput(
        data_volumes=[req_data_volumes],
        image_id="image-id",
        instance_charge_type="PostPaid",
        instance_type_ids=["instance-type"],
        name_prefix="prefix",
        security=req_security,
        subnet_ids=["subnet-id"],
        system_volume=req_system_volume,
        tags=[req_tags],
    )
    req_tags1 = volcenginesdkvke.TagForCreateNodePoolInput(
        key="tag-key",
        value="tag-value",
    )
    create_node_pool_request = volcenginesdkvke.CreateNodePoolRequest(
        auto_scaling=req_auto_scaling,
        client_token="client-token",
        cluster_id="cluster-id",
        kubernetes_config=req_kubernetes_config,
        name="nodepool-name",
        node_config=req_node_config,
        tags=[req_tags1],
    )
    
    try:
        # 复制代码运行示例,请自行打印 API 返回值。
        api_instance.create_node_pool(create_node_pool_request)
    except ApiException as e:
        # 复制代码运行示例,请自行打印 API 错误信息。
        # print("Exception when calling api: %s\n" % e)
        pass

更新节点池配置(UpdateNodePoolConfig)

下方示例中参数的详细说明及配置方法,请参见 UpdateNodePoolConfig

# Example Code generated by Beijing Volcanoengine Technology.
from __future__ import print_function
import volcenginesdkcore
import volcenginesdkvke
from volcenginesdkcore.rest import ApiException

if __name__ == '__main__':
    # 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "Your AK"
    configuration.sk = "Your SK"
    configuration.region = "Your Region"
    # set default configuration
    volcenginesdkcore.Configuration.set_default(configuration)

    # use global default configuration
    api_instance = volcenginesdkvke.VKEApi()
    req_auto_scaling = volcenginesdkvke.AutoScalingForUpdateNodePoolConfigInput(
        desired_replicas=10,
    )
    update_node_pool_config_request = volcenginesdkvke.UpdateNodePoolConfigRequest(
        auto_scaling=req_auto_scaling,
        client_token="client-token",
        cluster_id="cluster-id",
        id="nodepool-id",
        name="nodepool-name",
    )
    
    try:
        # 复制代码运行示例,请自行打印 API 返回值。
        api_instance.update_node_pool_config(update_node_pool_config_request)
    except ApiException as e:
        # 复制代码运行示例,请自行打印 API 错误信息。
        # print("Exception when calling api: %s\n" % e)
        pass

查询节点池列表及详情(ListNodePools)

下方示例中参数的详细说明及配置方法,请参见 ListNodePools

# Example Code generated by Beijing Volcanoengine Technology.
from __future__ import print_function
import volcenginesdkcore
import volcenginesdkvke
from volcenginesdkcore.rest import ApiException

if __name__ == '__main__':
    # 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "Your AK"
    configuration.sk = "Your SK"
    configuration.region = "Your Region"
    # set default configuration
    volcenginesdkcore.Configuration.set_default(configuration)

    # use global default configuration
    api_instance = volcenginesdkvke.VKEApi()
    req_filter = volcenginesdkvke.FilterForListNodePoolsInput(
        cluster_ids=["cluster-id"],
        ids=["nodepool-id"],
    )
    list_node_pools_request = volcenginesdkvke.ListNodePoolsRequest(
        filter=req_filter,
        page_number=1,
        page_size=10,
    )
    
    try:
        # 复制代码运行示例,请自行打印 API 返回值。
        api_instance.list_node_pools(list_node_pools_request)
    except ApiException as e:
        # 复制代码运行示例,请自行打印 API 错误信息。
        # print("Exception when calling api: %s\n" % e)
        pass

安装组件(CreateAddon)

下方示例中参数的详细说明及配置方法,请参见 CreateAddon

# Example Code generated by Beijing Volcanoengine Technology.
from __future__ import print_function
import volcenginesdkcore
import volcenginesdkvke
from volcenginesdkcore.rest import ApiException

if __name__ == '__main__':
    # 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "Your AK"
    configuration.sk = "Your SK"
    configuration.region = "Your Region"
    # set default configuration
    volcenginesdkcore.Configuration.set_default(configuration)

    # use global default configuration
    api_instance = volcenginesdkvke.VKEApi()
    create_addon_request = volcenginesdkvke.CreateAddonRequest(
        client_token="client-token",
        cluster_id="cluster-id",
        deploy_mode="Managed",
        deploy_node_type="Node",
        name="cluster-autoscaler",
    )
    
    try:
        # 复制代码运行示例,请自行打印 API 返回值。
        api_instance.create_addon(create_addon_request)
    except ApiException as e:
        # 复制代码运行示例,请自行打印 API 错误信息。
        # print("Exception when calling api: %s\n" % e)
        pass