【问题标题】:Azure-sdk-for-python AKS: how to add a new nodepool usingAzure-sdk-for-python AKS:如何使用添加新节点池
【发布时间】:2020-08-20 22:00:23
【问题描述】:

我无法从 azure for python SDK 的文档中了解如何在现有 Kubernetes 集群中创建新的节点池? 在命令行中很容易做到:

az aks nodepool add --cluster-name CLUSTER-NAME
    --resource-group RESOURCE-GROUP
    --name NODE-NAME
    --enable-cluster-autoscaler
    --kubernetes-version 1.15.7
    --min-count 1
    --max-count 4
    --node-count 1
    --node-vm-size Standard_NC6s_v2

如何使用 python SDK 实现完全相同的命令? 目前,我可以像这样连接到客户端:

# pip install azure
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.containerservice import ContainerServiceClient
credentials = ServicePrincipalCredentials(CLIENT,KEY,tenant = TENANT_ID)
client = ContainerServiceClient(credentials, subscription_id)
credentialResults = client.managed_clusters.list_cluster_user_credentials(resource_group, aks_service_name)

# How can I continue from here to create or delete a new nodepool?

我可以从这里继续创建或删除新的节点池吗?

【问题讨论】:

    标签: python azure kubernetes azure-aks azure-sdk-python


    【解决方案1】:

    您可以使用以下代码创建节点池,

    def aks_scale(cmd, client, resource_group_name, name, node_count, nodepool_name="", no_wait=False):
        instance = client.get(resource_group_name, name)
        # TODO: change this approach when we support multiple agent pools.
        for agent_profile in instance.agent_pool_profiles:
            if agent_profile.name == nodepool_name or (nodepool_name == "" and len(instance.agent_pool_profiles) == 1):
                agent_profile.count = int(node_count)  # pylint: disable=no-member
                # null out the SP and AAD profile because otherwise validation complains
                instance.service_principal_profile = None
                instance.aad_profile = None
                return sdk_no_wait(no_wait, client.create_or_update, resource_group_name, name, instance)
        raise CLIError('The nodepool "{}" was not found.'.format(nodepool_name))
    

    添加节点池

    def aks_agentpool_add(cmd, client, resource_group_name, cluster_name, nodepool_name,
                          kubernetes_version=None,
                          zones=None,
                          enable_node_public_ip=False,
                          node_vm_size=None,
                          node_osdisk_size=0,
                          node_count=3,
                          vnet_subnet_id=None,
                          max_pods=0,
                          os_type="Linux",
                          min_count=None,
                          max_count=None,
                          enable_cluster_autoscaler=False,
                          node_taints=None,
                          tags=None,
                          labels=None,
                          mode="User",
                          no_wait=False):
        instances = client.list(resource_group_name, cluster_name)
        for agentpool_profile in instances:
            if agentpool_profile.name == nodepool_name:
                raise CLIError("Node pool {} already exists, please try a different name, "
                               "use 'aks nodepool list' to get current list of node pool".format(nodepool_name))
    
        taints_array = []
    
        if node_taints is not None:
            for taint in node_taints.split(','):
                try:
                    taint = taint.strip()
                    taints_array.append(taint)
                except ValueError:
                    raise CLIError('Taint does not match allowed values. Expect value such as "special=true:NoSchedule".')
    
        if node_vm_size is None:
            if os_type.lower() == "windows":
                node_vm_size = "Standard_D2s_v3"
            else:
                node_vm_size = "Standard_DS2_v2"
    
        agent_pool = AgentPool(
            name=nodepool_name,
            tags=tags,
            node_labels=labels,
            count=int(node_count),
            vm_size=node_vm_size,
            os_type=os_type,
            storage_profile=ContainerServiceStorageProfileTypes.managed_disks,
            vnet_subnet_id=vnet_subnet_id,
            agent_pool_type="VirtualMachineScaleSets",
            max_pods=int(max_pods) if max_pods else None,
            orchestrator_version=kubernetes_version,
            availability_zones=zones,
            enable_node_public_ip=enable_node_public_ip,
            node_taints=taints_array,
            mode=mode
        )
    
        _check_cluster_autoscaler_flag(enable_cluster_autoscaler, min_count, max_count, node_count, agent_pool)
    
        if node_osdisk_size:
            agent_pool.os_disk_size_gb = int(node_osdisk_size)
    
        return sdk_no_wait(no_wait, client.create_or_update, resource_group_name, cluster_name, nodepool_name, agent_pool)
    

    Github reference

    【讨论】:

    • 谢谢。这段代码似乎扩展了一个现有的节点池,但我怎样才能创建一个新的呢?我在哪里可以定义池的“大小”,如“Standard_NC6s_v2”
    • 感谢您的参考! - 我实际上找到了正确的命令 'aks_agentpool_add' - 不知道 azure cli 实际上是在使用 python-sdk
    • 酷,即使我是新来使用通过 sdk 做到这一点,很高兴你找到了
    【解决方案2】:

    感谢Sajeetharan,我在 azure-cli 代码中找到了“az aks nodepool add”的实现: GitHub ref: azure cli:

    def aks_agentpool_add(cmd, client, resource_group_name, cluster_name, nodepool_name,
                          kubernetes_version=None,
                          zones=None,
                          enable_node_public_ip=False,
                          node_vm_size=None,
                          node_osdisk_size=0,
                          node_count=3,
                          vnet_subnet_id=None,
                          max_pods=0,
                          os_type="Linux",
                          min_count=None,
                          max_count=None,
                          enable_cluster_autoscaler=False,
                          node_taints=None,
                          tags=None,
                          labels=None,
                          mode="User",
                          no_wait=False):
        instances = client.list(resource_group_name, cluster_name)
        for agentpool_profile in instances:
            if agentpool_profile.name == nodepool_name:
                raise CLIError("Node pool {} already exists, please try a different name, "
                               "use 'aks nodepool list' to get current list of node pool".format(nodepool_name))
    
        taints_array = []
    
        if node_taints is not None:
            for taint in node_taints.split(','):
                try:
                    taint = taint.strip()
                    taints_array.append(taint)
                except ValueError:
                    raise CLIError('Taint does not match allowed values. Expect value such as "special=true:NoSchedule".')
    
        if node_vm_size is None:
            if os_type.lower() == "windows":
                node_vm_size = "Standard_D2s_v3"
            else:
                node_vm_size = "Standard_DS2_v2"
    
        agent_pool = AgentPool(
            name=nodepool_name,
            tags=tags,
            node_labels=labels,
            count=int(node_count),
            vm_size=node_vm_size,
            os_type=os_type,
            storage_profile=ContainerServiceStorageProfileTypes.managed_disks,
            vnet_subnet_id=vnet_subnet_id,
            agent_pool_type="VirtualMachineScaleSets",
            max_pods=int(max_pods) if max_pods else None,
            orchestrator_version=kubernetes_version,
            availability_zones=zones,
            enable_node_public_ip=enable_node_public_ip,
            node_taints=taints_array,
            mode=mode
        )
    
        _check_cluster_autoscaler_flag(enable_cluster_autoscaler, min_count, max_count, node_count, agent_pool)
    
        if node_osdisk_size:
            agent_pool.os_disk_size_gb = int(node_osdisk_size)
    
        return sdk_no_wait(no_wait, client.create_or_update, resource_group_name, cluster_name, nodepool_name, agent_pool)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 2022-01-05
      • 1970-01-01
      • 2021-12-17
      相关资源
      最近更新 更多