简体   繁体   中英

Scale deployment with python client in Kubernetes

I am trying to scale deployments using Kubernetes python client. The cluster is running on 3 nodes and I have remote access via kubectl. I need to scale my deployment based on a python script via Kubernetes python client. I am trying the following code but I get an error. I couldn't figure out the issue. Any suggestions? Thank you.

from kubernetes import client, config

replicas=3

config.load_kube_config()
k8s_apps_v1 = client.AppsV1Api()

container = client.V1Container(
    name="trafficapp-deployment",
    image="traffic-app",#docker repository name?
    ports=[client.V1ContainerPort(container_port=5000)],
    )

template = client.V1PodTemplateSpec(
    metadata=client.V1ObjectMeta(labels={"app": "trafficapp"}),
    spec=client.V1PodSpec(containers=[container]))

spec = client.V1DeploymentSpec(
    replicas=replicas,
    template=template,
    selector={'matchLabels': {'app': 'trafficapp'}})

deployment = client.V1Deployment(
    api_version="apps/v1",
    kind="Deployment",
    metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME),
    spec=spec)
deployment.spec.replicas=replicas

api_response = k8s_apps_v1.patch_namespaced_deployment(
    name=DEPLOYMENT_NAME,
    namespace="default",
    body=deployment)

print("Deployment updated. status='%s'" % str(api_response.status))

Error I am getting -

Traceback (most recent call last):
File "/home/k8/Desktop/K8 Python client/test_pyclient.py", line 31, in <module>
api_response = k8s_apps_v1.patch_namespaced_deployment(
File "/home/k8/.local/lib/python3.10/site-packages/kubernetes/client 
/api/apps_v1_api.py", line 4627, in patch_namespaced_deployment
return self.patch_namespaced_deployment_with_http_info(name, namespace, body, **kwargs)
 # noqa: E501
File "/home/k8/.local/lib/python3.10/site-packages/kubernetes/client
/api/apps_v1_api.py", line 4742, in patch_namespaced_deployment_with_http_info
return self.api_client.call_api(
File "/home/k8/.local/lib/python3.10/site-packages/kubernetes/client/api_client.py",
line 348, in call_api
return self.__call_api(resource_path, method,
File "/home/k8/.local/lib/python3.10/site-packages/kubernetes/client/api_client.py", 
line 180, in __call_api
response_data = self.request(
File "/home/k8/.local/lib/python3.10/site-packages/kubernetes/client/api_client.py", 
line 407, in request
return self.rest_client.PATCH(url,
File "/home/k8/.local/lib/python3.10/site-packages/kubernetes/client/rest.py", line 
295, in PATCH
return self.request("PATCH", url,
File "/home/k8/.local/lib/python3.10/site-packages/kubernetes/client/rest.py", line 
234, in request
raise ApiException(http_resp=r)
kubernetes.client.exceptions.ApiException: (404)
Reason: Not Found
HTTP response headers: HTTPHeaderDict({'Audit-Id': 'b4523864-3ea8-4857- 
a1ac-59034d630644', 'Cache-Control': 'no-cache, private', 'Content-Type':  
'application/json', 'X-Kubernetes-Pf-Flowschema-Uid': 'f90063cc-3ae9-45ed-aed6-
d6ea009c6b10', 'X-Kubernetes-Pf-Prioritylevel-Uid': 'cda270d5-00fc-4012-
bb7e-58a85a29842f', 'Date': 'Wed, 07 Sep 2022 14:20:23 GMT', 'Content-Length': '232'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":
{},"status":"Failure","message":"deployments.apps \"DEPLOYMENT_NAME\" not 
found","reason":"NotFound","details":
{"name":"DEPLOYMENT_NAME","group":"apps","kind":"deployments"},"code":404}

You did not specify variable DEPLOYMENT_NAME

Specify

DEPLOYMENT_NAME="your deployment name" and try below snippet.

api_response= k8s_apps_v1.patch_namespaced_deployment_scale(name=DEPLOYMENT_NAME,
                                           namespace="default",
                                          {'spec': {'replicas': replicas}} )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM