<p>I have a Kubernetes <code>JOB</code> that does database migrations on a CloudSQL database.<br>
One way to access the CloudSQL database from GKE is to use the CloudSQL-proxy container and then connect via <code>localhost</code>. Great - that's working so far. But because I'm doing this inside a K8s <code>JOB</code> the job is not marked as successfully finished because the proxy keeps on running.</p>

<pre><code>$ kubectrl get po
NAME                      READY     STATUS      RESTARTS   AGE
db-migrations-c1a547      1/2       Completed   0          1m
</code></pre>

<p>Even though the output says 'completed' one of the initially two containers is still running - the proxy.</p>

<p>How can I make the proxy exit on completing the migrations inside container 1?</p>

<p>The best way I have found is to <a href="https://kubernetes.io/docs/tasks/configure-pod-container/share-process-namespace/" rel="noreferrer">share the process namespace</a> between containers and use the SYS_PTRACE securityContext capability to allow you to kill the sidecar.</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: my-db-job
spec:
  template:
    spec:
      restartPolicy: OnFailure
      shareProcessNamespace: true
      containers:
      - name: my-db-job-migrations
        command: [&quot;/bin/sh&quot;, &quot;-c&quot;]
        args:
          - |
            &lt;your migration commands&gt;;
            sql_proxy_pid=$(pgrep cloud_sql_proxy) &amp;&amp; kill -INT $sql_proxy_pid;
        securityContext:
          capabilities:
            add:
              - SYS_PTRACE
      - name: cloudsql-proxy
        image: gcr.io/cloudsql-docker/gce-proxy:1.17
        command:
          - &quot;/cloud_sql_proxy&quot;
        args:
          - &quot;-instances=$(DB_CONNECTION_NAME)=tcp:5432&quot;
          
</code></pre>
