<p>I am attempting to have a kubernetes nginx deployment with zero downtime. Part of that process has been to initiate a rollingUpdate, which ensures that at least one pod is running nginx at all times. This works perfectly well.</p>

<p>I am running into errors when the old nginx pod is terminating. 
According to the kubernetes docs on <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods" rel="noreferrer">termination</a>, kubernetes will:</p>

<ol>
<li>remove the pod from the endpoints list for the service, so it is
not receiving any new traffic when termination begins </li>
<li>invoke a pre-stop hook if it is defined, and wait for it to complete </li>
<li>send SIGTERM to all remaining processes</li>
<li>send SIGKILL to any remaining processes after the grace period expires.</li>
</ol>

<p>I understand that the command <code>nginx -s quit</code> is supposed to gracefully terminate nginx by waiting for all workers to complete requests before the master terminates. It responds gracefully to the SIGQUIT command, while SIGTERM results in violent termination. Other forums say that it is as easy as adding the following preStop hook to your deployment:</p>

<pre><code>lifecycle:
  preStop:
    exec:
      command: ["/usr/sbin/nginx", "-s", "quit"]
</code></pre>

<p>However, from testing this command I have found that <code>nginx -s quit</code> returns immediately, instead of waiting for the workers to complete. It also does not return the PID of the master process, which is what I was hoping for D:</p>

<p>What happens is, kubernetes invokes <code>nginx -s quit</code>, which will send a proper SIGQUIT to the worker children, but not wait for them to complete. Instead it will jump right to step 3 and SIGTERM those processes instead, resulting in violent termination, and thus, lost connections.</p>

<p><strong>QUESTION</strong>: Has anyone figured out a good way to gracefully shut down their nginx controller during a rolling deployment and have zero downtime? A <code>sleep</code> workaround isn't good enough, I'm looking for something more robust.</p>

<p>Below is the full deployment yaml:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-ingress-controller
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
 template:
    metadata:
      labels:
        app: nginx-ingress-lb
    spec:
      terminationGracePeriodSeconds: 60
      serviceAccount: nginx
      containers:
        - name: nginx-ingress-controller
          image: gcr.io/google_containers/nginx-ingress-controller:0.9.0-beta.8
          imagePullPolicy: Always
          readinessProbe:
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
          livenessProbe:
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            initialDelaySeconds: 10
            timeoutSeconds: 5
          args:
            - /nginx-ingress-controller
            - --default-backend-service=$(POD_NAMESPACE)/default-backend
            - --v=2
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          ports:
            - containerPort: 80
          lifecycle:
            preStop:
              exec:
                command: ["/usr/sbin/nginx", "-s", "quit"]
</code></pre>

<p>I hate answering my own questions, but after noodling a bit this is what i have so far. </p>

<p>I created a bash script that is semi-blocking, called <code>killer</code>:</p>

<pre><code>#!/bin/bash

sleep 3
PID=$(cat /run/nginx.pid)
nginx -s quit

while [ -d /proc/$PID ]; do
  sleep 0.1
done
</code></pre>

<p>I found that inside the nginx pod there is a file <code>/run/nginx.pid</code> which has the PID of the master process. If you call <code>nginx -s quit</code> and initiate a wait until the process disappears, you have essentially made the quit command "blocking".</p>

<p>Note that there is a <code>sleep 3</code> before anything happens. This is due to a race condition where Kubernetes marks a pod as terminating, but takes a little time (&lt; 1s) to remove this pod from the service that points traffic toward it. </p>

<p>I have mounted this script into my pod, and called it via the <code>preStop</code> directive. It mostly works, but during testing there are still occasional blips where i get a curl error that the connection was "reset by peer." But this is a step in the right direction.</p>
