1

how can I create a yaml file for pod that get its name from a environment variables defined in yaml file.

I have tried this but it is not allowed.

metadata:
  generateName: $(HOSTNAME)
.
.
.

env: 
  - name: HOSTNAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
1

Kubernetes allows you to set environment variables using the values of other fields in the Pod definition that are only available at the time the Pod is initiated.

In your example you tried to set spec.nodeName instead of metadata.name. You config should look something like this:

  env:
    - name: MY_POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name

Here you can find an official example of how to set it up.

Please let me know if that helped.

  • thank you OhHiMark. it works – esxzawq Jan 17 '20 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.