flyer
flyer

Reputation: 9816

How to get the current namespace in a Pod?

One way I could think of is to set an environment which value is the namespace of the Pod when defining the Pod.
Getting the namespace dynamically without requiring changes for Pod will be better because it lessens the burden of constructing a Pod.

So is there a way to get current namespace in a Pod?

Upvotes: 25

Views: 16691

Answers (2)

slintes
slintes

Reputation: 770

you don't need to set a static namespace env variable in the pod spec if you want to use env variables, you can use the "Downward API" for letting k8s fill it dynamically with the current namespace. See https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#the-downward-api

Upvotes: 8

Graham Dumpleton
Graham Dumpleton

Reputation: 58523

Try the file:

/var/run/secrets/kubernetes.io/serviceaccount/namespace

Upvotes: 55

Related Questions