Cloudsql-proxy: Terminating Sidecar after Kubernetes Job

Created on 15 Mar 2019  路  6Comments  路  Source: GoogleCloudPlatform/cloudsql-proxy

I have a cron job running in kubernetes that will run a mysql query, so therefore I've deployed the cron with a cloudsql sidecar container.

This works fine, however I want to be able to gracefully shut the down proxy container when the job completes, otherwise the job itself does not clean itself up as desired.

I have shareProcessNamespace: set to true so I have tried doing a pkill -15 cloud_sql_proxy from the master container at the end of the cron job script but this causes the sidecar container to exit with error which doesn't allow the job to succeed and finish gracefully.

Is there any hacky way that I can force the proxy sidecar to exit when the job is finished, in such a way that the job succeeds in cleaning itself up?

question

All 6 comments

You might be interested in this article on the subject: https://medium.com/@cotton_ori/how-to-terminate-a-side-car-container-in-kubernetes-job-2468f435ca99

The gist of it is the main container creates a file at /tmp/pod/terminated during termination, which the sidecar watches for, and exits once it appears.

The article referenced above does not exist any more.Do we have a solution for this?

@dshetty312 The article describe a situation where the sidecar ran a script that did the following:

  1. Start the cloud sql proxy in the background cloud_sql_proxy <your_args> &
  2. Wait for a file to appear (e.g. while loop until a file at /tmp/pod/teminated exists)
  3. Exit the proxy

The from the main container, do the following:

  1. Run your process
  2. Create a file at /tmp/pod/terminated

This means the main container tells the sidecar when it's finished, and the side car exits the proxy and terminates.

Thanks.

The newer versions of the container don't include /bin/sh, so this approach is no longer possible.

I haven't tried it, but https://github.com/karlkfi/kubexit seems like a promising possibility for now.

@nicknovitski there are alpine and buster based distributions that both include shells if you still need this functionality

Was this page helpful?
0 / 5 - 0 ratings