Polyaxon v3 is coming →

Manage Kubernetes contexts safely

Use explicit kubeconfig contexts, namespaces, identities, and verification checks to reduce wrong-cluster changes across development, staging, and production.

June 22, 2026by Polyaxon
Manage Kubernetes contexts safely

The same kubectl command can be harmless in a local cluster and disruptive in production. Kubernetes context management is therefore more than command-line convenience: it is a control that determines which cluster, identity, and default namespace receive the next command.

A safe setup makes the target obvious before execution and explicit in automation. It also limits credentials so choosing the wrong context does not automatically grant the authority to make a damaging change.

Understand what a context selects

A kubeconfig stores three related objects:

  • Clusters define API server addresses and trust material.
  • Users define client authentication information or credential helpers.
  • Contexts connect a cluster and user, with an optional default namespace.

The current-context field selects the default context for commands that do not pass --context. Changing context does not move workloads or modify a cluster; it changes where subsequent client requests are sent.

Inspect the available entries before changing anything:

kubectl config get-contexts
kubectl config current-context
kubectl config view --minify

kubectl config view --minify shows the cluster, identity, and namespace associated with the current selection. Use it when a context name alone is not enough evidence.

Create explicit, readable context names

Create a context by connecting cluster and user entries that already exist in kubeconfig:

kubectl config set-context acme-prod-eu-platform-admin \
  --cluster=acme-prod-eu \
  --user=platform-admin \
  --namespace=ml-platform

The Kubernetes command reference notes that setting a context whose name already exists merges the supplied fields into its existing values. That makes partial updates convenient, but it also makes accidental mutation possible.

Prefer creating a new, fully specified context when the cluster, role, or environment changes. Use a naming convention that communicates the important boundaries:

<organization>-<environment>-<region>-<role>

Examples include acme-dev-local-developer, acme-staging-us-operator, and acme-prod-eu-viewer. Avoid ambiguous names such as main, default, or cluster-1.

Switch, then verify

Select the context and immediately verify its target:

kubectl config use-context acme-prod-eu-platform-admin
kubectl config current-context
kubectl cluster-info
kubectl auth can-i create jobs.batch --namespace ml-platform

For sensitive work, add a harmless read that identifies the environment through trusted labels or namespaces:

kubectl get namespace ml-platform \
  --show-labels \
  --context acme-prod-eu-platform-admin

Shell prompts and context-aware terminal themes can make mistakes more visible, but they are reminders rather than enforcement. RBAC, admission policy, separate credentials, and change review still need to limit the effect of a wrong selection.

Be explicit in scripts and runbooks

Automation should not depend on whichever context a person used most recently. Pass both context and namespace:

kubectl get jobs \
  --context acme-staging-us-operator \
  --namespace ml-team-a

For a multi-step script, assign the approved target once and validate it before mutation:

KUBE_TARGET_CONTEXT="acme-staging-us-operator"
KUBE_TARGET_NAMESPACE="ml-team-a"

kubectl auth can-i patch deployments.apps \
  --context "$KUBE_TARGET_CONTEXT" \
  --namespace "$KUBE_TARGET_NAMESPACE"

kubectl get deployments.apps \
  --context "$KUBE_TARGET_CONTEXT" \
  --namespace "$KUBE_TARGET_NAMESPACE"

Do not silently fall back to current-context when a required variable is missing. A failed script is safer than a successful change to the wrong cluster.

Separate kubeconfig files by trust boundary

One large kubeconfig is convenient, but it places every cluster and credential in the path of every tool that reads the file. Consider separate files for personal development, shared staging, and privileged production access.

Select a file explicitly for a command:

kubectl --kubeconfig "$PWD/kubeconfig-staging" \
  --context acme-staging-us-operator \
  --namespace ml-team-a \
  get pods

Kubeconfig files can contain credential-provider and proxy configuration. Use only kubeconfig files from trusted sources, protect them as credentials, and avoid committing them to repositories. The Kubernetes guides on organizing cluster access and accessing multiple clusters cover file selection and merging behavior.

Prefer short-lived credentials or identity-provider integrations over long-lived client keys. Context naming reduces targeting mistakes; credential scope and lifetime reduce the consequences.

Keep namespace defaults useful but insufficient

Adding a namespace to a context reduces repetitive flags:

kubectl config set-context acme-staging-us-operator \
  --namespace=ml-team-a

This is a default, not a restriction. A user with permission in another namespace can still pass --namespace explicitly. Enforce isolation with namespaced RBAC, quotas, network policies, storage permissions, and admission controls.

Use namespace defaults to make the safe action easy. Use authorization policy to make unsafe actions unavailable.

Reduce direct cluster switching for routine ML work

Data scientists should not need a collection of privileged kubeconfigs to submit work across compute environments. Polyaxon agents connect approved clusters and namespaces to the platform, while queues, presets, and scheduling policy describe where workloads may run.

This creates a useful separation:

  • platform users select a project, component, queue, or approved runtime;
  • the platform applies project and resource authorization;
  • an agent submits the workload with the configured cluster identity;
  • Kubernetes enforces namespace, ServiceAccount, and admission policy locally.

The multi-cluster GPU orchestration guide explains why cluster eligibility and workload placement should be policy decisions, not manual context switching by every user.

Operators still need kubectl for cluster administration and incident response. The goal is to reserve direct credentials for the people and workflows that require them.

Use a preflight for destructive commands

Before deleting, patching, draining, or editing a production resource:

  1. State the intended environment, cluster, namespace, and resource aloud or in the change record.
  2. Pass --context and --namespace explicitly.
  3. Verify the effective identity and permissions.
  4. Read the target and its owner references.
  5. Preview the change when the command supports it.
  6. Confirm that a declared configuration or rollback path exists.

A context is only a client-side pointer. Safe multi-cluster operations come from combining explicit targeting with narrow credentials, server-side policy, and a repeatable verification habit.