Serve asset files in nginx using Kubernetes

10/29/2019

I'm trying to deploy a pod to kubernetes using my node app and an nginx proxy server which should also serve my asset files.

I'm using two containers inside one pod for that. Below code runs the application correctly but asset files are not being served by nginx.

Below is my front-end-deployment.yaml files which takes care of creating the deployment for me. I'm wondering why nginx by this configurations doesn't not serve the static files?

apiVersion: v1
kind: ConfigMap
metadata:
  name: mc3-nginx-conf
data:
  nginx.conf: |
    user nginx;
    worker_processes  3;
    error_log  /var/log/nginx/error.log;
    events {
      worker_connections  10240;
    }
    http {
      log_format  main
              'remote_addr:$remote_addr\t'
              'time_local:$time_local\t'
              'method:$request_method\t'
              'uri:$request_uri\t'
              'host:$host\t'
              'status:$status\t'
              'bytes_sent:$body_bytes_sent\t'
              'referer:$http_referer\t'
              'useragent:$http_user_agent\t'
              'forwardedfor:$http_x_forwarded_for\t'
              'request_time:$request_time';
      access_log    /var/log/nginx/access.log main;
      upstream webapp {
        server 127.0.0.1:3000;
      }
      server {
          listen       80;
          root /var/www/html;
          location / {
              proxy_pass         http://webapp;
              proxy_redirect     off;
          }
      }
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      component: web
  template:
    metadata:
      labels:
        component: web
    spec:
      volumes:
        - name: nginx-proxy-config
          configMap:
            name: mc3-nginx-conf
        - name: shared-data
          emptyDir: {}

      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80
          volumeMounts:
            - name: nginx-proxy-config
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
            - name: shared-data
              mountPath: /var/www/html

        - name: frontend
          image: sepehraliakbari/rtlnl-frontend:latest
          volumeMounts:
            - name: shared-data
              mountPath: /var/www/html
          lifecycle:
            postStart:
              exec:
                command: ['/bin/sh', '-c', 'cp -r /app/build/client/. /var/www/html']
          ports:
            - containerPort: 3000
-- Sepehr
javascript
kubernetes
nginx
node.js

0 Answers