Search code examples
kuberneteskubernetes-pod

Deploying multi-container pod in Kubernetes


Newbie here, I am trying to run two containers in a pod. I need them in the "dev" namespace. Here is my YAML:

kind: Deployment
apiVersion: apps/v1
metadata:
  name: test-deployment
  namespace: dev
spec:
  selector:
    matchLabels:
      deploy: example
  template:
    metadata:
      labels:
        deploy: example
    spec:
      containers:
        - name: nginx
          image: nginx:latest
        - name: busybox
          image: busybox:1.34

I got this error:

CrashLoopBackOff

kubectl describe pod gives me this result:

Events:
  Type     Reason     Age                   From               Message
  ----     ------     ----                  ----               -------
  Normal   Scheduled  8m14s                 default-scheduler  Successfully assigned dev/nginx-deployment-55fd86946-hgjlk to minikube
  Normal   Pulling    8m13s                 kubelet            Pulling image "nginx:latest"
  Normal   Pulled     8m8s                  kubelet            Successfully pulled image "nginx:latest" in 4.9201206s
  Normal   Created    8m8s                  kubelet            Created container nginx
  Normal   Started    8m8s                  kubelet            Started container nginx
  Normal   Pulling    8m8s                  kubelet            Pulling image "busybox:1.34"
  Normal   Pulled     8m3s                  kubelet            Successfully pulled image "busybox:1.34" in 5.2652786s
  Normal   Created    7m16s (x4 over 8m3s)  kubelet            Created container busybox
  Normal   Started    7m16s (x4 over 8m3s)  kubelet            Started container busybox
  Normal   Pulled     7m16s (x3 over 8m2s)  kubelet            Container image "busybox:1.34" already present on machine
  Warning  BackOff    3m3s (x23 over 8m1s)  kubelet            Back-off restarting failed container

So what am I doing wrong? Tried to run different images, I got the same error. Is it even possible to run two containers in a pod through kind:Deployment or I should use kind:Pod and specify them that way?


Solution

  • Your busybox container is crashing because it needs a process to run for indefinite period. As long as a process is running inside, the container won't crash (unless there's some un-handled exception). BusyBox image doesn't come with a default process.

    Just add command attribute with sleep in your busybox container.

    kind: Deployment
    apiVersion: apps/v1
    metadata:
      name: test-deployment
      namespace: dev
    spec:
      selector:
        matchLabels:
          deploy: example
      template:
        metadata:
          labels:
            deploy: example
        spec:
          containers:
            - name: nginx
              image: nginx:latest
            - name: busybox
              image: busybox:1.34
              command: ["sh", "-c", "sleep 3600"] # run sleep command for 3600 seconds / 1 hr