Docker nginx reverse proxy on Kubernetes

7/5/2019

I have a couple of applications, which runs in Docker containers (all on the same VM). In front of them, I have an nginx container as a reverse proxy. Now I want to migrate that to Kubernetes.

When I start them by docker-composer locally it works like expected. On Kubernetes not.

nginx.conf

http {
        server {
                location / {
                        proxy_pass http://app0:80;
                }

                location /app1/ {
                        proxy_pass http://app1:80;
                        rewrite ^/app1(.*)$ $1 break;
                }

                location /app2/ {
                        proxy_pass http://app2:80;
                        rewrite ^/app2(.*)$ $1 break;
                }
        }
}

edit: nginx.conf is not used on kubernetes. I have to use ingress-controller for that:

deployment.yaml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: app0
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: app0
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: app0
        image: appscontainerregistry1.azurecr.io/app0:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          name: nginx
---
#the other apps
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-nginx
  annotations:
    # use the shared ingress-nginx
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: apps-url.com
    http:
      paths:
      - path: /
        backend:
          serviceName: app0
          servicePort: 80
      - path: /app1
        backend:
          serviceName: app1
          servicePort: 80
      - path: /app2
        backend:
          serviceName: app2
          servicePort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: loadbalancer
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: ingress-nginx

I get the response on / (app0). Unfortunately, the subroutes are not working. What I´m doing wrong?

EDIT

I figured out. Ich missed installing the ingress controller. Like on this page (https://kubernetes.io/docs/concepts/services-networking/ingress/) described, the ingress doesn't work if no controller is installed. I used ingress-nginx as a controller (https://kubernetes.github.io/ingress-nginx/deploy/) because it was the best-described install guide which I was able to find and I didn´t want to use HELM. I have one more question. How I can change my ingress that subdomains are working. For example, k8url.com/app1/subroute shows me every time the start page of my app1. And if I use a domain name proxying, it rewrites every time the domain name by the IP.

-- Nico Schuck
docker
docker-compose
kubernetes
nginx

2 Answers

7/5/2019

you have created deployment successfully but with that service should be there. nginx ngress on kubernetes manage traffic based on the service.

so flow goes like

nginx-ingress > service > deployment pod.

you are missing to create the service for both applications and add the proper route based on that in kubernetes ingress.

-- Harsh Manvar
Source: StackOverflow

7/8/2019

Add this :

apiVersion: v1
kind: Service
metadata:
  name: loadbalancer
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: ingress-nginx

Because you didnot route for Service Load balancer to targetPort to 80

-- Thanh Nguyen Van
Source: StackOverflow