Interview Prep/Python for DevOps/Kubernetes, Docker, SSH & fleet automation

A Python script needs to run `kubectl` against three clusters. How do you manage contexts safely?

IntermediateScenarioKubernetes, Docker, SSH & fleet automation
Answer

Situation read → Risk of running against the wrong cluster.

What I'd do →

  1. Never rely on the current context; pass --context explicitly on every call, or config.load_kube_config(context=name) with the Python client.
  2. Print the target cluster and require confirmation (or a flag) for prod.
  3. Use separate kubeconfig files per environment via KUBECONFIG for CI jobs.

Why → "Current context" is shared mutable state; someone else's kubectl config use-context changes your target.

Interview tip → Mention explicit --context as a habit.

All Python for DevOps questions Roadmap: Python for automation

More on Kubernetes, Docker, SSH & fleet automation