Easy Way to Schedule Pod Restart in Kubernetes

Maciej
2 min readMay 9, 2022
Photo by Ian Taylor on Unsplash

Introduction

In this post I will show how to easily restart pods in Kubernetes using CronJobs. We will use CronJob, not to run our pods, but to schedule a Kubernetes API command that will restart our deployment once day with kubectl rollout restart , if something goes wrong, the old pods will not be down or removed. Rollout will be create new ReplicaSets, and will wait for them to be up, before killing off old pods, and rerouting the traffic. Service will continue uninterrupted. To fully do this we will have to setup RBAC, so that the Kubernetes client running from inside the cluster has permissions to do needed calls to the Kubernetes API.

Let’s Start

Create Service Account

kind: ServiceAccount
apiVersion: v1
metadata:
name: restart-deploy
namespace: testns

Create Role

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: restart-deployment
namespace: testns
rules:
- apiGroups: ["apps", "extensions"]
resources: ["deployments"]
resourceNames: ["test-pod"]
verbs: ["get", "patch", "list", "watch"]

Create RoleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name…

--

--

Maciej
Maciej

Written by Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.