gpt4 book ai didi

azure - 在 python 中的 aks 中缩放 pod

转载 作者:行者123 更新时间:2023-12-03 00:21:34 24 4
gpt4 key购买 nike

我目前可以在 Powershell 中使用以下代码来扩展我的 aks pod。

kubectl scale --replicas=5 部署/azure-vote-front

但是我怎样才能在Python中缩放它们呢?查遍了整个网络,但没有太大进展。任何有关这方面的线索都会非常有帮助。

最佳答案

使用patch_namespaced_deployment & patch_namespaced_deployment_scale api,需要传递body = {"spec":{"replicas":no.ofreplica}} 就像 this answer 中记录的那样

这是一个代码示例:

from kubernetes.client.rest import ApiException
from kubernetes import client,config

configuration = kubernetes.client.Configuration()
TOKENFILE='/run/secrets/kubernetes.io/serviceaccount/token'
with open(TOKENFILE, 'r') as file:
configuration.api_key['authorization'] = file.read()
configuration.api_key_prefix['authorization'] = 'Bearer'
configuration.host = "https://%s" % os.environ['KUBERNETES_SERVICE_HOST']
configuration.ssl_ca_cert="/run/secrets/kubernetes.io/serviceaccount/ca.crt"

aApiClient = kubernetes.client.ApiClient(configuration)
v1 = kubernetes.client.AppsV1Api(aApiClient)
deploymenttoscale="mydeploymentname"
mynamespace="statenamespacehere"

PAYLOAD={"spec":{"replicas":0}}
try:
print("scaling %s deployment down to 0" % ())
resultV1Scale = v1.patch_namespaced_deployment_scale(deploymenttoscale,mynamespace,PAYLOAD)
print('status: %s' % (resultV1Scale.status))
except ApiException as e:
print("Exception when calling CoreV1Api->delete_namespaced_pod: %s\n" % e)

示例输出:

scaling mydeploymentname deployment down to 0
status: {'replicas': 1, 'selector': 'app=xyz'}

关于azure - 在 python 中的 aks 中缩放 pod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68063215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com