Azure-sdk-for-python AKS:如何使用下面的方法添加新的节点池?

问题描述 投票:0回答:1

我不明白从 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?

Gow我可以从这里继续创建或删除一个新的nodepool吗?

python azure kubernetes azure-kubernetes azure-sdk-python
1个回答
2
投票

你可以利用以下代码来创建nodepool。

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))

添加NodePool

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


0
投票

谢谢 Sajeetharan我找到了'az aks nodepool add'在 azure-cli 代码中的实现。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)
© www.soinside.com 2019 - 2024. All rights reserved.