Cronjob to restart a deployment

  





Implementation:

1kubectl apply -f cron-job.yaml 2kubectl get cronjob -n metricgaming-test 3kubectl config set-context --current --namespace=metricgaming-test 4kubectl apply -f nginx_deployment.yaml

Rollback:

1kubectl delete -f cron-job.yaml 2kubectl delete -f nginx_deployment.yaml

Test Case:

1kubectl config set-context --current --namespace=metricgaming-test 2kubectl apply -f nginx_deployment.yaml 3deployment.apps/nginx-deployment created


1apiVersion: apps/v1 2kind: Deployment 3metadata: 4 name: nginx-deployment 5 labels: 6 app: nginx 7spec: 8 replicas: 3 9 selector: 10 matchLabels: 11 app: nginx 12 template: 13 metadata: 14 labels: 15 app: nginx 16 spec: 17 containers: 18 - name: nginx 19 image: nginx:1.14.2 20 ports: 21 - containerPort: 80

 


1kubectl apply -f cron-job.yaml 2serviceaccount/restart-nginx-deployment created 3role.rbac.authorization.k8s.io/restart-nginx-deployment created 4rolebinding.rbac.authorization.k8s.io/restart-nginx-deployment created 5cronjob.batch/restart-nginx-deployment created

cronjob.yaml

cron-job.yaml
12 Oct 2022, 09:50 AM

 


1# Create file cron-job.yaml 2--- 3kind: ServiceAccount 4apiVersion: v1 5metadata: 6 name: restart-nginx-deployment 7 namespace: test 8 9--- 10apiVersion: rbac.authorization.k8s.io/v1 11kind: Role 12metadata: 13 name: restart-nginx-deployment 14 namespace: test 15rules: 16 - apiGroups: ["apps", "extensions"] 17 resources: ["deployments"] 18 resourceNames: ["nginx-deployment"] 19 verbs: ["get", "patch"] 20 21--- 22apiVersion: rbac.authorization.k8s.io/v1 23kind: RoleBinding 24metadata: 25 name: restart-nginx-deployment 26 namespace: test 27roleRef: 28 apiGroup: rbac.authorization.k8s.io 29 kind: Role 30 name: restart-nginx-deployment 31subjects: 32 - kind: ServiceAccount 33 name: restart-nginx-deployment 34 namespace: test 35--- 36apiVersion: batch/v1beta1 37kind: CronJob 38metadata: 39 name: restart-nginx-deployment 40 namespace: test 41spec: 42 concurrencyPolicy: Forbid # Do not run concurrently! 43 schedule: '*/5 * * * *' # At every 5th minute 44 jobTemplate: 45 spec: 46 backoffLimit: 2 47 activeDeadlineSeconds: 600 48 template: 49 spec: 50 serviceAccountName: restart-nginx-deployment # Run under the service account created above 51 restartPolicy: Never 52 containers: 53 - name: kubectl 54 image: raspbernetes/kubectl # Specify the kubectl image 55 command: # The kubectl command to execute 56 - 'kubectl' 57 - 'rollout' 58 - 'restart' 59 - 'deployment/nginx-deployment' 60# kubectl apply -f cron-job.yaml 61# kubectl get cronjob -n test

 

Manually triggered cronjob:

 


Cronjob ran after 5 minutes:

 




Post a Comment

Previous Post Next Post