Kubernetes aws- what's the best practice to setup docroot contents for Nginx

2/3/2021

I have implemented a web app using Nginx docker container on aws eks using ingress. Currently I'm creating the docker image with the docroot content and using Nginx to serve it. If in case my do root content is huge and I want to keep it outside the image how can I do it? Please share some examples.

Here's the docker file content

FROM nginx
  
ADD . /usr/share/nginx/html/

COPY env.conf /etc/nginx/conf.d/

COPY nginx.conf /etc/nginx/

Content from env.conf

server {
      listen 80 ;
      server_name localhost;
      root   /usr/share/nginx/html;
      index  index.html;
      large_client_header_buffers 4 32k;
      #default_type text/html;

     # CSS
      location / {
        add_header Cache-Control public;
       # Equivalent to above:
        expires     15m; # Indicate that the resource can be cached for 86400 seconds (24 hours)
        etag on; # Add an ETag header with an identifier that can be stored by the client

        rewrite ^/.*$ /index.html;
      }
     location ~ \/[^\/]*\.[^\/]*$ { }
     location /ABC {
        rewrite ^/abc(/|)$ /abc/index.html break;
        rewrite ^/abc/.*$ /abc/index.html break;
      }
   }

Here's the kubernetes yaml file.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-dev4
  namespace: abc
spec:
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nginx-dev4
  replicas: 3 # tells deployment to run 1 pods matching the template
  template: # create pods using pod definition in this template
    metadata:
      labels:
        app: nginx-dev4
    spec:
      containers:
      - name: nginx-dev4
        image: XXXX.amazonaws.com/pcl-pv/dev4
        ports:
        - containerPort: 80
        volumeMounts:
          - mountPath: "/usr/share/nginx/html"
            name: "task-pv-volume"
      volumes:
        - name: task-pv-volume
          persistentVolumeClaim:
            claimName: task-pv-claim

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-dev4
  namespace: pv
  labels:
    app: nginx-dev4
spec:
  ports:
  - name: http
    port: 9091
    targetPort: 80
  type: ClusterIP
  selector:
    app: nginx-dev4

---
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    ingress.kubernetes.io/ssl-passthrough: "true"
    ingress.kubernetes.io/ssl-redirect: "true"
  name: nginx-dev4
  namespace: pv
spec:
  rules:
    - host: XXXX.aws.com
      http:
        paths:
          - path: /
            backend:
              serviceName: nginx-dev4
              servicePort: 9091

added persistence volume, persistence volume claim to achieve this.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  namespace: pv
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/home/ec2-user/nginx"


---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
  namespace: pv
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi
-- Nikhil Bharati
amazon-eks
kubernetes
nginx

1 Answer

2/3/2021

you can mount the NFS or Cloud storage with the POD as option to server large content.

here is one nice example : https://medium.com/grensesnittet/mounting-a-gcp-bucket-as-nfs-in-kubernetes-8f6d3faf4da3

you can also use EFS, NFS, or Cloud storage bucket as per requirement.

-- Harsh Manvar
Source: StackOverflow