How do you patch or scale a Deployment with the Python client?
IntermediateConceptKubernetes, Docker & SSH clients
Answer
apps = client.AppsV1Api()
apps.patch_namespaced_deployment_scale("api", "prod", {"spec": {"replicas": 5}})
# trigger a rolling restart, same as kubectl rollout restart
now = datetime.now(UTC).isoformat()
apps.patch_namespaced_deployment("api", "prod", {"spec": {"template": {"metadata": {
"annotations": {"kubectl.kubernetes.io/restartedAt": now}}}}})
A dict body is sent as a strategic merge patch by default. Errors raise kubernetes.client.exceptions.ApiException with .status (404, 409, 422) and .body.
More on Kubernetes, Docker & SSH clients
How does the official Kubernetes Python client authenticate?BeginnerHow do you list pods with high restart counts using the Python client?BeginnerHow does `watch.Watch()` work, and what is a `410 Gone`?AdvancedHow do you apply a YAML manifest from Python?AdvancedHow do you use the Docker SDK for Python?BeginnerHow do you clean up dangling Docker images and stopped containers with Python?Intermediate