简体   繁体   中英

kubernetes create a persistent volume and use in my django app

I wouldd to create a persistent volume on my kubernetes (gcp) cluster and use it in my django app as, for example, media folder. On my kubernetes side i do:

First create a volumes claim:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-zeus
  namespace: ctest
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

, then in my deployments.yaml i create a volume and associate to the pod:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: django
  namespace: ctest
  labels:
    app: django
spec:
  replicas: 3
  selector:
    matchLabels:
      app: django
  template:
    metadata:
      labels:
        app: django
    spec:
      volumes:
        - name: cc-volume
          persistentVolumeClaim:
           claimName: pvc-zeus 
      containers:
        - name: django
          image: gcr.io/direct-variety-3066123/cc-mirror
          volumeMounts:
           - mountPath: "/app/test-files"
             name: cc-volume
      ...

then in my django settings:

MEDIA_URL = '/test-files/'

Here my Dockerfile:

FROM python:3.8-slim

ENV PROJECT_ROOT /app
WORKDIR $PROJECT_ROOT

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .

RUN chmod +x run.sh

CMD python manage.py runserver --settings=settings.kube 0.0.0.0:8000

when i apply volume claim on my cluster all was done (volume claim was created) but whe apply deployment.yaml no volume was created for the pods (also if i connect in bash to my pods, no folder test-files exist).

How can i create a volume on my deployments pods and use it in my django app?

So many thanks in advance

You need to have one of two Kubernetes objects in place in order to make a PVC: aPersistentVolume (PV) or a StorageClass (SC).

As you showed, your PVC does not indicate a PV or a SC from which to create a volume.

Usually, when you don't specify a PV or a SC in a PVC, a default SC will be used and you are not supposed to indicate the.resources in the PVC but in the default SC.

Maybe, if you just want to work with default SC, you would want to check if your specific cluster has it active or whether you need to create/activate it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM