Kubernetes pod recreation

9/10/2018

Is it possible to tell Kubernetes to just throw away my current pod and recreate it again every 48 hours? Is there some type of a scheduler on Google Cloud Kubernetes? Or can I just configure my deployment this way? I have a Node.js application containerized with Docker running inside of a Kubernetes cluster on Google Cloud Platform. Thank you in advance!

-- Nick
docker
google-cloud-platform
kubernetes

3 Answers

9/10/2018

Did you try CronJobs ?

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: your_app_name
            image: your_image
            args:
            - your
            - args
          restartPolicy: OnFailure

It's official Kubernetes docs under cron jobs.

Also, here is a cron schedule expression editor that might be useful: crontab.guru.

-- Onur Yartaşı
Source: StackOverflow

9/10/2018

From what I understand, there are two ways to achieve this. I'm assuming your app is running as a deployment in kubernetes, so deleting pod should create a new one.

  1. Exit your node app after 48 hours i.e. exit from within your code, a dirty way, but it's simplest.
  2. Create a cronjob that runs after 48 hours and delete any pod created under your deployment.
-- Talha Irfan
Source: StackOverflow

9/10/2018

True "Kubernetes way" to resolve this issue - is to design ReadinessProbe/LivenessProbe for your app's deployment/statefulset/pod. Once your Pod fall down correct probes will handle it and your Pod will be recreated fully automatically

P.S: you are the one who knows your app better then anyone. Try out to resolve "every 48h issues" and then make the right probes. Good luck!

Link: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/

-- Konstantin Vustin
Source: StackOverflow