How to make deployment restart if another pod restarts

1/16/2019

I have a web deployment and a mongoDB statefulset. The web deployment connects to the mongodb but once in a while a error may occur in the mongodb and it reboots and starts up. The connection from the web deployment to the mongodb never get restarted. Is there a way in the web deployment. If the mongodb pod restarts to restart the web pod as well?

-- Shawn Varughese
azure-aks
kubernetes

2 Answers

1/16/2019

Unfortunately, there's no easy way to do this within Kubernetes directly, as Kubernetes has no concept of dependencies between resources.

The best place to handle this is within the web server pod itself.

The ideal solution is to update the application to retry the connection on a failure.

A less ideal solution would be to have a side-car container that just polls the database and causes a failure if the database goes down, which should cause Kubernetes to restart the pod.

-- Swiss
Source: StackOverflow

1/16/2019

Yes, you can use a liveness probe on your application container that probes your Mongo Pod/StatefulSet. You can configure it in such a way that it fails if it fails to TCP connect to your Mongo Pod/StatefulSet when Mongo crashes (Maybe check every second)

Keep in mind that with this approach you will have to always start your Mongo Pod/StatefulSet first.

The sidecar function described in the other answer should work too, only it would take a bit more configuration.

-- Rico
Source: StackOverflow