<p>I need to communicate to a Pod if it's restarting or not. Because depending of the situation my app is working differently (stateful app). I wouldn't like to create other pod that runs a kind of watchdog and then informs my app if it's restarting or not (after a fault). But maybe there is a way to do it with Kubernetes components (Kubelet,..). </p>

LDom
<p>Quoting from Kubernetes Docs:</p>

<ol>
<li><blockquote>
  <p>Processes in containers inside pods can also contact the apiserver.
  When they do, they are authenticated as a particular Service Account
  (for example, default)</p>
</blockquote></li>
<li><blockquote>
  <p>A RoleBinding or ClusterRoleBinding binds a role to subjects. Subjects can be groups, users or ServiceAccounts.</p>
</blockquote></li>
<li><blockquote>
  <p>An RBAC Role or ClusterRole contains rules that represent a set of
  permissions. </p>
  
  <p>A Role always sets permissions within a particular namespace.</p>
  
  <p>ClusterRole, by contrast, is a non-namespaced resource</p>
</blockquote></li>
</ol>

<p>So, In-order to get/watch the status of the other pod, you can call Kubernetes API from the pod running your code by using serviceaccounts. Follow below steps in-order to automatically retrieve other pod status from a given pod without any external dependency (Due to reliability concerns, you shouldn't rely upon nodes)</p>

<p>Create a serviceaccount in your pod's (requestor pod) namespace</p>

<pre><code>kubectl create sa pod-reader
</code></pre>

<p>If both pods are in same namespace, create role,rolebinding</p>

<blockquote>
  <p>Create a role</p>

<pre><code>kubectl create role pod-reader --verb=get,watch --resource=pods
</code></pre>
  
  <p>Create a rolebinding</p>

<pre><code>kubectl create rolebinding pod-reader-binding --role=pod-reader --serviceaccount=&lt;NAMESPACE&gt;:pod-reader
</code></pre>
</blockquote>

<p>Else, i.e the pods are in different namespaces, create clusterrole,clusterrolebinding</p>

<blockquote>
  <p>Create a clusterrole</p>

<pre><code>kubectl create clusterrole pod-reader --verb=get,watch --resource=pods
</code></pre>
  
  <p>Create a rolebinding</p>

<pre><code>kubectl create clusterrolebinding pod-reader-binding --clusterrole=pod-reader --serviceaccount=&lt;NAMESPACE&gt;:pod-reader
</code></pre>
</blockquote>

<p>Verify the permissions</p>

<pre><code>kubectl auth can-i watch pods --as=system:serviceaccount:&lt;NAMESPACE&gt;:pod-reader
</code></pre>

<p>Now deploy your pod/(your app) with this serviceaccount.</p>

<pre><code>kubectl create &lt;MY-POD&gt; --image=&lt;MY-CONTAINER-IMAGE&gt; --serviceaccount=pod-reader
</code></pre>

<p>This will mount serviceaccount secret token in your pod, which can be found at <code>/var/run/secrets/kubernetes.io/serviceaccount/token</code>. Your app can use this token to make GET requests to Kubernetes API server in-order to get the status of the pod. See below example (this assumes your pod has curl utility installed. However, you can make a relevant API call from your code, pass the Header by reading the serviceaccount token file mounted in your pod).</p>

<pre><code>TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)

curl https://kubernetes.default/api/v1/namespaces/&lt;NAMESPACE&gt;/pods/&lt;NAME_OF_THE_OTHER_POD&gt; -H "Authorization: Bearer ${TOKEN}" -k

curl https://kubernetes.default/api/v1/watch/namespaces/&lt;NAMESPACE&gt;/pods/&lt;NAME_OF_THE_OTHER_POD&gt;?timeoutSeconds=30 -H "Authorization: Bearer ${TOKEN}" -k
</code></pre>

<p>References:</p>

<ol>
<li><p><a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.11/#-strong-read-operations-pod-v1-core-strong-" rel="nofollow noreferrer">Kubernetes API</a></p></li>
<li><p><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/" rel="nofollow noreferrer">serviceaccount</a></p></li>
</ol>
