How to run a utils POD on K8S which doesn't run a process

9/15/2019

I have this docker image which has many useful tools installed. I use it inside docker to debug stuff, like testing connections to other containers. Now I would like to use this image in Kubernetes. However, because it doesn't run a process the pod will not start

Dockerfile:

FROM ubuntu:latest
RUN .... useful tools ...

And the kubernetes file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: uitls
spec:
  replicas: 1
  selector:
    matchLabels:
      bar: utils-xxl
  template:
    metadata:
      labels:
        bar: utils-xxl
    spec:
      containers:
      - name: utils
        image: jeanluca/base

When I try to apply this the pod it ends up in the CrashLoopBackOff state. Is there a way in kubernetes to start this pod? Maybe with exec and bash? Any suggestions?

-- Jeanluca Scaljeri
docker
kubernetes

3 Answers

9/15/2019

You can kubectl run a one-off pod for interactive debugging.

kubectl run jeanluca-debug \
  --generator=run-pod/v1 \
  --rm -it \
  --image=jeanluca/base

This is basically equivalent to a docker run command with the same options, except that the pod name is a required positional parameter.

This technique is useful for the sorts of debugging tasks you describe. If your image has tools like redis-cli, a mysql or psql client, DNS lookup tools like host or dig, and so on, it can be useful to figure out why exactly your combined system is broken. The image's default command can be CMD ["/bin/bash"] and that's fine, but it will exit immediately if it doesn't have an input stream attached, so you need to run it via kubectl run like this instead of trying to get a Deployment to keep it alive.

In general there's no point to keeping a container or pod running that's doing literally nothing. There are certainly tricks to keep a Docker container from exiting but I'd suggest avoiding them.

-- David Maze
Source: StackOverflow

9/15/2019

If you want an always up pod that you can use for exec-ing into and running commands, you can just tell the container to open a shell and sleep.

Something like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: uitls
spec:
  replicas: 1
  selector:
    matchLabels:
      bar: utils-xxl
  template:
    metadata:
      labels:
        bar: utils-xxl
    spec:
      containers:
      - name: utils
        image: jeanluca/base
        command: ["/bin/sh"]
        args: ["-c", "while true; sleep 999; done"]
-- Grant David Bachman
Source: StackOverflow

9/15/2019

You can try something like this:

  • Add CMD tail -f /dev/null at the end of your dockerfile. In this case the same deployment yaml should work.

OR

  • Modify your k8s deployment.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: uitls
spec:
  replicas: 1
  selector:
    matchLabels:
      bar: utils-xxl
  template:
    metadata:
      labels:
        bar: utils-xxl
    spec:
      containers:
      - name: utils
        image: jeanluca/base
        command: ["tail"]
        args: ["-f", "/dev/null"]

OR

  • Run a container in k8s like docker run using kubectl run refer this.

Hope this helps.

-- mchawre
Source: StackOverflow