Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 624
Jeanluca Scaljeri Avatar asked Oct 17 '25 07:10

Jeanluca Scaljeri


2 Answers

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.

like image 50
David Maze Avatar answered Oct 20 '25 07:10

David Maze


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"]

like image 41
Grant David Bachman Avatar answered Oct 20 '25 06:10

Grant David Bachman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!