Setting Up a Local Cluster

Cool, but how do I get access to a Kubernetes cluster for free?

This is usually the first question you ask when starting to learn about Kubernetes (k8s). One of the obvious choices is to sign up with a cloud provider that has a managed k8s offering. However, this is almost never free so you are limited based upon how much money you are willing to spend. Another option is to set up a cluster on your own bare-metal. If you are learning about creating a cluster then this is probably an ideal option. But will require you to purchase some hardware and worry about (and rightfully so) network security. But if all you want to do is deploy and manage applications on a cluster then this might be more work that you signed up for.

Luckily there are a few well tested solutions that allow you set up a k8s cluster on your laptop (or desktop). And these options don’t cost you a cent, well beyond what you’re already paying for in terms of internet and power.

I have not personally tried out using MicroK8s so won’t comment on it. Minikube is probably the most popular option from the list. It was what I used when I started my Kubernetes journey. At the time it met all of my needs and was very easy to setup and use. However, at the time I was not able to create clusters with multiple nodes. This made testing a few features impossible.

Kind and K3d create a K8s cluster by using Docker containers to function as the nodes of a cluster. Both of these options make it rather simple to work with multi-node clusters. One important distinction between these two options is that K3d uses a K3s as its Kubernetes distribution. As a result it is consumes less resources that Kind. However, this difference is small and I don’t think should be a deciding factor. However, the best feature provided by both of these options is the ability to create multiple clusters, and to specify these clusters through a configuration file. This makes it possible to recreate your exact cluster setup on a new machine.

I just happened to use K3d before discovering Kind (surprising I know), so it is what I will be using for the rest of this post.

Cluster definition

As mentioned above it is possible to create your k3d cluster(s) via configuration files. For my example I will be creating a cluster with one control plane node and three data plane nodes. K3d will automatically create a Docker container that will act as a load balancer. Therefore, I will also configure the cluster so that ports 80 and 443 in the cluster is linked with port 8080 and 8443 respectively, on my local machine. This way I can easily interact with server on the cluster without having to port-forward.

The following configuration accomplishes all of the above goals.

apiVersion:  k3d.io/v1alpha5
kind: Simple
metadata:
  name: demo-cluster
servers: 1 # Control plane nodes.
agents: 3 # Data plane nodes.
# Enable Ingress to expose services.
ports:
  - port: 8080:80
    nodeFilters:
      - loadbalancer
  - port: 8443:443
    nodeFilters:
      - loadbalancer
# Set node labels.
options:
  k3s:
    nodeLabels:
      - label: compute_type=expensive
        nodeFilters:
          - agent:1  

To ensure that everything is working as expected, you can deploy a single pod Nginx server exposed on port 80. Then once you navigate to localhost:8080 you will see the Nginx welcome screen.

All of configurations and code for this series of post can be found at https://github.com/putnam120/preview-environment.

Posts in this series