1

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
  • 1,834
  • 2
  • 33
  • 60
  • What language is the service written in? It could be easier/better to make the service "suicide" every now and then instead of relying on an external component to kill it. – whites11 Aug 18 '17 at 19:02
  • Thanks but as commented, I can't programmatically restart or solve the problem from the container itself. – icordoba Aug 19 '17 at 09:40

1 Answers1

6

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
  • 6,026
  • 3
  • 17
  • 40
  • Thanks. Can I add this to my yml file? As I read in Kubernetes docs activeDeadlineSeconds will kill pods, but no more pods will be created. Can you please confirm? Thanks. https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/ – icordoba Aug 19 '17 at 09:42
  • Ah, sorry: My bad: I should have posted a scheduledJob for the automated restart: Updating post – Norbert Aug 19 '17 at 15:59
  • After minor changes I managed to "apply" that CronJob (not ScheduledJob starting v1.5). It is scheduled but won't start the service when executed (probably it is waiting for midnight as of 0 * * * ?). Any idea how I can make it start first time I apply the yml? Thanks. – icordoba Aug 19 '17 at 18:45
  • 1
    You would have to set the schedule to about your current time (so my time now is 13:00: 1 13 * * * would do that (start a minute after 13:00). So your restart will be pretty much at the time you schedule it from that point on. – Norbert Aug 19 '17 at 20:01
  • 1
    ScheduledJob has been renamed to CronJob in more recent versions – Nicolas Martinez May 26 '22 at 22:23