<p>I have two containers inside one pod. One is my application container and the second is a CloudSQL proxy container. Basically my application container is dependent on this CloudSQL container. </p>

<p>The problem is that when a pod is terminated, the CloudSQL proxy container is terminated first and only after some seconds my application container is terminated.</p>

<p>So, before my container is terminated, it keeps sending requests to the CloudSQL container, resulting in errors:</p>

<pre><code>could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432
</code></pre>

<p>That's why, I thought it would be a good idea to specify the order of termination, so that my application container is terminated first and only then the cloudsql one.</p>

<p>I was unable to find anything that could do this in the documentation. But maybe there is some way.</p>

<p>This is not directly possible with the Kubernetes pod API at present. Containers may be terminated in any order. The Cloud SQL pod may die more quickly than your application, for example if it has less cleanup to perform or fewer in-flight requests to drain.</p>
<p>From <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods" rel="nofollow noreferrer">Termination of Pods</a>:</p>
<blockquote>
<p>When a user requests deletion of a pod, the system records the intended grace period before the pod is allowed to be forcefully killed, and a TERM signal is sent to the main process in each container.</p>
</blockquote>
<hr />
<p>You can get around this to an extent by wrapping the Cloud SQL and main containers in different entrypoints, which communicate their exit status between each other using a shared pod-level file system.</p>
<p>This solution will not work with the 1.16 release of the Cloud SQL proxy (<a href="https://stackoverflow.com/questions/52148322/control-order-of-container-termination-in-a-single-pod-in-kubernetes/52156131?noredirect=1#comment110296216_52156131">see comments</a>) as this release ceased to bundle a shell with the container. The 1.17 release is <a href="https://console.cloud.google.com/gcr/images/cloudsql-docker/GLOBAL/gce-proxy?gcrImageListsize=30" rel="nofollow noreferrer">now available in Alpine or Debian Buster variants</a>, so this version is now a viable upgrade target which is once again compatible with this solution.</p>
<p>A wrapper like the following may help with this:</p>
<pre><code>containers:
- command: [&quot;/bin/bash&quot;, &quot;-c&quot;]
  args:
  - |
    trap &quot;touch /lifecycle/main-terminated&quot; EXIT
    &lt;your entry point goes here&gt;
  volumeMounts:
  - name: lifecycle
    mountPath: /lifecycle
- name: cloudsql_proxy
  image: gcr.io/cloudsql-docker/gce-proxy
  command: [&quot;/bin/bash&quot;, &quot;-c&quot;]
  args:
  - |
    /cloud_sql_proxy &lt;your flags&gt; &amp;
    PID=$!

    function stop {
        while true; do
            if [[ -f &quot;/lifecycle/main-terminated&quot; ]]; then
                kill $PID
            fi
            sleep 1
        done
    }
    trap stop EXIT
    # We explicitly call stop to ensure the sidecar will terminate
    # if the main container exits outside a request from Kubernetes
    # to kill the Pod.
    stop &amp;
    wait $PID
  volumeMounts:
  - name: lifecycle
    mountPath: /lifecycle
</code></pre>
<p>You'll also need a local scratch space to use for communicating lifecycle events:</p>
<pre><code>volumes:
- name: lifecycle
  emptyDir:
</code></pre>
<p><strong>How does this solution work?</strong> It intercepts in the Cloud SQL proxy container the <code>SIGTERM</code> signal passed by the Kubernetes supervisor to each of your pod's containers on shutdown. The &quot;main process&quot; running in that container is a shell, which has spawned a child process running the Cloud SQL proxy. Thus, the Cloud SQL proxy is not immediately terminated. Rather, the shell code blocks waiting for a signal (by simple means of a file appearing in the file system) from the main container that it has successfully exited. Only at that point is the Cloud SQL proxy process terminated and the sidecar container returns.</p>
<p>Of course, this has no effect on forced termination in the event your containers take too long to shutdown and exceed the configured grace period.</p>
<p>The solution depends on the containers you are running having a shell available to them; this is true of the Cloud SQL proxy (except 1.16, and 1.17 onwards when using the <code>alpine</code> or <code>debian</code> variants), but you may need to make changes to your local container builds to ensure this is true of your own application containers.</p>
