kubernetes 尝试请求Knative自动缩放设置更新

jw5wzhpr  于 2023-10-17  发布在  Kubernetes
关注(0)|答案(1)|浏览(78)

我有一个安装了knative的AKS集群应用程序,我试图请求更新其自动缩放设置。我目前正在做这个:

def update_service_autoscaling(self, service_name: str, namespace: str, min_scale: int, max_scale: int):
        uri = f"https://{self.api_server_url}/apis/serving.knative.dev/v1/namespaces/{namespace}/services/{service_name}"
        
        headers = {
        "Authorization": f"Bearer {self.api_server_token}",
        "content-type": "application/json-patch+json",
    }

        patch_data = [
        {
            "op": "add",
            "path": "/spec/template/metadata/annotations/autoscaling.knative.dev~1min-scale",
            "value": str(min_scale),
        },
        {
            "op": "add",
            "path": "/spec/template/metadata/annotations/autoscaling.knative.dev~1max-scale",
            "value": str(max_scale),
        }
    ]

        response = requests.patch(
        uri,
        headers=headers,
        verify=False,
        json=patch_data,
    )

        if response.status_code != 200:
            exception_message = f"HTTP {response.status_code}: {response.json()['message']}"
            raise Exception(exception_message)

        return response.json()

从理论上讲,它应该工作,所有的参数都被正确地达到,但当我发出请求时,我得到“HTTP 422:服务器拒绝了我们的请求,因为我们的请求中存在错误。我已经通过VSCode调试,但找不到它的问题。

plicqrtu

plicqrtu1#

Knative服务通过向Knative服务的API发送PATCH请求来自动缩放设置。它独立于Azure服务,可与任何安装了Knative的Kubernetes集群一起使用。HTTP 422错误代码表示服务器理解请求,但由于客户端错误而无法处理。检查URIAuthorization标头和patch_data

错误处理:

if response.status_code != 200:
    exception_message = f"HTTP {response.status_code}: {response.text}"
    print(exception_message)  # Print the error message for debugging
    raise Exception(exception_message)

我还交叉检查了使用Azure SDK管理Azure AKS集群。它为AKS群集的代理池启用自动缩放,并为池中的节点设置最小和最大计数。此代码特定于Azure AKS,用于更新AKS群集设置,两者都会导致相同的响应。

from azure.identity import DefaultAzureCredential
from azure.mgmt.containerinstance import ContainerInstanceManagementClient
from azure.mgmt.containerinstance.models import ContainerGroup
from azure.mgmt.containerservice import ContainerServiceClient
from azure.core.exceptions import ResourceNotFoundError

# Authenticate using default credentials
credential = DefaultAzureCredential()

# Specify your Azure subscription ID and resource group name
subscription_id = ' '
resource_group_name = ' '

# Initialize the Container Instance management client
container_instance_client = ContainerInstanceManagementClient(credential, subscription_id)

# Initialize the AKS management client
aks_client = ContainerServiceClient(credential, subscription_id)

# Specify your AKS cluster name
aks_cluster_name = ' '

try:
    # Get the AKS cluster resource
    aks_cluster = aks_client.managed_clusters.get(resource_group_name, aks_cluster_name)

    # Print auto scale settings before the update
    print(f"Before Update - enable_auto_scaling: {aks_cluster.agent_pool_profiles[0].enable_auto_scaling}, "
          f"min_count: {aks_cluster.agent_pool_profiles[0].min_count}, "
          f"max_count: {aks_cluster.agent_pool_profiles[0].max_count}")

    # Enable autoscaling for the agent pool and set min_count and max_count
    aks_cluster.agent_pool_profiles[0].enable_auto_scaling = True
    aks_cluster.agent_pool_profiles[0].min_count = 1
    aks_cluster.agent_pool_profiles[0].max_count = 3

    # Update the AKS cluster with the modified properties
    poller = aks_client.managed_clusters.begin_create_or_update(resource_group_name, aks_cluster_name, aks_cluster)
    aks_cluster = poller.result()

    # Print auto scale settings after the update
    print(f"After Update - enable_auto_scaling: {aks_cluster.agent_pool_profiles[0].enable_auto_scaling}, "
          f"min_count: {aks_cluster.agent_pool_profiles[0].min_count}, "
          f"max_count: {aks_cluster.agent_pool_profiles[0].max_count}")

    print(f"Updated autoscale settings for AKS cluster {aks_cluster_name}.")

except ResourceNotFoundError:
    print(f"AKS cluster {aks_cluster_name} not found in resource group {resource_group_name}.")
except Exception as e:
    print(f"An error occurred while updating the AKS cluster: {str(e)}")

相关问题