<p>After a pod crashes and restarts, is it possible to retrieve the IP address of the pod prior to the crash?</p>

<p>This is a very wide question as not possible to identify where exactly and why pod crashes.
However I'll show what's possible to do with in different scenarios.</p>
<ul>
<li>Pod's container crashes and then restarts itself:</li>
</ul>
<p>In this case pod will save its IP address. The easiest way is to run
<code>kubectl get pods -o wide</code></p>
<p>Output will be like</p>
<pre><code>NAME                                        READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
nginx-deployment-699f9b54d-g2w97            0/1     CrashLoopBackOff   2          55s   10.244.1.53   worker1   &lt;none&gt;           &lt;none&gt;
</code></pre>
<p>As you can see even if container crashes, pod has assigned with IP address</p>
<p>Also it's possible to add <code>initContainers</code> and add a command which will get the IP address of the pod (depending on the image you will use, there are different options like <code>ip a</code>, <code>ifconfig -a</code> etc.</p>
<p>Here's a simple example how it can be added:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      initContainers: # here is where to add initContainer section
      - name: init-container
        image: busybox
        args: [/bin/sh, -c, &quot;echo IP ADDRESS ; ifconfig -a&quot;]
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
        command: [&quot;/sh&quot;, &quot;-c&quot;, &quot;nginx --version&quot;] #this is to make nginx image failing
</code></pre>
<p>Before your main container starts, this <code>init-container</code> will run an <code>ifconfig -a</code> command and will put its results into logs.</p>
<p>You can check it with:</p>
<p><code>kubectl logs %pod_name% -c init-container</code></p>
<p>Output will be:</p>
<pre><code>IP ADDRESS
eth0 Link encap:Ethernet HWaddr F6:CB:AD:D0:7E:7E inet addr:10.244.1.52 Bcast:10.244.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1410 Metric:1 RX packets:5 errors:0 dropped:0 overruns:0 frame:0 TX packets:1 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:398 (398.0 B) TX bytes:42 (42.0 B)
lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
</code></pre>
<p>Also you can check logs for previous running version of pod by adding <code>--previous</code> to the command above.</p>
<ul>
<li>Pod crashes and then is recreated
In this case new pod is created which means local logs are gone. You will need to think about saving them separately from pods. For this matter you can use <code>volumes</code>. E.g. <code>hostPath</code> will store logs on the node where pod runs or <code>nfs</code> can be attached to different pods and be accessed.</li>
<li>Control plane crashed while pods are still running
You can't access logs using control-plane and <code>kubectl</code> however your containers will still be running on the nodes. To get logs directly from nodes where your containers are running use <code>docker</code> or <code>crictl</code> depending on your runtime.</li>
</ul>
<p>Ideal solution for such cases is to use monitoring systems such as <code>prometheus</code> or <code>elasticseach</code>.
It will require additional set up of <a href="https://github.com/fluent/fluentd-kubernetes-daemonset" rel="nofollow noreferrer">fluentd</a> or <a href="https://github.com/kubernetes/kube-state-metrics" rel="nofollow noreferrer">kube-state-metrics</a></p>
