Skip to content

Instantly share code, notes, and snippets.

@mattsn0w
Last active February 23, 2023 05:26
Show Gist options
  • Select an option

  • Save mattsn0w/fd23fc0112251c4c151a6dcb5ef479a3 to your computer and use it in GitHub Desktop.

Select an option

Save mattsn0w/fd23fc0112251c4c151a6dcb5ef479a3 to your computer and use it in GitHub Desktop.
deploy k3s using k3d with host networking and metalLB for local load balancing

Create your k3d/k3s cluster

  • Install docker-ce runtime per the basic instructions on docker.com.
  • Disable the k3s default lb(klipper-lb).
  • use host networking for the docker container.
  • tell k3s server to pass --ipcs-strict-arp to its kube-proxy code path.

k3d cluster create my-cluster --no-lb --network host --k3s-arg "--kube-proxy-arg=--ipvs-strict-arp"

Install and configure MetalLB

kubectl apply -f https://raw.githubusercontent.com/mattsn0w/k3s-home/main/metallb/metallb-native_v13.7.yaml You might need to apply this twice since its slow to load first time.
kubectl apply -f https://raw.githubusercontent.com/mattsn0w/k3s-home/main/metallb/l2advertise.yaml

Modify the address pool range based on the subnet info in WSL ip a s output

wget https://raw.githubusercontent.com/mattsn0w/k3s-home/main/metallb/ipaddresspool.yaml
kubectl apply -f ipaddresspool.yaml

alias k=kubectl

Create a simple service loadbalancer and deployment

k create svc loadbalancer my-metal-lb --dry-run=client -o yaml --tcp=80:80 > svc-lb.yaml
k apply -f svc-lb.yaml # give it a look over before applying.
k create deployment lb-web-demo --image=nginxdemos/hello --replicas=2 --port=80 --dry-run=server -o yaml > deployment.yaml
# Edit the deployment to ensure that you have the selector.app set to match the load balancer service.
vim deployment.yaml
k apply -f deployment.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
generation: 1
labels:
app: lb-web-demo
app: my-metal-lb
name: lb-web-demo
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: lb-web-demo
app: my-metal-lb
template:
metadata:
labels:
app: lb-web-demo
app: my-metal-lb
spec:
containers:
- image: nginxdemos/hello
imagePullPolicy: Always
name: hello
ports:
- containerPort: 80
protocol: TCP
---
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: home-net-pool
namespace: metallb-system
spec:
addresses:
- 172.23.90.100-172.23.90.105
---
apiVersion: v1
kind: Service
metadata:
labels:
app: my-metal-lb
name: my-metal-lb
spec:
ports:
- name: 80-80
port: 80
protocol: TCP
targetPort: 80
selector:
app: my-metal-lb
type: LoadBalancer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment