0
0
Kubernetesdevops~5 mins

Ingress vs LoadBalancer Service decision in Kubernetes - CLI Comparison

Choose your learning style9 modes available
Introduction
When you want to expose your applications running in Kubernetes to the outside world, you need a way to route external traffic. Ingress and LoadBalancer services are two common methods to do this, each solving different problems about how traffic reaches your app.
When you have multiple services and want to route traffic to them using one external IP with different URLs or paths.
When you want a simple way to expose a single service directly to the internet with its own IP address.
When you want to use advanced routing rules like SSL termination or host-based routing.
When your cloud provider supports automatic external load balancers for services.
When you want to reduce the number of public IPs used by sharing one IP across many services.
Commands
This command creates a Service of type LoadBalancer that exposes your app with a cloud provider's external IP address.
Terminal
kubectl apply -f loadbalancer-service.yaml
Expected OutputExpected
service/my-loadbalancer-service created
This command checks the status of the LoadBalancer service and shows the external IP assigned by the cloud provider.
Terminal
kubectl get svc my-loadbalancer-service
Expected OutputExpected
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-loadbalancer-service LoadBalancer 10.96.0.1 34.123.45.67 80:32456/TCP 1m
This command creates an Ingress resource that routes external traffic to different services based on rules like URL paths or hostnames.
Terminal
kubectl apply -f ingress.yaml
Expected OutputExpected
ingress.networking.k8s.io/my-ingress created
This command shows the external IP or hostname assigned to the Ingress controller that handles routing traffic to your services.
Terminal
kubectl get ingress my-ingress
Expected OutputExpected
NAME CLASS HOSTS ADDRESS PORTS AGE my-ingress <none> example.com 34.123.45.89 80 1m
Key Concept

If you remember nothing else from this pattern, remember: Use LoadBalancer service for simple direct exposure of one app, and use Ingress to route traffic to many apps with one external IP and flexible rules.

Common Mistakes
Creating multiple LoadBalancer services for many apps without considering IP limits.
Cloud providers limit the number of external IPs and it can be costly or inefficient.
Use an Ingress resource to share one external IP and route traffic to multiple services.
Applying an Ingress resource without installing an Ingress controller.
Ingress resources need a controller to actually route traffic; without it, the Ingress does nothing.
Install a supported Ingress controller like NGINX Ingress or cloud provider's controller before creating Ingress resources.
Summary
Use LoadBalancer service to expose a single app with its own external IP.
Use Ingress to route traffic to multiple services using one external IP and rules.
Check service or ingress status with kubectl get commands to find assigned external IPs.