<p>So I'm researching how to use Kubernetes for my case. I installed it and played a bit.</p>

<p>The question is when the replication controller starts couple of replicas they have something like an id in their name:</p>

<ol>
<li>How unique is this id? Is it uniqueness for the lifetime of kubernetes? Is it unique across different kubernetes runs (i.e. if I restart kubernetes)?</li>
<li>How to pass this id to the app in the container? Can I specify some kind of template in the yaml so for example the id will be assigned to environment variable or something similar?</li>
<li>Alternatively is there a way for the app in the container to ask for this id?</li>
</ol>

<p>More explanation of the use case. I have an application that writes some session files inside a directory. I want to guarantee unique for the session ids in the system. This means if one app instance is running on VM1 and another instance on VM2, I want to prepend some kind of identifier to the ids like app-1-dajk4l and app-2-dajk4l, where app is the name of the app and 1, 2 is the instance identifier, which should come from the replication controller because it is dynamic and can not be configured manually. dajk4l is some identifier like the current timestamp or similar.</p>

<p>Thanks.</p>

<ol>
<li><p>The ID is guaranteed to be unique at any single point in time, since Kubernetes doesn't allow two pods in the same namespace to have the same name. There aren't any longer-term guarantees though, since they're just <a href="https://github.com/kubernetes/kubernetes/blob/7c9bbef96ed7f2a192a1318aa312919b861aee00/pkg/api/generate.go#L59" rel="noreferrer">generated as a random string of 5 alphanumeric characters</a>. However, given that there are more than 60 million such random strings, conflicts across time are also unlikely in most environments.</p></li>
<li><p>Yes, you can pull in the pod's namespace and name as environment variables using what's called the <a href="https://github.com/kubernetes/kubernetes/blob/release-1.0/docs/user-guide/downward-api.md" rel="noreferrer">"Downward API"</a>, adding a field on the container like
<code>
  env:
    - name: MY_POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
</code></p></li>
</ol>
