kubectl config set-context: Command Reference for CI/CD
Create or modify a context, or pin a default namespace.
kubectl config set-context edits a context entry: which cluster, which user, and which default namespace. Pinning the namespace once means every later command does not need -n. This reference covers the flags and a CI example.
Common flags and usage
- set-context --current --namespace=<ns>: pin a default namespace
- set-context <name> --cluster=<c> --user=<u> --namespace=<ns>: define a context
- --current: edit the active context in place
- Pair with use-context to define then switch
- set-credentials / set-cluster define the referenced parts
Example
shell
kubectl config set-context --current --namespace=ci-${BUILD_ID}
kubectl get pods # no -n needed now
kubectl apply -f k8s/In CI
Pinning the namespace with --current --namespace at the top of a job removes a whole class of "wrong namespace" bugs from the rest of the script. It only edits the kubeconfig entry; the namespace must still exist for commands to succeed.
Key takeaways
- --current --namespace pins a default so later commands skip -n.
- set-context edits the kubeconfig entry; it does not create the namespace.
- Define a context then use-context to switch to it.
Related guides
kubectl Cheat Sheet: Commands for Everyday KubernetesA kubectl cheat sheet - get, describe, logs, apply, rollout, debug, and context commands for daily Kubernetes…
kubectl config use-context: Command Reference for CI/CDReference for kubectl config use-context: switch the active kubeconfig context in CI, verify with current-con…
kubectl create configmap: Command Reference for CI/CDReference for kubectl create configmap: build a ConfigMap from literals, files, or a directory, the create-pi…
kubectl get: Command Reference for CI/CDReference for kubectl get: list resources, output formats, label and field selectors, jsonpath extraction, an…