1
votes

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.

2
How does your nginx.conf get into the container? (Is there a ConfigMap you haven't shown? It'd have matching mount options in the Deployment spec.)David Maze
@DavidMaze thank you for the answer. No idk ConfigMap. From my knowledge I added the conf on volumes in docker-compose.yaml?Nico Schuck
Kubernetes doesn't know anything about your Docker Compose setup. Configure a Pod to Use a ConfigMap in the Kubernetes documentation is an important read, it's the standard way to push in not-especially-sensitive configuration files like this.David Maze
all right. If I get you right, I don`t need the nginx.conf inside my docker-compose. The job on kubernetes is taken over by ConfigMap?Nico Schuck
Can you post your solution as an answer so it might be helpful for others with the same problem.Crou

2 Answers

1
votes

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.

1
votes

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