Skip to content

Commit b1b1395

Browse files
authored
Merge pull request #28695 from geoffcline/kubectl-namespace-patch-1
update desc of namespace defaulting in CLI
2 parents cdefcc3 + 57c0fe1 commit b1b1395

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

content/en/docs/reference/kubectl/overview.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@ Flags that you specify from the command line override default values and any cor
7171

7272
If you need help, run `kubectl help` from the terminal window.
7373

74+
## In-cluster authentication and namespace overrides
75+
76+
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.
77+
78+
To maintain backwards compatibility, if the `POD_NAMESPACE` environment variable is set during in-cluster authentication it will override the default namespace from the from the service account token. Any manifests or tools relying on namespace defaulting will be affected by this.
79+
80+
**`POD_NAMESPACE` environment variable**
81+
82+
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.
83+
84+
Explicit use of `--namespace <value>` overrides this behavior.
85+
86+
**How kubectl handles ServiceAccount tokens**
87+
88+
If:
89+
* there is Kubernetes service account token file mounted at
90+
`/var/run/secrets/kubernetes.io/serviceaccount/token`, and
91+
* the `KUBERNETES_SERVICE_HOST` environment variable is set, and
92+
* the `KUBERNETES_SERVICE_PORT` environment variable is set, and
93+
* you don't explicitly specify a namespace on the kubectl command line
94+
then kubectl assumes it is running in your cluster. The kubectl tool looks up the
95+
namespace of that ServiceAccount (this is the same as the namespace of the Pod)
96+
and acts against that namespace. This is different from what happens outside of a
97+
cluster; when kubectl runs outside a cluster and you don't specify a namespace,
98+
the kubectl command acts against the `default` namespace.
99+
74100
## Operations
75101

76102
The following table includes short descriptions and the general syntax for all of the `kubectl` operations:

0 commit comments

Comments
 (0)