Skaffold with non-blocking program

3/29/2020

Say I want to deploy a pod with skaffold that will not contain a continuously running/blocking program. E.g. take the getting started example and change main.go to:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello world!")
}

If I run skaffold dev with the above modified example and just wait without making any changes to the code, the pod will continuously restart, cycling through statuses Completed -> CrashLoopBackOff -> Completed, with each restart running the program in the pod again. How do I get the pod to run the program once while only rerunning/restarting the pod on changes to the code?

This is with skaffold v1.6.0-docs, ubuntu 18, microk8s 1.16/stable, having set skaffold config set default-repo localhost:32000.

-- lsimmons
kubernetes
skaffold

1 Answer

3/30/2020

First of all I would like to emphasize that there is nothing specific to Skaffold. It is rather related with the very nature of kubernetes Pod which is not meant to run to completion but rather keep running (at least with its default settings).

You can easily verify it by running a Pod from this example in a few differant variants:

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox
    command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']

Try to reduce sleep time to some shorter value and you'll observe that it is also constantly changing between Completed and CrashLoopBackOff states. It will also happen when you remove the command which keeps the container up and running.

If you run:

kubectl get pod myapp-pod -o yaml

you may notice that there is restartPolicy defined in Pod specification and if you don't set it explicitely to a different value, by default it is set to Always. Here you have the reason why your Completed Pods are constantly restarted.

Setting it to Never should give you the result you want to achieve.:

spec:
  restartPolicy: Never
  containers:
  - name: myapp-container
    image: busybox
    ...

However bear in mind that you typically won't be using bare Pods for running your workload in kubernetes. You will rather use controllers such as Deployment that manage them. As long as Deployment is used to ensure that certain set of Pods is up and running, for running something to completion you have in kubernetes another controller, named Job.

-- mario
Source: StackOverflow