Azure-sdk-for-python AKS-如何在 AKS 上升级 kubernetes
Azure-sdk-for-python AKS- How to upgrade kubernetes on AKS
我试图找出我可以使用哪个 python sdk 在 AKS 上升级 Kubernetes,换句话说如何通过 python sdk 运行 这个命令?
az aks upgrade --name mycluster --resource-group myrg --kubernetes-version newversion
但是我的搜索没有成功。
这是我的函数的一部分:init.py
import logging
from azure.mgmt.containerservice import ContainerServiceClient
from azure.identity import ClientSecretCredential
from azure.mgmt.subscription import SubscriptionClient
from azure.mgmt.resource import ResourceManagementClient
....
for group in list(group_list):
#print("ResourceGroupName:", group.name)
resource_list = resource_client.resources.list_by_resource_group(group.name)
print("Fetching AKS resource\n")
for resource in list(resource_list):
if resource.type== "Microsoft.ContainerService/managedClusters":
print("ResourceName:", resource.name, "ResourceType:", resource.type)
containerservice_client = ContainerServiceClient(credential, sub.subscription_id)
print("Getting availabele versions...\n")
aks_get_version = containerservice_client.managed_clusters.get_upgrade_profile(group.name, resource.name)
aks_get_upgrade = aks_get_version.control_plane_profile
upgrades = aks_get_upgrade.upgrades
print("AKS_current_controlPlane", aks_get_upgrade.kubernetes_version)
for i in upgrades:
print("AKSGetUpdate_upgrade_controlPlane", i.kubernetes_version,i.is_preview)
好像
managed_clusters.update
可能是一个解决方案,但问题是如何在其中传递 properties.kubernetesVersion 和 properties.agentPoolProfiles[0].orchestratorVersion 值?
parameters = {"properties.kubernetesVersion": i.kubernetes_version, "properties.agentPoolProfiles[0].orchestratorVersion": i.kubernetes_version}
containerservice_client.managed_clusters.create_or_update(group.name, resource.name, parameters)
但是我得到这个错误:
Exception: AttributeError: 'ManagedClustersOperations' object has no attribute 'create_or_update'
如果有人可以提供帮助,我将不胜感激。
已更新
根据给定的答案修改了代码,但它没有更新我的集群,也没有给我任何错误,我不知道我遗漏了什么以及如何在这个阶段进行故障排除:
print("Getting availabele versions on AKS:", resource.name)
aks_get_version = containerservice_client.managed_clusters.get_upgrade_profile(group.name, resource.name)
aks_get_upgrade = aks_get_version.control_plane_profile
upgrades = aks_get_upgrade.upgrades
print("AKS_current_controlPlane", aks_get_upgrade.kubernetes_version)
agent_pool=containerservice_client.agent_pools.list(group.name, resource.name)
for i in upgrades:
print("AKSAvailable_Versions_List:", i.kubernetes_version,i.is_preview)
latest_version = i.kubernetes_version
print("LatestVersion:", latest_version)
for x in agent_pool:
agent_pool_name=x.name
print("Agent_Pool_Name:",x.name)
ManagedClusterAgentPoolProfile(orchestrator_version=latest_version,name=agent_pool_name)
param=ManagedCluster(location=location,
kubernetes_version=latest_version,agent_pool_profiles=[ManagedClusterAgentPoolProfile(orchestrator_version=latest_version,name=agent_pool_name,mode=x.mode,type=x.type_properties_type)])
update_aks=containerservice_client.managed_clusters.begin_create_or_update(resource_group_name=group.name ,resource_name=resource.name ,parameters=param)
输出:
AKS_current_controlPlane 1.19.11
最新版本:1.20.9
Agent_Pool_Name: 默认
您可以使用我在我的环境中测试过的以下代码:
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.containerservice import ContainerServiceClient
from azure.mgmt.containerservice.models import (ManagedClusterAgentPoolProfile,
ManagedCluster)
credential = AzureCliCredential()
subscription_id = "my sub"
resource_group= 'rg name'
resouce_client=ResourceManagementClient(credential,subscription_id)
container_client=ContainerServiceClient(credential,subscription_id)
resouce_list=resouce_client.resources.list_by_resource_group(resource_group)
for resource in list (resouce_list) :
if resource.type == 'Microsoft.ContainerService/managedClusters':
kub_name=resource.name
location=resource.location
print("ResourceName:", kub_name, "ResourceLocation:",location )
get_aks = container_client.managed_clusters.get(resource_group, resource.name)
aks_get_details = container_client.managed_clusters.get_upgrade_profile(resource_group, resource.name)
aks_control_plane_upgrade = aks_get_details.control_plane_profile
upgrades = aks_control_plane_upgrade.upgrades
print("AKS_current_controlPlane_version:", aks_control_plane_upgrade.kubernetes_version)
agent_pool=container_client.agent_pools.list(resource_group_name=resource_group, resource_name=kub_name)
for i in upgrades:
available_version=i.kubernetes_version
print("Available_Versions_List:",available_version)
#version_to_upgrade=input("PLease enter the version you want to upgrade to from the above list:")
for x in agent_pool:
agent_pool_name=x.name
print("Agent_Pool_Name:",x.name)
param=ManagedCluster(location=location, kubernetes_version=available_version,agent_pool_profiles=[ManagedClusterAgentPoolProfile(orchestrator_version=available_version,name=agent_pool_name,mode=x.mode,type=x.type_properties_type)])
update_aks=container_client.managed_clusters.begin_create_or_update(resource_group_name=resource_group ,resource_name=kub_name ,parameters=param)
输出:
我试图找出我可以使用哪个 python sdk 在 AKS 上升级 Kubernetes,换句话说如何通过 python sdk 运行 这个命令?
az aks upgrade --name mycluster --resource-group myrg --kubernetes-version newversion
但是我的搜索没有成功。
这是我的函数的一部分:init.py
import logging
from azure.mgmt.containerservice import ContainerServiceClient
from azure.identity import ClientSecretCredential
from azure.mgmt.subscription import SubscriptionClient
from azure.mgmt.resource import ResourceManagementClient
....
for group in list(group_list):
#print("ResourceGroupName:", group.name)
resource_list = resource_client.resources.list_by_resource_group(group.name)
print("Fetching AKS resource\n")
for resource in list(resource_list):
if resource.type== "Microsoft.ContainerService/managedClusters":
print("ResourceName:", resource.name, "ResourceType:", resource.type)
containerservice_client = ContainerServiceClient(credential, sub.subscription_id)
print("Getting availabele versions...\n")
aks_get_version = containerservice_client.managed_clusters.get_upgrade_profile(group.name, resource.name)
aks_get_upgrade = aks_get_version.control_plane_profile
upgrades = aks_get_upgrade.upgrades
print("AKS_current_controlPlane", aks_get_upgrade.kubernetes_version)
for i in upgrades:
print("AKSGetUpdate_upgrade_controlPlane", i.kubernetes_version,i.is_preview)
好像
managed_clusters.update
可能是一个解决方案,但问题是如何在其中传递 properties.kubernetesVersion 和 properties.agentPoolProfiles[0].orchestratorVersion 值?
parameters = {"properties.kubernetesVersion": i.kubernetes_version, "properties.agentPoolProfiles[0].orchestratorVersion": i.kubernetes_version}
containerservice_client.managed_clusters.create_or_update(group.name, resource.name, parameters)
但是我得到这个错误:
Exception: AttributeError: 'ManagedClustersOperations' object has no attribute 'create_or_update'
如果有人可以提供帮助,我将不胜感激。
已更新
根据给定的答案修改了代码,但它没有更新我的集群,也没有给我任何错误,我不知道我遗漏了什么以及如何在这个阶段进行故障排除:
print("Getting availabele versions on AKS:", resource.name)
aks_get_version = containerservice_client.managed_clusters.get_upgrade_profile(group.name, resource.name)
aks_get_upgrade = aks_get_version.control_plane_profile
upgrades = aks_get_upgrade.upgrades
print("AKS_current_controlPlane", aks_get_upgrade.kubernetes_version)
agent_pool=containerservice_client.agent_pools.list(group.name, resource.name)
for i in upgrades:
print("AKSAvailable_Versions_List:", i.kubernetes_version,i.is_preview)
latest_version = i.kubernetes_version
print("LatestVersion:", latest_version)
for x in agent_pool:
agent_pool_name=x.name
print("Agent_Pool_Name:",x.name)
ManagedClusterAgentPoolProfile(orchestrator_version=latest_version,name=agent_pool_name)
param=ManagedCluster(location=location,
kubernetes_version=latest_version,agent_pool_profiles=[ManagedClusterAgentPoolProfile(orchestrator_version=latest_version,name=agent_pool_name,mode=x.mode,type=x.type_properties_type)])
update_aks=containerservice_client.managed_clusters.begin_create_or_update(resource_group_name=group.name ,resource_name=resource.name ,parameters=param)
输出: AKS_current_controlPlane 1.19.11 最新版本:1.20.9 Agent_Pool_Name: 默认
您可以使用我在我的环境中测试过的以下代码:
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.containerservice import ContainerServiceClient
from azure.mgmt.containerservice.models import (ManagedClusterAgentPoolProfile,
ManagedCluster)
credential = AzureCliCredential()
subscription_id = "my sub"
resource_group= 'rg name'
resouce_client=ResourceManagementClient(credential,subscription_id)
container_client=ContainerServiceClient(credential,subscription_id)
resouce_list=resouce_client.resources.list_by_resource_group(resource_group)
for resource in list (resouce_list) :
if resource.type == 'Microsoft.ContainerService/managedClusters':
kub_name=resource.name
location=resource.location
print("ResourceName:", kub_name, "ResourceLocation:",location )
get_aks = container_client.managed_clusters.get(resource_group, resource.name)
aks_get_details = container_client.managed_clusters.get_upgrade_profile(resource_group, resource.name)
aks_control_plane_upgrade = aks_get_details.control_plane_profile
upgrades = aks_control_plane_upgrade.upgrades
print("AKS_current_controlPlane_version:", aks_control_plane_upgrade.kubernetes_version)
agent_pool=container_client.agent_pools.list(resource_group_name=resource_group, resource_name=kub_name)
for i in upgrades:
available_version=i.kubernetes_version
print("Available_Versions_List:",available_version)
#version_to_upgrade=input("PLease enter the version you want to upgrade to from the above list:")
for x in agent_pool:
agent_pool_name=x.name
print("Agent_Pool_Name:",x.name)
param=ManagedCluster(location=location, kubernetes_version=available_version,agent_pool_profiles=[ManagedClusterAgentPoolProfile(orchestrator_version=available_version,name=agent_pool_name,mode=x.mode,type=x.type_properties_type)])
update_aks=container_client.managed_clusters.begin_create_or_update(resource_group_name=resource_group ,resource_name=kub_name ,parameters=param)
输出: