Setup your personal Kubernetes cluster with k3s and k3d

There are a lot of reasons why you might want to have your personal Kubernetes cluster. Kubernetes cluster on your development machine, giving you fast iteration times in a production-like environment.

There are several option to setup a Kubernetes cluster on a local machine for development or testing purposes. But with a full-blown Kubernetes cluster running on your local machine, you will soon hit a wall if you want to play with multi-node cluster or multiple clusters on the same machine.

To address this issue, we will be looking into how we can setup a lightweight Kubernetes cluster using k3s and k3d in our local machine.

What is k3s?

K3s is a lightweight, easy-to-use, CNCF-certified Kubernetes distribution of Kubernetes created at Rancher Labs. Designed for low-resource environments, K3s is distributed as a single <40MB binary that uses under 512MB of RAM.

If you are interested in what makes k3s so light, you can watch the talk on k3s under the hood.

What is k3d?

k3d is a lightweight wrapper for running a K3s cluster in Docker. k3d makes it very easy to create single and multi-node k3s clusters in docker, e.g. for local development on Kubernetes.

k3d uses a Docker image built from the K3s repository to spin up multiple K3s nodes in Docker containers on any machine with Docker installed. That way, a single physical (or virtual) machine (let’s call it Docker Host) can run multiple K3s clusters, with multiple server and agent nodes each, simultaneously.

If you are interested in understanding more on k3d, you can watch the talk on Simplifying Your Cloud-Native Development Workflow With K3s, K3c and K3d

Installation

Installation is very easy and available through many installers such as wget, curl, Homebrew, Aur etc. and supports all well known OSes (linux, darwin, windows) and processor architectures (386, amd64)

I am installing using the following comments in Ubuntu 16.04. Please refer instructions from the official documentation for your environment.

Let us verify the installed version

List existing clusters

Of course, there are no existing clusters.

Cluster Creation — The “Simple” Way

Lets create a first k3d cluster by running the following comments

By default, k3d cluster create command creates a single node cluster with a default name.

Use help command to know all possible parameters for cluster create command

Let us list the created cluster

Now, we can see a cluster created with default name k3s-default

by default, cluster create command also configured kube config file under ~/.kube/config

docker ps will show the underlying containers created by the cluster create command

Let us clean up the created resources

You can use the following command to create a single node cluster with name dev-cluster and the following port requirements

  • add a mapping of local host port 8080 to loadbalancer port 80, which will proxy requests to port 80 on all agent nodes
  • add a mapping of local host port 8443 to loadbalancer port 443, which will proxy requests to port 443 on all agent nodes

Let us test the cluster by deploying and exposing a simple nginx container application

Now you can access the nginx application from your local machine on http://localhost:8080

Cluster Creation — The “Simple but Sophisticated” Way

Let us create a k3s cluster with name dev-cluster with three master and three worker nodes.

Port Mapping Requirements

  • add a mapping of local host port 8080 to loadbalancer port 80, which will proxy requests to port 80 on all agent nodes
  • add a mapping of local host port 8443 to loadbalancer port 443, which will proxy requests to port 443 on all agent nodes
  • add a mapping of local host port 6443 to loadbalancer port 6443 so that the load balancer will be the access point to the Kubernetes API, so even for multi-server clusters, you only need to expose a single api port. The load balancer will then take care of proxying your requests to the appropriate server node
  • You may as well expose a NodePort range (if you want to avoid the Ingress Controller) by -p “32000–32767:32000–32767@loadbalancer”

Let us verify the k3d nodes

Let us verify the k3s cluster info and node details

References:

https://en.sokube.ch/post/k3s-k3d-k8s-a-new-perfect-match-for-dev-and-test-1

Site Reliability Engineer linkedin.com/in/surenraju/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store