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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With