Using Django with db.sqlite3 with persistent volume in a Kubernetes pod outputs - django.db.utils.OperationalError: unable to open database file
NickName:Erokos Ask DateTime:2021-10-25T14:58:29

Using Django with db.sqlite3 with persistent volume in a Kubernetes pod outputs - django.db.utils.OperationalError: unable to open database file

I'm trying to deploy a small Django app, that creates its own db.sqlite3 database, in a Kubernetes pod. When doing so without a persistent volume to save the db.sqlite3, it works fine but when trying to save it in a persistent volume, the pod outputs django.db.utils.OperationalError: unable to open database file

The first problem I had is when these commands:

python ./manage.py migrate
sh -c "envdir ${ENVDIR} python manage.py collectstatic"

were run in the Dockerfile. After mounting the volume, none of my files would be visible. I learned that K8s volumes behaved differently from docker volumes and my solution was to put the commands in a shell script and execute it in the CMD or ENTYPOINT. This created the files after mounting and were visible but still doesn't help with the current problem.

I tried using a persistent volume, tried using a hostPath defined volume and even tried using an initContainer that has the same image as the app, only it first sets the permissions to 777 for db.sqlite3 and executes the two commands above, but it can't even start for some reason.

Here's the deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  labels:
    app: myapp
  namespace: app-prod
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      securityContext:
        runAsUser: 0
        runAsGroup: 0
        fsGroup: 0
        fsGroupChangePolicy: "OnRootMismatch"
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                - key: role
                  operator: In
                  values:
                  - on-demand-worker
      terminationGracePeriodSeconds: 30
      containers:
      - name: notation
        image: myimage
        imagePullPolicy: Always
        command: ["bash", "-c", "python ./manage.py runserver 0.0.0.0:8000"]
        ports:
        - containerPort: 8000
          name: http
        volumeMounts:
          - name: myapp-data
            mountPath: /app/db.sqlite3
            subPath: db.sqlite3
        securityContext:
            privileged: true
        #securityContext:
        #allowPrivilegeEscalation: false
      initContainers:
      - name: sqlite-data-permission-fix
        image: myimage
        command: ["bash","-c","chmod -R 777 /app && python ./manage.py migrate && envdir ./notation/.devenv python manage.py collectstatic"]
        volumeMounts:
          - name: myapp-data
            mountPath: /app/db.sqlite3
            subPath: db.sqlite3
        resources: {}

      volumes:
        - name: myapp-data
          persistentVolumeClaim:
            claimName: notation

The permissions I see on the host look good, 777 as I wanted them to be so I really don't know what the problem is... Any help would be appreciated.

Copyright Notice:Content Author:「Erokos」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69703942/using-django-with-db-sqlite3-with-persistent-volume-in-a-kubernetes-pod-outputs

More about “Using Django with db.sqlite3 with persistent volume in a Kubernetes pod outputs - django.db.utils.OperationalError: unable to open database file” related questions

Using Django with db.sqlite3 with persistent volume in a Kubernetes pod outputs - django.db.utils.OperationalError: unable to open database file

I'm trying to deploy a small Django app, that creates its own db.sqlite3 database, in a Kubernetes pod. When doing so without a persistent volume to save the db.sqlite3, it works fine but when tryi...

Show Detail

django.db.utils.OperationalError: unable to open database file

When I run python manage.py runserver I get this error File "/usr/local/lib/python2.7/dist-packages/Django-1.10.1-py2.7.egg/django/db/backends/sqlite3/base.py", line 209, in get_new_connection

Show Detail

django.db.utils.OperationalError: unable to open database file error

I am new to Django. I've installed in virtual env in ubuntu. Whenever i am trying to run python3 manage.py runserver i am getting the following error File "/home/aashish/Python/Django/project_e...

Show Detail

How respawn a pod with persistent volume which stuck on failed node in Kubernetes

I have a simple k8s installation with few nodes and ceph (kubernetes.io/rbd) as storageclass. I have a deployment with a single pod which uses a persistent volume from the persistent volume claim (

Show Detail

Access Kubernetes Persistent Volume data

Is there any way to access Google cloud Kubernetes persistent volume data without using pod. I cannot start pod due to data corruption in persistent volume. Have any command line tool or any other ...

Show Detail

Is there a way to create a persistent volume per pod in a kubernetes deployment (or statefulset)?

I'm currently creating a kubernetes deployment, in this deployment I have replicas value set at X and I want to create X volume that are not empty when the corresponding pod is restarted. I'm not u...

Show Detail

Difficulty using Kubernetes Persistent Volume Claims with Amazon EBS

I'm attempting to follow the instructions at https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/registry to add a private docker registry to Kubernetes, but the pod created by the ...

Show Detail

Container Data in Persistent Volume Claim when POD crashes

I want to create a replication controller with a POD which will have a PVC (persistent volume claim). My PVC will use an NFS storage for PV(Persistent Volume). Once the POD is operational the RC ...

Show Detail

Docker volume vs Kubernetes persistent volume

The closest answer I found is this. But I want to know is that, will the Dockerfile VOLUME command be totally ignored by Kubernetes? Or data will be persisted into two places? One for docker volum...

Show Detail

Kubernetes persistent volume overriding existing data in the pod/container

We are trying to use kubernetes persistent volume mapped with pod/container directory to have as backup. Container directory (/home) already have data from dockerimage but when we mount kubernetes

Show Detail