kubernetesvolumes

Custom Nginx Deployment Configuration in Kubernetes


Well, I there is actually two questions, 1: The Title. 2: How to access the Application deployment through the Nginx upstream.. Just Take A Look at the Following Example..

There Is An Nginx Deployment with Following Kubernetes Manifest Setup...


apiVersion: apps/v1
kind: Deployment 

metadata:
  name: service-nginx-server 
  namespace: ingress-nginx
  
spec:
  selector:
     matchLabels:
      name: internal-service
  template:
    metadata:
      labels: 
        name: internal-service
    spec:
      containers:
        - name: service-nginx-server 
          image: nginx:1.7.9 
          ports:
            - containerPort: 80 
          resources:
            limits:
              cpu: "0.4"
              memory: "1Gi"
            requests:
              cpu: "0.4"
              memory: "1Gi"

And the Following Configuration for the Nginx is...


events {
    worker_connections 4096;
}

http {

    upstream application_upstream {
        server  APPLICATION_DEPLOYMENT_HOST:APPLICATION_DEPLOYMENT_PORT;
    }

    server {

        listen 80;
        location / {

            proxy_pass http://application_upstream;
            proxy_http_version              1.1;
            proxy_set_header   Host         $host;
            proxy_set_header Upgrade        $http_upgrade;
            
            add_header Access-Control-Allow-Origin $http_origin;
            add_header Access-Control-Allow-Methods "GET,OPTIONS";
            add_header Access-Control-Allow-Credentials "true";
            add_header Access-Control-Allow-Headers "*";

            if ($request_method = "OPTIONS") {
                return 200;
            }
        }
    }
}


And Also there is an Application Manifest...


apiVersion: apps/v1 
kind: Deployment 

metadata:
  name: application-service 
  namespace: namespace 

  spec:
    selector:
      matchLabels:
        app: application-service 
    template:
      metadata: 
        labels:
          app: application-service 
      spec:
        containers:
        - name: service-application 
          image: some_image:latest
          ports:
          - containerPort: 8000
            protocol: HTTP 
          envFrom:
            - secretRef:
              name: project-secrets
        restartPolicy: onFailure 
        
          livenessProbe:
            - httpGet:
                path: /healthcheck/
                port: 8000 
                protocol: HTTP  

          resources:
            requests:
              cpu: "0.5"
              memory: "3Gi"
            limits:
              cpu: "0.6"
              memory: "3Gi"

So the First Question is How can I Replace the Default Nginx Configuration with the Custom One, using Kubernetes Volumes.

And the Second is actually related to the request destination, how can I Access The Application Deployment, what IP and Port I Need to Specify at Nginx Upstream, in order to forward the request to the application??

Thanks.


Solution

  • So the First Question is How can I Replace the Default Nginx Configuration with the Custom One, using Kubernetes Volumes.

    You can use the configmap to store the Nginx config and mount it to POD and it will replace the existing default one you can use it with the application.

    Example

    apiVersion: extensions/v1
    kind: Deployment
    metadata:
      labels:
        app: wordpress-site
      name: wordpress-site
      namespace: development
    spec:
      progressDeadlineSeconds: 600
      replicas: 1
      revisionHistoryLimit: 10
      selector:
        matchLabels:
          app: wordpress-site
          tier: frontend
      strategy:
        rollingUpdate:
          maxSurge: 25%
          maxUnavailable: 25%
        type: RollingUpdate
      template:
        metadata:
          creationTimestamp: null
          labels:
            app: wordpress-site
            tier: frontend
        spec:
          volumes:
          - configMap:
              defaultMode: 256
              name: nginxthroughpass
              optional: false
            name: nginxconf
          - name: shared-files
            emptyDir: {}
          containers:
            - name: app
              image: <REPLACE WITH DOCKER PHP-FPM IMAGE URL>
              imagePullPolicy : IfNotPresent
              volumeMounts:
                - name: shared-files
                  mountPath: /var/www/html
              envFrom:
                - configMapRef:
                    name: wordpress-configmap
            - name: nginx
              image: nginx
              imagePullPolicy : IfNotPresent
              volumeMounts:
              - name: shared-files
                mountPath: /var/www/html
              - mountPath: /etc/nginx/conf.d
                name: nginxconf
                readOnly: true
    

    Ref files : https://github.com/harsh4870/Kubernetes-wordpress-php-fpm-nginx

    The above example is with the Nginx with WordPress in the same container. Mounting the Nginx config at /etc/nginx/conf.d from configmap: nginxconf.

    Ref file : https://github.com/harsh4870/Kubernetes-wordpress-php-fpm-nginx/blob/master/nginx-configmap.yaml

    how can I Access The Application Deployment, what IP and Port I Need to Specify at Nginx Upstream, in order to forward the request to the application??

    If you mean request routing from Nginx to an application running inside the same POD you can access it over localhost and PORT like i have done.

    If your app is running at 3000 inside Nginx config it will be localhost:3000, each container running in the same POD can access each other over the localhost.

    Extra note : In a general scenario, people use the Nginx ingress in front of the applications to route the traffic.