Delete kubernetes pod at run time

4/12/2018

We are using kubernetes for deployment of our microservices , In production mode we will not be able to access kubernetes directly , So is there any way to perform operation like delete a pod using rest api calls or perform any operation through http requests.

We need to delete a pod or its replica set to restart the pod forcefully.

Like we access a remote kubernetes and delete a particular pod at runtime

-- Vartika
kubernetes

2 Answers

4/12/2018

Yes by setting up RBAC.

You need following:

  1. A Role in Namespace your pods are running with "delete" permission on resources you want to delete.
  2. A Service Account which will identify your Pod "service"
  3. RoleBinding which grants your Service Account (2) the Role (1) to delete Pods in the Namespace

Then with given Serivce Account token you can either use kubectl or actual REST call to k8s apiserver to delete.

-- bits
Source: StackOverflow

4/12/2018

If you will not receive the right to access the kubectl command or API for the production, you should implement a liveness probe on the container.

https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/

This you can do entirely in software in your microservices. The liveness check should fail, if your application is no longer "good". You can combine the liveness check with a system condition that you can create manually (a field "restart_request" in the database is set to 1, a file exists or sth like that)

This way, you can restart your containers manually and k8s will restart them for you if they cease to operate properly.

-- thst
Source: StackOverflow