K3d - How To
Overview
k3s
is the lightweight Kubernetes (k8s) distribution by Rancher: rancher/k3s
k3d
creates containerized k3s
clusters. This means, that you can spin up a multi-node k3s
cluster on a single machine using Docker.
Source code and further information is on GitHub, here: https://github.com/rancher/k3d
Background
In order to facilitate development of applications running in Kubernetes environments, localized testing is always a preferred point of initial development.
To keep a local environment small, but at parity to a larger “enterprise” k8s deployments, the Rancher team developed k3s
as a form of “micro-kubernetes” that’s more of a distribution than a workstation flavor (i.e, minikube). As noted in the Overview, k3d
is simply a containerized version of this k3s
.
This k3d
deployment allows for a simple, usable Docker-run k8s cluster on a workstation to solve localized testing. It allow removes a lot of the Kubernetes cluster management overhead one would see should they deploy a full cluster locally.
Deployment
Prerequisites
- Docker
- MacOS: https://docs.docker.com/docker-for-mac/install/
- Windows: https://docs.docker.com/docker-for-windows/install/
- Kubernetes management tools:
kubectl
: (https://github.com/kubernetes/kubectl)brew install kubernetes-cli
choco install kubernetes-cli
helm
: (https://github.com/helm/helm)brew install helm
choco install kubernetes-helm
These are best installed via package managers to ensure you’re using the latest release, however manual binaries are available from the Helm and Kubernetes repositories.
Installation
Multiple options, including Homebrew (Mac/Linux) and Chocolatey (Windows), depending on your flavor of workstation.
- Homebrew:
brew install k3d
- Chocolatey:
choco install k3d
You can also consult the GitHub repository for additional deployment methods.
Common Uses
- Create cluster with single node
k3d cluster create mycluster
- This also merges the configuration into your default kubeconfig (
~/.kube/config
) used bykubectl
- Create cluster with multiple nodes
k3d cluster create multiserver --servers 3
- Map ingress to a localhost port, allowing you to expose that as a service
k3d cluster create localawesomeness --api-port 6550 -p "8081:80@loadbalancer" --agents 2
- Further example as to what this is doing: https://k3d.io/usage/guides/exposing_services/
- Deploy a Helm chart (Bitnami repo in this example)
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install mariadb bitnami/mariadb
- Delete a cluster
k3d cluster delete k3s-default
TL;DR (MacOS)
brew install -q kubernetes-cli helm k3d
k3d cluster create localawesomeness
kubectl get all --all-namespaces
k3d cluster list
echo "Ready for action..."
Further Information
- K3s: https://rancher.com/docs/k3s/latest/en/
- K3d: https://k3d.io/
- Decent blog post adding additional details: https://yannalbou.medium.com/k3s-k3d-k8s-a-new-perfect-match-for-dev-and-test-e8b871aa6a42