Kubernetes, Loadbalancing, and Nginx Ingress - AKS

9/10/2020

Stack: Azure Kubernetes Service\ NGINX Ingress Controller - https://github.com/kubernetes/ingress-nginx \ AKS Loadbalancer\ Docker containers

My goal is to create a K8s cluster that will allow me to use multiple pods, under a single IP, to create a microservice architecture. After working with tons of tutorials and documentation, I'm not having any luck with my endgoal. I got to the point of being able to access a single deployment using the Loadbalancer, but introducing the ingress has not been successful so far. The services are separated into their respective files for readability and ease of control.

Additionally, the Ingress Controller was added to my cluster as described in the installation instructions using: kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.35.0/deploy/static/provider/cloud/deploy.yaml

LoadBalancer.yml:

apiVersion: v1
kind: Service
metadata:
  name: backend
spec:
  loadBalancerIP: x.x.x.x
  selector:
    app: ingress-service
    tier: backend
  ports:
  - name: "default"
    port: 80
    targetPort: 80
  type: LoadBalancer

IngressService.yml:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - http:
      paths:
      - path: /api
        backend:
          serviceName: api-service
          servicePort: 80

api-deployment.yml

apiVersion: v1
kind: Service
metadata:
  name: api-service
spec:
  selector:
    app: api
  ports:
    - port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
   name: api-deployment
spec:
  selector:
    matchLabels:
      app: api
      tier: backend
      track: stable
  replicas: 1
  template:
    metadata:
      labels:
        app: api
        tier: backend
        track: stable
    spec:
      containers:
      - name: api
        image: image:tag
        ports:
        - containerPort: 80
        imagePullPolicy: Always
      imagePullSecrets:
      - name: SECRET

The API in the image is exposed on port 80 correctly.

After applying each of the above yml services and deployments, I attempt a web request to one of the api resources via the LoadBalancer's IP and receive only a timeout on my requests.

-- Carson
azure-aks
azure-load-balancer
kubernetes
kubernetes-ingress
nginx-ingress

1 Answer

9/10/2020

Found my answer after hunting around enough. Basically, the problem was that the Ingress Controller has a Load Balancer built into the yaml, as mentioned in comments above. However, the selector for that LoadBalancer requires marking your Ingress service as part of the class. Then that Ingress service points to each of the services attached to your pods. I also had to make a small modification to allow using a static IP in the provided load balancer.

-- Carson
Source: StackOverflow