0

i.e. without having to configure any variable in the spec of the container? I imagine there might be some environment variables where the name of the Kubernetes namespace is exposed but I did not see anything in the documentation.

Wondering if this is possible at all or I'd need to inject the name of the namespace as a variable in the container spec.

1 Answer 1

2

You can use field reference.

Take a look on the example:

apiVersion: v1
kind: Pod
metadata:
  name: envars-fieldref-example
spec:
  containers:
    - name: test
      image: k8s.gcr.io/busybox
      command: [ "sh", "-c"]
      args:
      - while true; do
          echo -en '\n';
          printenv YOUR_POD_NAMESPACE, YOUR_POD_IP;
          sleep 10;
        done;
      env:
        - name: YOUR_POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: YOUR_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP

In the configuration file, you can see five environment variables. The env field is an array of EnvVars. The first element in the array specifies that the YOUR_POD_NAMESPACE environment variable gets its value from the Pod's metadata.namespace field. Similarly, the other environment variables get their names from Pod fields.

See more: env-pod.

1
  • Cool! Amazing. Thank you!
    – Jose
    Nov 12, 2020 at 8:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .