argamanza of Docker Container
5/25/2019 - 7:19 PM

Nginx Reverse Proxy ConfigMap

Use nginx as reverse proxy in Kubernetes

apiVersion: apps/v1
kind: Deployment
metadata:
  name: proxy
spec:
  selector:
    matchLabels:
      app: proxy
  template:
    metadata:
      labels:
        app: proxy
    spec:
      containers:
      - name: proxy
        image: nginx:latest
        ports:
        - containerPort: 80
        volumeMounts:
          - mountPath: /etc/nginx # mount nginx-conf volumn to /etc/nginx
            readOnly: true
            name: nginx-conf
      volumes:
      - name: nginx-conf
        configMap:
          name: nginx-conf # place ConfigMap `nginx-conf` on /etc/nginx
          items:
            - key: nginx.conf
              path: nginx.conf
apiVersion: v1
kind: Service
metadata:
  name: proxy
spec:
  type: LoadBalancer
  selector:
    app: proxy
  ports:
  - port: 80
    targetPort: 80
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
  labels:
    app: proxy
data:
  nginx.conf: |
    events {}
    # event context needs to be defined to consider config valid
    http {
        server {
            listen 80;

            location / {
                proxy_pass http://app:8080;
                proxy_redirect off;
                proxy_set_header Host             $host;
                proxy_set_header X-Real-IP        $remote_addr;
                proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
            }
        }
    }