Cronjob in Kubernetes to restart (delete) the pod in a deployment

8/18/2017

I am using Kubernetes to run a Docker service. This is a defective service that requires a restart everyday. For multiple reasons we can't programmatically solve the problem and just restarting the docker everyday will do. When I migrated to Kubernetes I noticed I can't do "docker restart [mydocker]" but as the docker is a deployment with reCreate strategy I just need to delete the pod to have Kubernetes create a new one.

Can I automate this task of deleting the Pod, or an alternative one to restart it, using a CronTask in Kubernetes?

Thanks for any directions/examples.

Edit: My current deployment yml:

apiVersion: v1
kind: Service
metadata:
  name: et-rest
  labels:
    app: et-rest
spec:
  ports:
    - port: 9080
      targetPort: 9080
      nodePort: 30181
  selector:
    app: et-rest
    tier: frontend
  type: NodePort
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: et-rest
  labels:
    app: et-rest
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: et-rest
        tier: frontend
    spec:
      containers:
      - image: et-rest-image:1.0.21
        name: et-rest
        ports:
        - containerPort: 9080
          name: et-rest
        volumeMounts:
        - name: tz-config
          mountPath: /etc/localtime
      volumes:
      - name: tz-config
        hostPath:
          path: /usr/share/zoneinfo/Europe/Madrid
-- icordoba
docker
kubernetes

1 Answer

8/18/2017

You can use a scheduled job pod:

A scheduled job pod has build in cron behavior making it possible to restart jobs, combined with the time-out behavior, it leads to your required behavior or restarting your app every X hours.

apiVersion: batch/v2alpha1
kind: ScheduledJob
metadata:
  name: app-with-timeout
spec:
  schedule: 0 * * * ?
  jobTemplate:
    spec:
      activeDeadlineSeconds: 3600*24
      template:
        spec:
          containers:
          - name: yourapp
            image: yourimage
-- Norbert van Nobelen
Source: StackOverflow