Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recycle pods in Kubernetes

I want my pods to be gracefully recycled from my deployments after certain period of time such as every week or month. I know I can add a cron job for that if I know the Kubernetes command.

The question is what is the best approach to do this in Kubernetes. Which command will let me achieve this goal?

Thank you very much for helping me out on this.

like image 561
rayhan Avatar asked Dec 22 '18 19:12

rayhan


Video Answer


2 Answers

As the OP rayhan has found out, and as commented in kubernetes/kubernetes issue 13488, a kubectl patch of an environment variable is enough.

But... K8s 1.15 will bring kubectl rollout restart... that is when PR 77423 is accepted and merged.

kubectl rollout restart now works for daemonsets and statefulsets.

like image 69
VonC Avatar answered Sep 22 '22 21:09

VonC


You should be managing your Pods via a higher-level controller like a Deployment or a StatefulSet. If you do, and you change any detail of the embedded pod spec, the Deployment/StatefulSet/... will restart all of your pods for you. Probably the most minimal way to do this is to add an annotation to the pod spec that says when it was last deployed:

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      annotations:
        deployed-at: 20181222

There is probably a kubectl patch one-liner to do this; if you're using a deployment manager like kubernetes-helm then you can just pass in the current date as a "value" (configuration field) and have it injected for you.

If you want to think bigger, though: the various base images routinely have security updates and minor bug fixes, and if you docker pull ubuntu:18.04 once a month or so you'll get these updates. If you actively know you want to restart your pods every month anyways, and you have a good CI/CD pipeline set up, consider setting up a scheduled job in your Jenkins or whatever that rebuilds and redeploys everything, even if there are no changes in the underlying source tree. That will cause the image: to get updated, which will cause all of the pods to be destroyed and recreated, and you'll always be reasonably up-to-date on security updates.

like image 39
David Maze Avatar answered Sep 22 '22 21:09

David Maze