How do I get django static files actually styling in Kubernetes?

2/17/2018

This is my deployment for the django app with rest framework:

#Deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata: 
  labels: 
    service: my-api-service e
  name: my-api-deployment 

spec: 
  replicas: 1
  template: 
    metadata: 
      labels: 
        name: my-api-selector
    spec: 
      containers: 
        - 
          name: nginx
          image: nginx
          command: [nginx, -g,'daemon off;']
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - 
              name: shared-disk
              mountPath: /static
              readOnly: true
            - 
              name: nginx-config
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf

          ports:
            - 
              name: nginx
              containerPort: 80
        - 
          env: 

            - 
              name: STATIC_ROOT
              value: /src/static/
            -
              name: MEDIA_ROOT
              value: /src/media/         
            -
              name: CLIENT_ORIGIN
              value: https://marketforce.platinumcredit.co.ke
            -
              name: DJANGO_SETTINGS_MODULE
              value: config.production
            -
              name: DEBUG
              value: "true"

          image: localhost:5000/workforce-api:0.2.0         
          command: [ "./entrypoint.sh" ]
          name: my-api-container
          imagePullPolicy: IfNotPresent
          ports: 
            - 
              name: my-api-port
              containerPort: 9000
              protocol: TCP

          volumeMounts:
            - 
              name: shared-disk
              mountPath: /src/static


      initContainers:
        - 
          name: assets
          image: localhost:5000/workforce-api:0.2.0
          command: [bash, -c]
          args: ["python manage.py collectstatic --noinput"]
          command: [bash, -c]
          args: ["sleep 10"]
          command: [bash, -c]
          args: ["cp -r static/* /data"]
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - 
              mountPath: /data
              name: shared-disk
      volumes:
        -
          name: shared-disk
          emptyDir: {}
        -
          name: nginx-config
          configMap:
            name: nginx-config

My service:

# Service
apiVersion: v1
kind: Service
metadata:
  name: my-api-service
  labels:
    label: my-api-service
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http

  selector:
    name: my-api-selector

And here's my nginx configuration:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    events {
      worker_connections  1024;
    }
    http {
      upstream api {
        server 127.0.0.1:9000 fail_timeout=0;
      }
      server {
        access_log /var/log/nginx/api.my.log;
        error_log /var/log/nginx/api.my.mesozi.com-http-error.log;
        listen 80;
        server_name localhost;

        location /static/ {
          autoindex on;    
          alias /static/; 
        }

        location /media/ {
        alias   /src/media/;
        }

       location = /favicon.ico {
        access_log off;
        log_not_found off;
        }


        location / {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $host;
          proxy_set_header X-NginX-Proxy true;
          proxy_pass http://127.0.0.1:9000/;
        }
      }
    }

As you can see in my deployment file I am running both nginx and my app in the same pod. Everything's running well in minikube except it's like the styles are not being applied. Looking at network in the browser all static file requests are OK 200. What am I missing?

I guess displaying styles for my API is not any useful but I just want to get it tow work cause it should.

-- ArdentLearner
django
django-rest-framework
kubernetes
nginx

2 Answers

9/24/2019

Just add the following code on your nginx.conf ... http { include /etc/nginx/mime.types; ... } ...

-- jbalmant
Source: StackOverflow

2/28/2018

Looking at your static files section:

location /static/ {
          autoindex on;    
          alias /static/; 
        }

Is /static/ an absolute path to your static web files?, if not, use the full path on alias since root has not been defined earlier.

Again why do you have autoindex flag turned on for this kind of files, it should be set to off like:

autoindex off;

Alternatively you could try using the root definition for static files:

location /static/ {
        root /var/;
        autoindex off;
      }

Then under /var have a static folder /var/static. The location part is appended to the path specified in the root.

-- Aby Sheffer
Source: StackOverflow