How to pass instance instance of kubernetes pod inside container at startup?

So, I am learning how to use Kubernetes for my business. I installed it and played a little.

The question is, when the replication controller starts a couple of replicas, they have something like id in their name:

  • How unique is this identifier? Is it unique to the life of the kubernets? Is it unique for different launches of kubernets (i.e. if I restart kubernets)?
  • How to transfer this identifier to the application in the container? Can I specify some kind of template in yaml, for example, the identifier will be assigned to an environment variable or something like that?
  • Alternatively, is there a way for an application in a container to request this identifier?

A more detailed description of the use case. I have an application that writes some session files inside a directory. I want to guarantee the uniqueness of session identifiers in the system. This means that if one instance of the application is running on VM1 and another instance on VM2, I want to add some kind of identifier to identifiers like app-1-dajk4l and app-2-dajk4l, where the application is the name of the application and 1, 2 - the identifier of the instance that should come from the replication controller, because it is dynamic and cannot be configured manually. dajk4l is some identifier similar to the current timestamp or the like.

Thanks.

+5
source share
1 answer
  • The identifier is guaranteed to be unique at any given time, since Kubernetes does not allow the use of two modules in the same namespace with the same name. However, no long-term guarantees exist, since they are simply generated as a random string of 5 alphanumeric characters . However, given that there are more than 60 million such random strings, time conflicts are also unlikely in most environments.

  • Yes, you can use the pod namespace and name as environment variables using what is called the "Downward API" by adding a field on the container like env: - name: MY_POD_NAME valueFrom: fieldRef: fieldPath: metadata.name

+8
source

All Articles