0

kubernetes: overriding container command caused CrashLoopBackOff.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat
  labels:
    app: tomcat
spec:
  selector:
    matchLabels:
      app: tomcat
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
        - image:  tomcat:latest
          name: tomcat
          command: ["bin/sh"]

kubectl get pods

NAME                      READY   STATUS             RESTARTS     AGE
tomcat-6c99d75bd8-6rpzx   0/1     CrashLoopBackOff   1 (8s ago)   12s

Trying to shell into the tomcat container, start the tomcat in shell for debugging.

eastwater
  • 4,624
  • 9
  • 49
  • 118
  • Can you post the output of `kubectl describe tomcat-6c99d75bd8-6rpzx`. It will help for further debugging. – Sai Chandra Gadde May 19 '23 at 06:10
  • Kubernetes doesn't run containers in an environment where they can accept interactive input, and in this environment `/bin/sh` will exit immediately. Do you need to `kubectl run` a temporary container? Or if you're just trying to debug the image/container filesystem, can you `docker run` the container locally, without Kubernetes? – David Maze May 19 '23 at 10:59
  • /bin/sh is a process like others. how can K8s control whether it is interactive mode? Trying to understand why "/bin/sh" will exit immediately. – eastwater May 19 '23 at 16:58

1 Answers1

0

There's slash missing in your command. It should be /bin/sh instead. Also, for debugging purposes, pass, sleep command so the pod doesn't exit.

command: ["/bin/sh", "-c", "sleep 3600"]
Taimoor Mirza
  • 1,093
  • 7
  • 19
  • the command "/bin/sh" without sleep will open an interactive shell and wait for command? thanks. – eastwater May 19 '23 at 09:15
  • without sleep, your pod will exit because the container running won't have a running process to wati for. A container inside a pod needs an active (running) proces to remain in `Running` phase. Once, you have a running pod, you simply `exec` into it and run tomcat server from the inside without facing any errors. – Taimoor Mirza May 19 '23 at 11:08
  • "/bin/sh" itself is not a running process? It will wait for user to type in commands, right? On linux, running "/bin/sh" on current shell will start a nested shell, type "exit" returns to current shell. – eastwater May 19 '23 at 16:51
  • Using `/bin/sh` will work in an interactive mode only. In order to run the pod and debug it from the inside separately, you will need a running which doesn't result in your container, and by extension your pod exiting. Passing `sleep` command will allow you do that. – Taimoor Mirza May 20 '23 at 05:35