Python Application Image run in Docker but It does not run in Kubernets

2/6/2022

I have been trying to run a Python Django application on Kubernets but not success. The application runs fine in Docker.

This is the yaml Deployment to Kubernets:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2022-02-06T14:48:45Z"
  generation: 1
  labels:
    app: keyvault
  name: keyvault
  namespace: default
  resourceVersion: "520"
  uid: ccf0e490-517f-4102-b282-2dcd71008948
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: keyvault
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: keyvault
    spec:
      containers:
      - image: david900412/keyvault_web:latest
        imagePullPolicy: Always
        name: keyvault-web-5wrph
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  conditions:
  - lastTransitionTime: "2022-02-06T14:48:45Z"
    lastUpdateTime: "2022-02-06T14:48:45Z"
    message: Deployment does not have minimum availability.
    reason: MinimumReplicasUnavailable
    status: "False"
    type: Available
  - lastTransitionTime: "2022-02-06T14:48:45Z"
    lastUpdateTime: "2022-02-06T14:48:46Z"
    message: ReplicaSet "keyvault-6944b7b468" is progressing.
    reason: ReplicaSetUpdated
    status: "True"
    type: Progressing
  observedGeneration: 1
  replicas: 1
  unavailableReplicas: 1
  updatedReplicas: 1

This is the docker compose file I'm using to run the image in Docker:

version: "3.9"
   
services:
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"

This is the docker file I'm using to run the image in Docker:

FROM python:3.9
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

Kubectl describe pod Output:

Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  51s                default-scheduler  Successfully assigned default/keyvault-6944b7b468-frss4 to minikube
  Normal   Pulled     37s                kubelet            Successfully pulled image "david900412/keyvault_web:latest" in 12.5095594s
  Normal   Pulled     33s                kubelet            Successfully pulled image "david900412/keyvault_web:latest" in 434.2995ms
  Normal   Pulling    17s (x3 over 49s)  kubelet            Pulling image "david900412/keyvault_web:latest"
  Normal   Created    16s (x3 over 35s)  kubelet            Created container keyvault-web-5wrph
  Normal   Started    16s (x3 over 35s)  kubelet            Started container keyvault-web-5wrph
  Normal   Pulled     16s                kubelet            Successfully pulled image "david900412/keyvault_web:latest" in 395.5345ms
  Warning  BackOff    5s (x4 over 33s)   kubelet            Back-off restarting failed container

Kubectl log pod Does not show anything :(

Thanks for your help.

-- David
docker
kubectl
kubernetes
python

1 Answer

2/11/2022

This is a community wiki answer posted for better visibility. Feel free to expand it.

Based on the comments, the solution should be as shown below.

  1. Remove volumes definition from the Compose file:
version: "3.9"

services:
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "8000:8000"
  1. Specify the startup command with CMD for an image in Dockerfile:
FROM python:3.9
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
CMD ["python3","manage.py","runserver"] 

Then translate a Docker Compose file to Kubernetes resources. This can be done with using Kompose or another suitable solution.

-- Andrew Skorkin
Source: StackOverflow