Set-up multi node docker cluster with KIND
Why
Because when playing with kubernetes locally, I would like to see more realistic scenarios of having multiple nodes. By default Kind comes with only one node.
Setup
- Create
~/kind.yaml
config (or whatever location you prefer). - Set content to
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30303
hostPort: 8881
- role: worker
extraPortMappings:
- containerPort: 30303
hostPort: 8882
- role: worker
extraPortMappings:
- containerPort: 30303
hostPort: 8883
- Create new cluster with command
kind create cluster --config ~/kind.yaml
. - Note that every change of config requires cluster recreation.
To verify your setup run docker ps
where you should see proper port mapping for each “node” which is actually one docker container.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
e037790892d0 kindest/node:v1.24.0 "/usr/local/bin/entr…" 3 weeks ago Up 2 weeks 0.0.0.0:8882->30303/tcp kind-worker
46c54b2558b0 kindest/node:v1.24.0 "/usr/local/bin/entr…" 3 weeks ago Up 2 weeks 127.0.0.1:45169->6443/tcp, 0.0.0.0:8881->30303/tcp kind-control-plane
9d3b4e50247f kindest/node:v1.24.0 "/usr/local/bin/entr…" 3 weeks ago Up 2 weeks 0.0.0.0:8883->30303/tcp kind-worker2
Expose –type=NodePort
You can modify your setup ports however you like, but note you will want to have container ports higher than 30000 due to kubernetes default node port range (30000-32767). containerPort
is the port where you can expose your pod nodePort
, while hostPort
is the port which will be exposed on the localhost.
Example
Let’s create 3 deployments, which has visualization set in-place, if everything is working fine. This is used as example in great udemy course Kubernetes Mastery by Bret Fisher.
kubectl create deployment db --image=bretfisher/wordsmith-db
kubectl create deployment web --image=bretfisher/wordsmith-web
kubectl create deployment words --image=bretfisher/wordsmith-words
Than we need to expose ports because our web is listening on port 80, api backend on 8080 and db on 5432.
kubectl expose deployment db --port=5432
kubectl expose deployment web --port=80 --type=NodePort
kubectl expose deployment words --port=8080
Here we are using NodePort for web because we would like to access web regardless of the cluster node we hit.
Now we need to edit config of web deployment because you cannot set NodePort
directly as parameter when exposing!
Run kubectl edit service web
and edit nodePort: 30303
entry under spec.ports
. You should have something like this
...
spec:
...
ports:
- nodePort: 30303
port: 80
protocol: TCP
targetPort: 80
...
You should be able to access web service by going to http://localhost:8881 or http://localhost:8882 or http://localhost:8883 and see the following image. If you see empty LEGO bricks, something’s wrong.
Node port is routing traffic to web server, because of our Kind config and NodePort service type.
This setup was tested on mac and windows WSL2.