Search code examples
azurekuberneteskubernetes-ingressazure-load-balancer

Azure AKS Loadbalancer serving multiple pods using service on same IP


I currently have a load balancer per pod. In my instance 2 pods with the following YAML definitions.

apiVersion: v1
kind: Service
metadata:
  name: service1
spec:
  ports:
  - name: https-service1
    port: 6379
    targetPort: 6379
  selector:
       app: service1-consoleapp
  type: LoadBalancer

apiVersion: v1
kind: Service
metadata:
  name: service2
spec:
  ports:
  - name: https-service2
    port: 443
    targetPort: 443
  selector:
       app: service2-consoleapp
  type: LoadBalancer

When I apply the above 2 yaml files I will get 2 external ip's that I then use to configure my A records in my dns subdomains.

service1.company.com => external ip 1 for service1-consoleapp

service2.company.com => external ip 2 for service2-consoleapp

Is there a way to combine the YAML file into one, so that I can only use one IP address instead of 2 ?

Also , it looks like in ingress you can do it but not sure how I deal with the "host" requirement.

Can someone please explain how the routing will work as I'm not sure what values should be in the path property ?

Will I still get 2 external ip's on this that I can use to populate the dns subdomains ?

 spec:
  rules:
  - host: service1.company.com
    http:
      paths:
      - backend:
          serviceName: service1
          servicePort: 6379
        path: ??
  - host: service2.company.com
    http:
      paths:
      - backend:
          serviceName: service2
          servicePort: 433
        path: ??

The result I'm looking for is if I type

service1.company.com:6379 in my browser then I should hit the pod endpoint (service1-consoleapp) and if I type

service2.company.com:443 in my browser then I should hit the pod endpoint (service2-consoleapp).

where the service1.company.com and service2.company.com is on the same IP address.

thanks in advance.


Solution

  • The ingress resource that you have currently should work. Remove the path section completely. Also in your DNS you need to create subdomains service1.company.com, service2.company.com and a A record to point to IP of the loadbalancer.

    This loadBalancer is the one which will route traffic form outside to ingress controller pods and ingress controller will forward the traffic to the backend pods according to rules defined in the ingress resource. The host rule works this way - if a HTTP request has a Host header service1.company.com ingress controller will send that request to service1 and if it has a Host header service2.company.com ingress controller will send that request to service2

    When you deploy a ingress controller such as Nginx you need to create a LoadBalancer type service.So you will have only one loadBalancer which is for exposing ingress controller pods.