Sobes.tech
Middle

How to access the external IP address of a service based on a load balancer (load balancer service) in the Ingress controller?

sobes.tech AI

Answer from AI

In the Ingress controller, you do not directly access the external IP of a LoadBalancer type service. The Ingress controller itself acts as the entry point and routes traffic to various services based on rules defined in the Ingress resource.

To access a LoadBalancer type service through Ingress, you need to:

  1. Create a LoadBalancer type service for your application. The cloud provider will allocate an external IP address to this service.

    // service.yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: my-app-service
    spec:
      selector:
        app: my-app
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
      type: LoadBalancer
    
  2. Create an Ingress resource that will route traffic to this service.

    // ingress.yaml
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: my-app-ingress
    spec:
      rules:
      - host: myapp.example.com # Or another domain/path where the application will be accessible
        http:
          paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: my-app-service # Name of your LoadBalancer service
                port:
                  number: 80 # Port of the service to which traffic will be redirected
    
  3. Ensure that the Ingress controller is installed and running in the cluster. The Ingress controller (e.g., Nginx Ingress Controller, Traefik) will listen on the external IP assigned to it and redirect requests to myapp.example.com to the my-app-service.

Thus, access to the application is provided through the external IP of the Ingress controller, not directly through the external IP of the LoadBalancer service (although Ingress can use this LoadBalancer to access pods).