0

I am new to Kubernetes. By reading some blogs and documentation I have successfully created the EKS cluster. I am using ALB(layer 7 load balancing) for my Django app. For controlling the routes/paths I am using the ALB ingress controller. But I am unable to serve my static contents for Django admin. I know that I need a webserver(Nginx) to serve my static files. I'm not sure how to configure to serve static files. note: (I don't want to use whitenoise)

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: "backend-ingress"
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/subnets: subnet-1, subnet-2, subnet-3
    alb.ingress.kubernetes.io/certificate-arn: arn:aws:acm:ap-southeast-1:***:certificate/*
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}, {"HTTPS":443}]'
    alb.ingress.kubernetes.io/actions.ssl-redirect: '{"Type": "redirect", "RedirectConfig": { "Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'
  labels:
    app: stage
spec:
  rules:
    - host: *.somedomain.com
      http:
        paths:
          - path: /*
            backend:
              serviceName: backend-service
              servicePort: 8000

this is the ingress yaml i am using. But whenever i am trying to visit my Django admin it's not loading the css and js files.

Deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: server-dashboard-backend
  labels:
    app: backend
spec:
  replicas: 2
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      volumes:
        - name: staticfiles
          emptyDir: {}
      containers:
       - name: server-dashboard 
         image: *.dkr.ecr.ap-southeast-1.amazonaws.com/*:4
      volumeMounts:
            - name: staticfiles
              mountPath: /data  
         lifecycle:
            postStart:
              exec:
                command: ["/bin/sh", "-c" , "cp -r /static /data/"]

       - name: nginx
         image: nginx:stable
         ports:
            - containerPort: 80
         volumeMounts:
            - name: staticfiles
              mountPath: /data  
Raisul
  • 366
  • 1
  • 11
  • Could you provide any examples of waht you tried already? Any yaml files? –  Dec 22 '21 at 08:13
  • @p10l I have edited my question part. What can be the process to serve static files? for example, do i need to add nginx container inside the pod and address the path in ingress or what? – Raisul Dec 22 '21 at 16:02

2 Answers2

2

I have solved the problem. i removed the command ["/bin/sh", "-c", "cp -r /path/to/staticfiles /data/"]

I was mounting in the wrong path. So the new deployment file is:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: server-dashboard-backend
  labels:
    app: backend
spec:
  replicas: 2
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      volumes:
        - name: staticfiles
          emptyDir: {}

      containers:
       - name: server-dashboard 
         image: *.dkr.ecr.ap-southeast-1.amazonaws.com/*:4
         volumeMounts:
            - name: staticfiles
              mountPath: /usr/src/code/static
          

       - name: nginx
         image: nginx:stable
         ports:
            - containerPort: 80
         volumeMounts:
            - name: staticfiles
              mountPath: /usr/share/nginx/html/static/
Raisul
  • 366
  • 1
  • 11
1

I solved the problem creating a pod with the Django BE and Nginx reverse-proxy, sharing the static files volume:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      volumes:
        - name: staticfiles
          emptyDir: {}
      containers:
        - name: nginx
          image: ...
          ports:
            - containerPort: 80
          volumeMounts:
            - name: staticfiles
              mountPath: /data

        - name: django
          image: ...
          ports:
            - containerPort: 8000
          volumeMounts:
            - name: staticfiles
              mountPath: /data
          lifecycle:
            postStart:
              exec:
                command: ["/bin/sh", "-c", "cp -r /path/to/staticfiles /data/"]

Then, in the Service (and the Ingress), point the Nginx 80 port.

Marco Caberletti
  • 1,353
  • 1
  • 10
  • 13
  • PostStartHookError: command '/bin/sh -c cp -r /static /data/' exited with 1: cp: cannot stat '/static': No such file or directory... @Marco. i am getting this error. After that i tried to use initcontainer method but no luck . – Raisul Dec 27 '21 at 12:20
  • During the docker image build, do you collect static files with `python manage.py collectstatic`? – Marco Caberletti Dec 27 '21 at 16:02
  • yes, bro did that. Even I checked the pods and saw that static folder is there but it keeps telling me the same error. I can show you the full deployment file. I am editing my question. i changed my command to ["/bin/sh", "-c" , "echo Hello from the debian container > /data/index.html"] in the command section and it's working and also checked the directory it's showing the same workdir that i set in docker. but no luck! may be i am doing something wrong. @marco – Raisul Dec 28 '21 at 07:19