0
0
Azurecloud~5 mins

AKS with Azure Load Balancer - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run applications on Azure Kubernetes Service, you need a way to let users reach your app from the internet. Azure Load Balancer helps by spreading incoming traffic evenly to your app's containers, so it stays fast and reliable.
When you want to expose a web app running in AKS to the internet with a stable IP address.
When you need to balance traffic across multiple pods to avoid overloading one container.
When you want automatic health checks to send traffic only to healthy pods.
When you want a simple, built-in way to expose your AKS service without extra setup.
When you want to use Azure's native load balancing features for your Kubernetes workloads.
Config File - service-loadbalancer.yaml
service-loadbalancer.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
  labels:
    app: my-app
spec:
  type: LoadBalancer
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

This file defines a Kubernetes Service of type LoadBalancer.

type: LoadBalancer tells AKS to create an Azure Load Balancer to route traffic.

selector matches pods with label app: my-app to send traffic to.

ports maps port 80 on the load balancer to port 8080 on the pods.

Commands
This command creates the LoadBalancer service in AKS, which triggers Azure to provision a load balancer and assign a public IP.
Terminal
kubectl apply -f service-loadbalancer.yaml
Expected OutputExpected
service/my-app-service created
This command checks the status of the service and shows the external IP assigned by Azure Load Balancer.
Terminal
kubectl get services my-app-service
Expected OutputExpected
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-app-service LoadBalancer 10.0.123.45 52.170.12.34 80:31234/TCP 1m
This command shows detailed information about the service, including the load balancer's IP and backend pods.
Terminal
kubectl describe service my-app-service
Expected OutputExpected
Name: my-app-service Namespace: default Labels: app=my-app Annotations: <none> Selector: app=my-app Type: LoadBalancer IP: 10.0.123.45 LoadBalancer Ingress: 52.170.12.34 Port: 80/TCP TargetPort: 8080/TCP Endpoints: 10.244.1.5:8080,10.244.1.6:8080 Session Affinity: None Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal EnsuringLoadBalancer 2m service-controller Ensuring load balancer Normal EnsuredLoadBalancer 1m service-controller Ensured load balancer
Key Concept

If you remember nothing else from this pattern, remember: setting the service type to LoadBalancer in AKS automatically creates an Azure Load Balancer with a public IP to route traffic to your pods.

Common Mistakes
Not labeling pods with the selector label used in the service.
The load balancer cannot find any pods to send traffic to, so your app won't receive requests.
Make sure your pods have the label 'app: my-app' to match the service selector.
Using the wrong port numbers in the service spec.
Traffic won't reach the correct port on your containers, causing connection failures.
Set 'port' to the external port (e.g., 80) and 'targetPort' to the container's listening port (e.g., 8080).
Expecting the external IP to appear immediately after creating the service.
Azure Load Balancer provisioning takes time, so the external IP may show as <pending> initially.
Wait a few minutes and re-run 'kubectl get services' to see the assigned external IP.
Summary
Create a Kubernetes Service of type LoadBalancer to expose your app in AKS.
Apply the service YAML with kubectl to trigger Azure Load Balancer creation.
Check the service status to find the external IP address for accessing your app.