A Kubernetes CronJob to restart all deployments daily at 23:00 UTC, excluding kube-system and kube-custom namespaces. This project provides a Docker image and Kubernetes manifests to automate deployment restarts in a Kubernetes cluster.
The Docker image is publicly available at Docker Hub: unicolored/k8s-restart-deployments. The repository includes a shell script, Dockerfile, and Kustomize manifests to deploy the CronJob with proper RBAC permissions.
- Daily Restarts: Runs at 23:00 UTC to gracefully restart all deployments using
kubectl rollout restart. - Namespace Exclusions: Skips
kube-systemandkube-customto avoid disrupting critical components. - Kubernetes-Native: Uses a
CronJobfor scheduling and RBAC for secure access. - Kustomize Support: Applies a
cron-restart-prefix andkube-customnamespace for easy customization. - Public Docker Image: Lightweight image based on
bitnami/kubectlfor executing the restart script.
- A Kubernetes cluster (v1.19 or later).
kubectlconfigured with cluster access.- Kustomize (or
kubectlwith Kustomize support, v1.14+). - Permissions to create namespaces,
ServiceAccount,ClusterRole,ClusterRoleBinding, andCronJob.
git clone https://github.com/unicolored/k8s-restart-deployments.git
cd k8s-restart-deploymentsThe Docker image is available at docker.io/unicolored/k8s-restart-deployments. It contains:
- A shell script (
restart-deployments.sh) to list and restart deployments. - A base image (
bitnami/kubectl) forkubectlcommands.
To pull the image:
docker pull unicolored/k8s-restart-deployments:latestThe repository includes Kustomize manifests in the kustomize/ directory:
rbac.yaml: DefinesServiceAccount,ClusterRole, andClusterRoleBinding.cronjob.yaml: Defines theCronJobto run daily at 23:00 UTC.kustomization.yaml: AppliesnamePrefix: cron-restart-andnamespace: kube-custom, and patches theClusterRoleBinding.
To deploy:
kubectl apply -k kube-custom/This creates:
ServiceAccount: cron-restart-deployments-sainkube-custom.ClusterRole: cron-restart-deployments-role.ClusterRoleBinding: cron-restart-deployments-binding.CronJob: cron-restart-cronjob-deploymentsinkube-custom.
Check the resources:
kubectl get namespace kube-custom
kubectl get serviceaccount,cronjob -n kube-custom
kubectl get clusterrole cron-restart-deployments-role
kubectl get clusterrolebinding cron-restart-deployments-bindingRun a manual job to test:
kubectl create job --from=cronjob/cron-restart-cronjob-deployments manual-test-job -n kube-custom
kubectl get jobs -n kube-custom
kubectl get pods -n kube-custom
kubectl logs <pod-name> -n kube-customVerify deployments were restarted (excluding kube-system and kube-custom):
kubectl get pods --all-namespaces -o custom-columns="NAMESPACE:.metadata.namespace,NAME:.metadata.name,CREATION:.metadata.creationTimestamp"Ensure the CronJob is scheduled for 23:00 UTC:
kubectl get cronjob cron-restart-cronjob-deployments -n kube-custom -o yaml | grep scheduleExpected: schedule: "0 23 * * *"
Check for suspension:
kubectl get cronjob cron-restart-cronjob-deployments -n kube-custom -o yaml | grep suspendIf suspend: true, enable the CronJob:
kubectl patch cronjob cron-restart-cronjob-deployments -n kube-custom -p '{"spec":{"suspend":false}}'- Change Schedule: Edit
cronjob.yamlto adjust thespec.schedule(e.g.,0 20 * * *for 20:00 UTC). - Modify Excluded Namespaces: Update
restart-deployments.shto exclude additional namespaces:Rebuild and push the Docker image:if [[ "$ns" == "kube-system" || "$ns" == "kube-custom" || "$ns" == "other-namespace" ]]; then continue fi
docker build -t unicolored/k8s-restart-deployments:latest . docker push unicolored/k8s-restart-deployments:latest - Change Namespace or Prefix: Edit
kustomization.yamlto modifynamespaceornamePrefix. - Private Registry: Add
imagePullSecretstocronjob.yamlif using a private registry.
Dockerfile: Builds the Docker image withrestart-deployments.sh.restart-deployments.sh: Script to restart deployments, excludingkube-systemandkube-custom.kustomize/rbac.yaml: Defines RBAC resources.kustomize/cronjob.yaml: Defines theCronJob.kustomize/kustomization.yaml: Applies Kustomize customizations.
- Image Pull Errors: Ensure the cluster can access
docker.io/unicolored/k8s-restart-deployments:latest. - RBAC Errors: Verify the
ClusterRoleandClusterRoleBinding:kubectl describe clusterrole cron-restart-deployments-role kubectl describe clusterrolebinding cron-restart-deployments-binding
- No Restarts: Check if deployments exist in non-excluded namespaces:
kubectl get deployments --all-namespaces
- CronJob Not Running: Confirm the schedule and time zone (UTC). Check
kubectl describe cronjob.
Contributions are welcome! To contribute:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/your-feature). - Commit changes (
git commit -m "Add your feature"). - Push to the branch (
git push origin feature/your-feature). - Open a pull request.
Please include tests and documentation for new features.
This project is licensed under the MIT License. See the LICENSE file for details.
For questions or issues, open an issue on GitHub or contact the maintainer at [[email protected]] (replace with your email if desired).