0

Whenever I create a pod the status of pod changes to 'CrashLoopBackOff' after 'Completed'. I am using microk8s I have pushed the image to microk8s registry. I am creating a pod by running this command : "kubectl create -f backend-deployment.yml"

backend.Dockerfile( this docker file is of Django):

From python:3
ENV PYTHONUNBUFFERED 1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . ./
EXPOSE 8000

backend-deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name : backend-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: backend
  template:
    metadata:
      labels:
        component: backend
    spec:
      containers:
        - name: backand
          image: localhost:32000/backend:latest
          ports:
          - containerPort: 8000

what am I doing wrong?

Usama Hameed
  • 129
  • 5
  • 14

2 Answers2

2

A Deployment by design will endeavour to keep all its replicas running.

If your pod completes, whether successfully or not, the Deployment will restart it.

If the Deployment detects the pod has been restarted repeatedly, it will add delays to its restart attempts, hence the CrashLoopBackoff

If the pod is only supposed to run once to completion, it should be constructed as a Job instead.

Further reading: https://kubernetes.io/docs/concepts/workloads/controllers/job/

Blender Fox
  • 4,442
  • 2
  • 17
  • 30
  • I was reading somewhere that I haven't added the command to run django server in dockerfile that's why the pod goes to CrashLoopBackoff. – Usama Hameed Feb 02 '22 at 12:56
  • 1
    If you haven't declared the image to run anything, that will also cause the pod to terminate early as it has nothing to do :) – Blender Fox Feb 02 '22 at 12:57
1

I find a fix for it the main issue was I didn't added the command to run the django server that's why the pods was crashing the image inside the pod didn't know what to run.

I changed my yml file to:


      containers:
        - name: backand
          image: localhost:32000/backend:latest
          command: ["python", "manage.py", "runserver"]
          ports:
          - containerPort: 8000

Usama Hameed
  • 129
  • 5
  • 14