Kubernetes, LoadBalancer and Ingress for more than one service on one domain

7/16/2021

I may misunderstand this concept:

I have two services (Frontend and API). Both should to use one subdomain (my.domain.com):

  • domain.com => for FRONTEND ('hello-world')
  • domain.com/api => for API

I have a K8s-cloud running at Hetzner. Hetzner also provides loadbalancers. The cluster was created with Rancher-UI

This is how I configure the loadbalancer Service:

---
apiVersion: "v1"
kind: Service
metadata:
  name: "nginx-hello-world"
  labels:
    app: "hello-world"
  annotations:
    load-balancer.hetzner.cloud/name: lb-development
    load-balancer.hetzner.cloud/hostname: my.domain.com
    load-balancer.hetzner.cloud/protocol: http
spec:
  type: LoadBalancer
  selector:
    app: "hello-world"
  ports:
    - name: "http"
      port: 80
      targetPort: 80

And this is my Deployment for the frontend hello-world:

apiVersion: "apps/v1"
kind: "Deployment"
metadata:
  name: "nginx-hello-world"
  labels:
    app: "hello-world"
spec:
  selector:
    matchLabels:
      app: "hello-world"
  strategy:
    type: "Recreate"
  template:
    metadata:
      labels:
        app: "hello-world"
    spec:
      containers:
        - image: "rancher/hello-world"
          name: "nginx-hello-world"
          imagePullPolicy: "Always"
          ports:
            - containerPort: 80
              name: "http"

The two manifests above are WORKING! I can reach out to 'hello-world' by visiting my.domain.com/

Now I wanna hook my backend API into that setup. I thought I can do this by using an ingress manifest:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  labels:
    app: my-ingress
  name: my-ingress
spec:
  rules:
  - host: my.domain.com
    http:
      paths:
      - backend:
          serviceName: hello-api
          servicePort: 80
        path: "/app"
        pathType: Prefix
      - backend:
          serviceName: hello-world
          servicePort: 80
        path: "/"
        pathType: Prefix

How can I achieve to connect another workload with my.domain.com/api for the API and keep the use of the Hetzner loadbalancer?

I thought I can do that, by simply changing the selector in the Loadbalancer Service to point to my-ingress, but nothing I tried, worked so far.

┌─────────────────┐  ┌──────────────────┐   ┌──────────────────┐
│                 │  │  Ingress         │   │                  │
Loadbalancer Svc├─►│my.domain.com/    ├──►│ Frontend deploym.│
│                 │  │my.domain.com/api ├──►│ backend API depl└─────────────────┘  └──────────────────┘   └──────────────────┘

PS: it may be interesting: When I deployed Rancher, it has also created a nginx-ingress-controller

Thank you in advance

-- Jan
kubernetes
kubernetes-ingress
nginx-ingress
rancher

1 Answer

7/16/2021

You need to create a service of type: LoadBalancer and use the the ingress controller app's label in the selector as explained in the link below. Although, the link talks about using Traefik, the process should be same for the Nginx ingress controller.

https://community.hetzner.com/tutorials/howto-k8s-traefik-certmanager

-- Rakesh Gupta
Source: StackOverflow