Need a deployment name passed to related pods

10/30/2019

I have docker image containing a nodejs app and I deployed it via kubernetes deployment and I have 3 pods. What I need is to pass the name of the each deployment to the related pods so this way I have a unique id for each deployment which is unique to only that deployment and all pods inside that deployment can consume that id. Also this one did not help much:

Kubernetes deployment name from within a pod?

I know for the fact that I can do the following in :

env:
  - name: unique id
    value: {{uuidv4}}

But I would rather the following:

env:
  - name: deployment name
    value: a way to get the deployment name

Any idea if it is even achievable?

-- Learner
kubernetes
kubernetes-pod
openshift

1 Answer

10/30/2019

So from the Kubernetes deployment documentation, one use case is:

Declare the new state of the Pods by updating the PodTemplateSpec of the Deployment. A new ReplicaSet is created and the Deployment manages moving the Pods from the old ReplicaSet to the new one at a controlled rate. Each new ReplicaSet updates the revision of the Deployment.

So knowing the deploying is not relevant unless you want to rollback. So what you need is to get the pods to see each other. In that case, you need a headless service.

https://dev.to/kaoskater08/building-a-headless-service-in-kubernetes-3bk8

There you can get the pod DNS and tag them in your Redis by IP or DNS

EDIT:

For getting the deployment, every pod has an env var called HOSTNAME, for example (in my environment):

HOSTNAME=stella-api-8675fcf6df-rm2m7

Where stella-api is the current deployment name, stella-api-8675fcf6df is the current replica set and finally, stella-api-8675fcf6df-rm2m7 would be the pod specific id

Hope this helps.

-- paltaa
Source: StackOverflow