Interview Prep/Python for DevOps/Kubernetes, Docker & SSH clients

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.

All Python for DevOps questions Roadmap: Python for automation

More on Kubernetes, Docker & SSH clients