Skip to content

Instantly share code, notes, and snippets.

@alazycoder101
Created July 9, 2023 09:43
Show Gist options
  • Save alazycoder101/9e48d667ad816105bb5c611dceb13ff1 to your computer and use it in GitHub Desktop.
Save alazycoder101/9e48d667ad816105bb5c611dceb13ff1 to your computer and use it in GitHub Desktop.
# https://stackoverflow.com/questions/52422300/how-to-schedule-pods-restart
---
# Service account the client will use to reset the deployment,
# by default the pods running inside the cluster can do no such things.
kind: ServiceAccount
apiVersion: v1
metadata:
name: deployment-restart
namespace: <namespace>
---
# allow getting status and patching only the one deployment you want
# to restart
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: deployment-restart
namespace: <namespace>
rules:
- apiGroups: ["apps", "extensions"]
resources: ["deployments"]
resourceNames: ["<deployment-name>"]
verbs: ["get", "patch", "list", "watch"] # "list" and "watch" are only needed
# if you want to use `rollout status`
---
# bind the role to the service account
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: deployment-restart
namespace: <namespace>
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: deployment-restart
subjects:
- kind: ServiceAccount
name: deployment-restart
namespace: <namespace>
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: deployment-restart
namespace: <namespace>
spec:
concurrencyPolicy: Forbid
schedule: '0 22 */2 * *' # cron spec of time, here, 22 o'clock
jobTemplate:
spec:
backoffLimit: 2 # this has very low chance of failing, as all this does
# is prompt kubernetes to schedule new replica set for
# the deployment
activeDeadlineSeconds: 600 # timeout, makes most sense with
# "waiting for rollout" variant specified below
template:
spec:
serviceAccountName: deployment-restart # name of the service
# account configured above
restartPolicy: Never
containers:
- name: kubectl
image: bitnami/kubectl # probably any kubectl image will do,
# optionaly specify version, but this
# should not be necessary, as long the
# version of kubectl is new enough to
# have `rollout restart`
command:
- 'kubectl'
- 'rollout'
- 'restart'
- 'deployment/<deployment-name>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment