<p>I am testing a log previous command and for that I need a pod to restart.</p>
<p>I can get my pods using a command like</p>
<pre class="lang-sh prettyprint-override"><code>kubectl get pods -n $ns -l $label
</code></pre>
<p>Which shows that my pods did not restart so far. I want to test the command:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl logs $podname -n $ns --previous=true
</code></pre>
<p>That command fails because my pod did not restart making the <code>--previous=true</code> switch meaningless.</p>
<p>I am aware of this command to restart pods when configuration changed:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl rollout restart deployment myapp -n $ns
</code></pre>
<p>This does not restart the containers in a way that is meaningful for my log command test but rather terminates the old pods and creates new pods (which have a restart count of 0).</p>
<p>I tried various versions of exec to see if I can shut them down from within but most commands I would use are not found in that container:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl exec $podname -n $ns -- shutdown
kubectl exec $podname -n $ns -- shutdown now
kubectl exec $podname -n $ns -- halt
kubectl exec $podname -n $ns -- poweroff
</code></pre>
<p>How can I use a <code>kubectl</code> command to forcefully restart the pod with it retaining its identity and the restart counter increasing by one so that my test log command has a previous instance to return the logs from.</p>
<p>EDIT:
Connecting to the pod is <a href="https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/" rel="nofollow noreferrer">well described</a>.</p>
<pre class="lang-sh prettyprint-override"><code>kubectl -n $ns exec --stdin --tty $podname -- /bin/bash
</code></pre>
<p>The process list shows only a handful running processes:</p>
<pre class="lang-sh prettyprint-override"><code>ls -1 /proc | grep -Eo &quot;^[0-9]{1,5}$&quot;
</code></pre>
<p>proc 1 seems to be the one running the pod.
<code>kill 1</code> does nothing, not even kill the proc with pid 1</p>
<p>I am still looking into this at the moment.</p>

<p>There are different ways to achieve your goal. I'll describe below most useful options.</p>
<h2>Crictl</h2>
<p>Most correct and efficient way - restart the pod on container runtime level.</p>
<p>I tested this on Google Cloud Platform - GKE and minikube with <code>docker</code> driver.</p>
<p>You need to <code>ssh</code> into the worker node where the pod is running. Then find it's <code>POD ID</code>:</p>
<pre><code>$ crictl ps
CONTAINER           IMAGE               CREATED             STATE           NAME                ATTEMPT             POD ID
9863a993e0396       87a94228f133e       3 minutes ago       Running         nginx-3             2                   6d17dad8111bc
</code></pre>
<p>OR</p>
<pre><code>$ crictl pods -s ready
POD ID              CREATED             STATE          NAME                    NAMESPACE       ATTEMPT         RUNTIME
6d17dad8111bc       About an hour ago   Ready          nginx-3                 default         2               (default)
</code></pre>
<p>Then stop it:</p>
<pre><code>$ crictl stopp 6d17dad8111bc
Stopped sandbox 6d17dad8111bc
</code></pre>
<p>After some time, <code>kubelet</code> will start this pod again (with different POD ID in CRI, however kubernetes cluster treats this pod as the same):</p>
<pre><code>$ crictl ps
CONTAINER           IMAGE               CREATED             STATE               NAME                        ATTEMPT             POD ID
f5f0442841899       87a94228f133e       41 minutes ago      Running             nginx-3                     3                   b628e1499da41
</code></pre>
<p>This is how it looks in cluster:</p>
<pre><code>$ kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
nginx-3                           1/1     Running   3          48m
</code></pre>
<p>Getting logs with <code>--previous=true</code> flag also confirmed it's the same POD for kubernetes.</p>
<h2>Kill process 1</h2>
<p>It works with most images, however not always.</p>
<p>E.g. I tested on simple pod with <code>nginx</code> image:</p>
<pre><code>$ kubectl get pods
NAME             READY   STATUS    RESTARTS   AGE
nginx            1/1     Running   0          27h

$ kubectl exec -it nginx -- /bin/bash
root@nginx:/# kill 1
root@nginx:/# command terminated with exit code 137

$ kubectl get pods
NAME             READY   STATUS    RESTARTS   AGE
nginx            1/1     Running   1          27h
</code></pre>
<h2>Useful link:</h2>
<ul>
<li><a href="https://kubernetes.io/docs/tasks/debug-application-cluster/crictl/" rel="nofollow noreferrer">Debugging Kubernetes nodes with crictl</a></li>
</ul>
