K3d: A lightning fast and easy way to run k8s on your local machine

Kubernetes is one of the most popular container orchestration platforms in the world, and K3s is a lightweight version of Kubernetes that’s optimized for edge computing. If you’re looking to run a K3s cluster locally, you might want to consider using K3d, a tool that makes it incredibly easy to run K3s on your machine. In this article, we’ll discuss what K3d is, how it works, and why you might want to use it.

What is K3d?

K3d is a command-line tool that creates and manages K3s clusters on your machine. With K3d, you can set up a K3s cluster in just a few seconds and start deploying your applications to it. K3d creates clusters that are isolated from each other, so you can run multiple clusters at the same time and not have to worry about them interfering with each other.

How does K3d work?

K3d works by creating Docker containers for each node in the cluster. Each node in the cluster runs a K3s agent, which communicates with the K3s server to manage the cluster. When you create a K3d cluster, the K3d tool creates a Docker container for the K3s server and one or more Docker containers for the K3s agents. The K3s server is responsible for managing the cluster, and the K3s agents are responsible for running your applications.

Why use K3d?

There are several reasons why you might want to use K3d to run a K3s cluster locally:

  1. It’s fast and easy to set up. With K3d, you can set up a K3s cluster in just a few seconds. You don’t have to worry about configuring nodes or setting up network connections, as K3d takes care of all of that for you.
  2. It’s isolated. Each K3d cluster is isolated from each other, so you can run multiple clusters at the same time and not have to worry about them interfering with each other.
  3. It’s a great way to test your applications. If you’re developing an application that’s going to run on a K3s cluster, you can use K3d to test it locally. You can test your application in a real K3s environment without having to worry about deploying it to a remote cluster.
  4. It’s great for learning. If you’re new to K3s and want to learn more about it, K3d is a great way to start. With K3d, you can set up a K3s cluster and start experimenting with it right away.
  5. It supports multi-node clusters. You can create a multi-node cluster in k3d using a single command. The command allows you to create additional nodes and join them to the same cluster, effectively creating a multi-node cluster.

Create a new cluster

Using K3d is incredibly easy. First, you’ll need to install the K3d tool on your machine. The official documentation provides instructions on how to do this for Windows, macOS, and Linux. Once you’ve installed K3d, you can create a K3s cluster by running the following command:

k3d cluster create my-k3s-cluster

This command will create a K3s cluster named my-k3s-cluster with one server node. You can access to the cluster via command:

kubectl cluster-info

Next, let’s explore how to manage the nodes in the cluster. You can use the list command to view all nodes in a cluster:

k3d node list

You can also use the ls command to list all clusters:

k3d cluster ls

Add more nodes to the cluster

Now, let’s say you want to add more nodes to your cluster. You can use the node create command to add more nodes to the cluster. For example, to add an agent node to the cluster, use the following command:

k3d node create my-agent-node --role=agent --cluster=my-k3s-cluster

The role of a node in a Kubernetes cluster refers to its function or purpose within the cluster. In a k3d cluster, nodes can have either the “server” or “agent” role. The “server” role is for the node that runs the Kubernetes control plane and is responsible for maintaining cluster state. The “agent” role is for nodes that run workloads and are managed by the control plane. However, in smaller clusters with limited resources, it is possible to run applications on the “server” nodes themselves. By default, new nodes created in k3d are assigned the “agent” role.

Expose services

In addition to creating and managing nodes, k3d also allows you to manage the network of your cluster. You can expose services running in the cluster to the host machine using the --port-add flag. For example, to expose a service running on port 30987 in the cluster, use the following command:

k3d cluster edit my-k3s-cluster --port-add 30987:30987@agent:0

Let’s say you want to access a service running on a specific node. You can use node filters to specify which node the --port-add flag should apply to. The syntax for node filters is @<group>:<subset>[:<suffix>]. You can find more information about node filters in the official documentation.

What’s next?

Like any other open source project, k3d has detailed documentation and a large community that will help you understand other features of the tool.

Continue your journey with them and find out what you can do else.

In conclusion

k3d is a powerful tool for creating and managing k3s-based Kubernetes clusters on your local development machine. With its easy-to-use commands and features, you can create, manage, and delete clusters quickly and efficiently. Try it