The Kubernetes emulator KIND, while serving as a valuable tool for local development and testing kubernetes env. In last years everybody who works in dev and test with kubernetes install cluster with kind (kubernetes in docker). This small community project is great but has one gap. NO LOAD BALANCER
This absence of load balancer support can pose challenges for developers aiming to accurately replicate production environments locally, as they may need to resort to alternative configurations or external tools to emulate load balancing behavior effectively.
Now this feature is available
How to do it?
- Create example cluster with kind (kubernetes in docker) https://github.com/kubernetes-sigs/kind/releases/tag/v0.22.0
kind create cluster --name=test-load-balancer
- Create kubernetes deployment wth service which expose load balancer
kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: policy-local
labels:
app: MyLocalApp
spec:
replicas: 1
selector:
matchLabels:
app: MyLocalApp
template:
metadata:
labels:
app: MyLocalApp
spec:
containers:
- name: agnhost
image: registry.k8s.io/e2e-test-images/agnhost:2.40
args:
- netexec
- --http-port=8080
- --udp-port=8080
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: lb-service-local
spec:
type: LoadBalancer
externalTrafficPolicy: Local
selector:
app: MyLocalApp
ports:
- protocol: TCP
port: 80
targetPort: 8080
EOF
kubectl get svc
Now you should see that service lb-service-local is waiting for external ip (column EXTERNAL-IP has value <pending>)
- Download cloud provider kind https://github.com/kubernetes-sigs/cloud-provider-kind/releases/tag/v0.1.0
wget https://github.com/kubernetes-sigs/cloud-provider-kind/releases/download/v0.1.0/cloud-provider-kind_0.1.0_linux_amd64.tar.gz
tar -xvf cloud-provider-kind_0.1.0_linux_amd64.tar.gz
- Remove node label
# Control-plane nodes need to remove the special label node.kubernetes.io/exclude-from-external-load-balancers to be able to access the workloads running on those nodes using a LoadBalancer Service.
kubectl label node kind-control-plane node.kubernetes.io/exclude-from-external-load-balancers-
- Execute cloud-provider-kind and wait 10 seconds for magic
./cloud-provider-kind
and now you should see that external ip has value of your docker container ip. In my installation is 172.18.0.4, but in your case you can have a different ip.
- Test your local load balancer Use sslip (dns emulator) service that, when queried with a hostname with an embedded IP address, returns that IP address
# curl <YOUR-LOCAL-IP>.sslip.io
curl 172.18.0.4.sslip.io
# Output: NOW: 2024-04-24 01:26:56.780779146 +0000 UTC m=+627.893822330
From now, you can test full kubernetes project installation with load balancer. From many years this feature was abandoned, but now is available and will make life with kubernetes much easier
This small but great project has presentation in last KubeCon 2024: https://www.youtube.com/watch?v=U6_-y24rJnI