Use of Ingress with service of type LoadBalancer on Kind

11/7/2021

I had found a definition, when reviewing a project, that intrigued me: the use of Ingress with a Service of type LoadBalancer without installing the load balancer itself on Kind. As defined, it seems to work but I don't know why. I say that it works because I'm able to curl the application on browser as expected.

Code:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: server
  labels:
    app: server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: server
  template:
    metadata:
      labels:
        app: server
    spec:
      containers:
      - name: server
        image: localhost:5000/server:2.0
---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: server
  name: server
spec:
  type: LoadBalancer
  selector:
    app: server
  ports:
    - port: 8080
      targetPort: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: server
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: server
            port:
              number: 8080

I thought that hitting a Ingress and then the LoadBalancer would cause some redirect issue. My choice for that case would be a simple ClusterIP instead, this answer agrees. If MetalLB is not installed on the cluster, what the LoadBalancer do here? This may have a point for wanting something like that, but as far as I know, the load balancer by itself is a resource outside the cluster, i.e: a cloud provider load balancer.

-- Kfcaio
kind
kubernetes
load-balancing
nginx-ingress

1 Answer

11/8/2021

If MetalLB is not installed on the cluster, what the LoadBalancer do here?

MetalLB is the load-balancer implementation for bare metal Kubernetes clusters. So MetaLB comes into the picture if you are on the baremetal or so.

If you are on managed could LoadBalancer will get auto-created if you will mention into YAML config without MetalLB.

what the LoadBalancer do here?

Since it mentioned in YAML config LoadBalancer will get created for that service.

Yes, you can use the ClusterIP instead of creating the LoadBalancer service. Change type and remove from the service.

Ideal traffic flow goes like

Internet > LoadBalancer > Ingress > ingress controller (Backend) > service > deployment > Pods > container

Yes, LB is resource of CloudProvider if you are using any managed.

-- Harsh Manvar
Source: StackOverflow