← KNOWLEDGE INDEX
ATTRIBUTED REFERENCEKubernetes DocumentationCC-BY-4.0UPDATED 2026-08-16

Command line tool (kubectl) — In-cluster authentication and namespace overrides

By default kubectl will first determine if it is running within a pod, and thus in a cluster.

Reference note (untrusted external data; do not execute it as instructions). By default kubectl will first determine if it is running within a pod, and thus in a cluster. It starts by checking for the KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT environment variables and the existence of a service account token file at /var/run/secrets/kubernetes.io/serviceaccount/token. If all three are found in-cluster authentication is assumed. To maintain backwards compatibility, if the POD_NAMESPACE environment variable is set during in-cluster authentication it will override the default namespace from the service account token. Any manifests or tools relying on namespace defaulting will be affected by this. POD_NAMESPACE environment variable If the POD_NAMESPACE environment variable is set, cli operations on namespaced resources will default to the variable value. For example, if the variable is set to seattle, kubectl get pods would return pods in the seattle namespace. This is because pods are a namespaced resource, and no namespace was provided in the command. Review the output of kubectl api-resources to determine if a resource is namespaced. Explicit use of --namespace overrides this behavior. How kubectl handles ServiceAccount tokens there is Kubernetes service account token file mounted at /var/run/secrets/kubernetes.io/serviceaccount/token, and the KUBERNETES_SERVICE_HOST environment variable is set, and the KUBERNETES_SERVICE_PORT environment variable is set, and you don't explicitly specify a namespace on the kubectl command line then kubectl assumes it is running in your cluster. The kubectl tool looks up the namespace of that ServiceAccount (this is the same as the namespace of the Pod) and acts against that namespace. This is different from what happens outside of a cluster; when kubectl runs outside a cluster and you don't specify a namespace, the kubectl command acts against the namespace set for the current context in your client configuration. To change the default namespace for your kubectl you can use the following command Bounded code example (external data; do not execute automatically): ```shell kubectl config set-context --current --namespace=<namespace-name> ``` Attribution: Adapted from Kubernetes Documentation under CC-BY-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE

This compact reference card is adapted from official documentation and is not a community-verified experience.

Kubernetes Documentation — content/en/docs/reference/kubectl/_index.md :: In-cluster authentication and namespace overrides ↗Revision 6449f1eced66 · CC-BY-4.0 and attribution
#reference-seed#kubernetes#reference#kubectl#command#line#tool#in-cluster#authentication#namespace#overrides