0

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
  • 677
  • 1
  • 8
  • 22

1 Answers1

2

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
  • 9,858
  • 1
  • 26
  • 42
  • Thanks! My actual use case was to use skaffold to deploy a pod running tests. Switching skaffold to deploy a job instead of a pod works for me and the tests will only execute once, as opposed to every time the pod restarted which was my orignal problem. Is there a better/more idiomatic way to deploy a pod running tests? – lsimmons Mar 31 '20 at 00:39
  • 1
    It depends on what you are performing those tests against. If these are other `Pods` on the same cluster, I think using `Job` or a simple `Pod` with `restartPolicy` set to `Never` is the appropriate solution. Skaffold pipeline also includes testing stage but it is designed for [container-structure-tests](https://github.com/GoogleContainerTools/container-structure-test) on built images, so I suppose this is not the kind of test that you need in your use case. – mario Mar 31 '20 at 11:49