Playing with Kubernetes using k3d and Rancher

Prakhar Malviya
47Billion
Published in
4 min readMay 28, 2022

Kubernetes is a container orchestration system that is considered the standard for deploying containerized applications. It is popular to the point that a lot of professional developers don’t even know that there are alternatives available. The reason for that is that Kubernetes is powerful, stable, flexible, and easy to use(for the most part). While most people find it easy to work with, it is still something scary for the new developers. There are multiple reasons for that that we can do nothing about, but 2 of them do not fall under that category -

  1. It is difficult to get your hands on a proper cluster that you can use freely just to try things out.
  2. It is hard to visualize the cluster using only the CLI tools until you get used to the structure.

We will try to get rid of these using some simple opensource tools including the 2 mentioned in the title. Here is a list of everything that we will install and a short description -

  1. Docker: If you don’t know what docker is, you are in the wrong place.
  2. Kubectl: It is a command-line tool used to control the cluster.
  3. Helm: It is like an installer. You tell helm what you want to be installed in your cluster and how you want it installed, helm will do the rest for you. (There is some witchcraft involved)
  4. K3d: k3d is a community-driven project, that is supported by Rancher (SUSE). It is a lightweight wrapper to run k3s in docker.
  5. K3s: Although we are not going to install it explicitly, we will use it. It is a lightweight production-ready Kubernetes distribution by Rancher.
  6. Rancher: It is a software stack that will make our lives a lot easier. Mainly by providing a very nice UI.

You might find the above description a bit questionable, but hopefully, it gave you a little understanding of what these things are. Now that all that is out of the way, let’s begin.

Note: The following process has been improved and automated at:

https://github.com/NuclearExperiments/KubeSandbox

If you are learning Kubernetes, it would be beneficial for you to go through the entire thing. If you just need a cluster for testing, you can skip to the link.

1. Installing the binaries

We will assume that you already have docker installed. If you do not have it, follow this. If you are a Windows user, you can still follow the rest using WSL. You can use this to get you started.

Go to the terminal and run the following -

# install kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
# install helm (this one takes some time)
curl -s https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
# install k3d
curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash

This is a one-time process. The rest, you will probably repeat a lot if you are serious about learning.

2. Creating the cluster

While creating a cluster, you need to make sure you expose some ports. Since k3d uses docker containers as nodes, you won’t be able to expose anything once the node is up. Use the following to create your cluster -

k3d cluster create mycluster -p "8900:30080@agent:0" -p "8901:30081@agent:0" -p "8902:30082@agent:0" --agents 2

This will create a cluster named “mycluster” with 3 ports exposed 30080, 30081 and 30082. These ports will map to ports 8900, 8901 and 8902 of your localhost respectively. The cluster will have 1 master node and 2 worker nodes. You can adjust these settings using the p and the agent flags as you wish.

You can check the nodes using -

kubectl get nodes

3. Deploying Rancher

Add the Rancher helm repository using -

helm repo add rancher-latest https://releases.rancher.com/server-charts/latest

Then install it using -

helm install rancher rancher-latest/rancher \
--namespace cattle-system \
--create-namespace \
--set ingress.enabled=false \
--set tls=external \
--set replicas=1

So, is it done? No, you still need a way to access the UI. For that, we will use a nodeport.

4. Creating the nodeport

Create a file called rancher.yaml and paste the following into it -

apiVersion: v1
kind: Service
metadata:
labels:
app: rancher
name: ranchernp
namespace: cattle-system
spec:
ports:
- name: http
nodePort: 30080
port: 80
protocol: TCP
targetPort: 80
- name: https-internal
nodePort: 30081
port: 443
protocol: TCP
targetPort: 443
selector:
app: rancher
type: NodePort

Then apply it using -

kubectl apply -f rancher.yaml

All done. Now you can explore your cluster at https://localhost:8901. Note the use of https. We are using that because it is compulsory in Rancher due to security reasons. But since we do not have a valid SSL certificate, you will get a warning from your browser. You can just click the “Advanced” button and then click the continue link below it. Your cluster is ready. GO WILD !!!!

--

--