For on-premises Kubernetes clusters or environments that don’t have native cloud load balancer integration, MetalLB is an excellent solution. MetalLB provides a load balancer implementation for bare metal Kubernetes clusters. By combining MetalLB with an Ingress proxy, you can effectively manage and route external traffic to your services. This blog post will guide you through the process of setting up MetalLB with an Ingress controller in your Kubernetes cluster.
Introduction to MetalLB and Ingress
MetalLB: A load balancer implementation for Kubernetes clusters that do not natively support external load balancers, such as those running on bare metal.
Ingress: A Kubernetes resource that manages external access to services within a cluster, typically HTTP/HTTPS. An Ingress controller handles the actual routing of traffic according to the rules defined in the Ingress resource.
Prerequisites
- A running Kubernetes cluster (preferably on bare metal or an environment without a cloud load balancer).
- kubectl configured to access your cluster.
- Administrative access to your cluster to install and configure MetalLB and Ingress.
Step-by-Step Setup Guide
Step 1: Deploy a Sample Application
First, let’s deploy a sample application to demonstrate the setup.
- Deploy a Sample Deployment:
sh
kubectl create deployment hello-world --image=gcr.io/google-samples/hello-app:1.0
- Expose the Deployment as a Service:
sh
kubectl expose deployment hello-world --type=NodePort --port=8080
Step 2: Install MetalLB
- Install MetalLB: Apply the MetalLB manifest to install it in your cluster.
sh
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/main/manifests/metallb.yaml
- Create a MetalLB ConfigMap: MetalLB needs a ConfigMap to allocate IP addresses to services. Define the IP address range that MetalLB can use. Replace <range-start> and <range-end> with a range of IPs that are available in your network.
yaml
apiVersion: v1
kind: ConfigMap
metadata:
namespace: metallb-system
name: config
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- <range-start>-<range-end>
- Apply the ConfigMap:
sh
kubectl apply -f metallb-config.yaml
Step 3: Create a LoadBalancer Service
Next, create a LoadBalancer service that will use MetalLB to provide an external IP.
- Define the LoadBalancer Service:
yaml
apiVersion: v1
kind: Service
metadata:
name: hello-world-lb
namespace: default
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: hello-world
- Apply the LoadBalancer Service:
sh
kubectl apply -f hello-world-lb.yaml
- Verify the LoadBalancer Service:
sh
kubectl get services
Wait until an external IP is assigned to the LoadBalancer service by MetalLB.
Step 4: Deploy an Ingress Controller
Deploy an Ingress controller to manage external access to your services.
- Deploy NGINX Ingress Controller:
sh
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
- Verify the Ingress Controller:
sh
kubectl get pods -n ingress-nginx
Ensure that the Ingress controller pods are running.
Step 5: Create an Ingress Resource
Define an Ingress resource to route traffic from the LoadBalancer to your service.
- Define the Ingress Resource:
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: hello-world-ingress
namespace: default
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: <your-domain>
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: hello-world
port:
number: 8080
- Apply the Ingress Resource:
sh
kubectl apply -f hello-world-ingress.yaml
Step 6: Update DNS Settings
Update your DNS settings to point your domain to the external IP assigned by MetalLB.
- Get the External IP:
sh
kubectl get service hello-world-lb
- Update DNS: Configure your DNS provider to point your domain (<your-domain>) to the external IP assigned by MetalLB.
Verification and Testing
- Access the Application: Open a browser and navigate to http://<your-domain>. You should see the hello-world application.
- Verify Ingress Rules: Check the defined Ingress rules to ensure traffic is being routed correctly.
Conclusion
Using MetalLB in conjunction with an Ingress proxy allows you to manage and route external traffic effectively in on-premises or bare-metal Kubernetes environments. MetalLB provides the external IP capability that cloud load balancers offer, while the Ingress controller manages the routing of traffic within the cluster based on defined rules. This setup provides a scalable and manageable way to expose your applications to the internet.
Feel free to reach out with any questions or comments, and happy Kubernetes networking!