Lesson 05
Creating our first cluster
~3 min read
You can set up a local Kubernetes cluster using tools like Minikube, Kind or k3s/k3d. We are going to be using k3d. K3d is a lightweight wrapper to run k3s (Rancher Lab's minimal Kubernetes distribution) in docker. k3d makes it very easy to create single- and multi-node k3s clusters in docker.
To install k3d, you can use Homebrew by running the command brew install k3d. Alternatively, you can download the binary from the k3d GitHub repository and install it manually.
It is going to come in handy to have a local cluster running so let's set one up.
To set up a local Kubernetes cluster with k3d, open your terminal and enter the following command:
k3d cluster create localk8s
What did we create?
We just created a single-node, lightweight Kubernetes cluster inside docker. If you run kubectl cluster-info you should see:
kubectl cluster-info
Kubernetes control plane is running at https://0.0.0.0:49606
CoreDNS is running at https://0.0.0.0:49606/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://0.0.0.0:49606/api/v1/namespaces/kube-system/services/https:metrics-server:https/proxyYou can view our node by running kubectl get nodes:
kubectl get nodes
NAME STATUS ROLES AGE VERSION
k3d-localk8s-server-0 Ready control-plane 13m v1.31.2+k3s1When we are done using our cluster we can stop it to avoid wasting resources. You can stop the cluster with:
k3d cluster stop localk8s and remove it with k3d cluster delete localk8s
Currently there aren't any workloads running in our cluster, you can verify that by running kubectl get pods. This isn't exactly true -- there are no workloads running in our default namespace. If we take a closer look at the kube-system namespace by running kubectl get pods -n kube-system, we see that there are indeed workloads running.
kubectl get pods -n kube-system
local-path-provisioner-957fdf8bc-dcsjm 1/1 Running 0 18h
coredns-77ccd57875-vb7ff 1/1 Running 0 18h
helm-install-traefik-crd-8gjxr 0/1 Completed 0 18h
helm-install-traefik-g7dzq 0/1 Completed 1 18h
metrics-server-648b5df564-6pqp2 1/1 Running 0 18h
traefik-64f55bb67d-kwnv4 1/1 Running 0 18hThese are workloads that ran or are running to maintain our cluster operational.
Finally we are going to create the container registry that will store the containers that we will end up orchestrating with Kubernetes. Note that we pass --registry-use when creating the cluster so it can pull images from the registry:
k3d registry create localk8s --port 12345
k3d cluster delete localk8s
k3d cluster create localk8s --registry-use k3d-localk8s:12345We can view the registry we created by invoking k3d registry list.
This registry is named k3d-localk8s and we can communicate with it by querying localhost:12345. Let's query this registry for its current catalogue:
curl -X GET localhost:12345/v2/_catalog
{"repositories":[]}As expected there are no repositories yet for our container registry but that will soon change.
Note: By default localhost is configured as the host for the registry and it is accessible on port number 12345 like we configured. If you would like to use the name of the registry as the host and access it via k3d-localk8s:12345 you will have to add a mapping in the /etc/hosts file, more on that here.
Now that we have a cluster and registry setup we can start to deploy something to it.