Django in kubernetes with ingress-nginx is not serving static files

1/16/2020

I'm new to Kubernetes and this is my first project using it.

What I'm trying to do is basically this. I have a Django app and a react app. I want to serve them together using a single ingress-nginx using kubernetes' own ingress nginx library (kubernetes/ingress-nginx) React app is served in root and Django is served in "/api" root. I've defined a FORCE_SCRIPT_NAME in Django settings.

Also for static files, I've created a separate persistent volume claim and mounted it in Django deployment file. But when I hit the Django's admin page, static files are not served. And also media files are served in api/api/media... path which has an extra api. What is the proper way to serve static files in Kubernetes? I don't want to use an online static root like S3. Here is my Django deployment yaml.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: django
  labels:
    deployment: django
spec:
  replicas: 1
  selector:
    matchLabels:
      pod: django
  template:
    metadata:
      labels:
        pod: django
    spec:
      volumes:
        - name: django-configmap-volume
          configMap:
            name: django-configmap
        - name: static-volume
          persistentVolumeClaim:
            claimName: static-volume
        - name: media-volume
          persistentVolumeClaim:
            claimName: media-volume
      containers:
        - name: django
          image: my_image:v1.0
          ports:
            - containerPort: 8182
          envFrom:
            - configMapRef:
                name: django-configmap
          env:
            - name: DB_HOST
              value: postgres-service
            - name: DB_NAME
              value: kubernetes_django
            - name: DB_USER
              valueFrom:
                secretKeyRef:
                  name: postgres-credentials
                  key: user
            - name: DB_PASS
              valueFrom:
                secretKeyRef:
                  name: postgres-credentials
                  key: password
            - name: DB_PORT
              value: "5432"
          volumeMounts:
            - name: django-configmap-volume
              mountPath: /etc/config
            - name: static-volume
              mountPath: /app/static
            - name: media-volume
              mountPath: /app/media

Django settings.py

...
FORCE_SCRIPT_NAME = '/api/'
STATIC_ROOT = '/app/static'
MEDIA_ROOT = '/app/media'
STATIC_URL = '/api/static/'
MEDIA_URL = '/api/media/'
...

I've defined a configuration snippet in ingress for static files and media files location but I think it doesn't work. ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: hoosthere-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/proxy-connect-timeout: '30'
    nginx.ingress.kubernetes.io/proxy-send-timeout: '500'
    nginx.ingress.kubernetes.io/configuration-snippet: |
      location ~* "^/api/static?(.*)" {
        alias /app/static/;
      }
      location ~* "^/api/media?(.*)" {
        alias /app/media/;
      }
spec:
  rules:
  - http:
      paths:
        - path: /?(.*)
          backend:
            serviceName: react-service
            servicePort: 1220
        - path: /api/?(.*)
          backend:
            serviceName: django-service
            servicePort: 8000

If you can provide a good example of this, I'd be appreciated for any help.

-- Çağatay Barın
django
kubernetes
kubernetes-ingress
nginx
nginx-ingress

0 Answers