Skip to main content

Rancher Lab Environment with K3d

What is Rancher ? #

Rancher is a Kubernetes management tool to deploy and run clusters anywhere and on any provider.

Rancher can provision Kubernetes from a hosted provider, provision compute nodes and then install Kubernetes onto them, or import existing Kubernetes clusters running anywhere.

Rancher adds significant value on top of Kubernetes, first by centralizing authentication and role-based access control (RBAC) for all of the clusters, giving global admins the ability to control cluster access from one location.

It then enables detailed monitoring and alerting for clusters and their resources, ships logs to external providers, and integrates directly with Helm via the Application Catalog. If you have an external CI/CD system, you can plug it into Rancher, but if you don’t, Rancher even includes Fleet to help you automatically deploy and upgrade workloads.

Rancher is a complete container management platform for Kubernetes, giving you the tools to successfully run Kubernetes anywhere.


What is K3d ? #

k3d is a lightweight wrapper to run k3s (Rancher Lab’s minimal Kubernetes distribution) in docker.

k3d makes it very easy to create single- and multi-node k3s clusters in docker, e.g. for local development on Kubernetes.

Note: k3d is a community-driven project but it’s not an official Rancher (SUSE) product.

Requirements ❯ β—‹ docker to be able to use k3d at all & β—‹ kubectl to interact with the Kubernetes cluster


If we’re ready, let’s start πŸƒ

Install K3d #

Installation is pretty simple. Here you can continue according to your operating system.

➜ brew install k3d

Create K3d Cluster #

We have completed the K3d installation. Now it’s time to create a cluster. I’m creating a cluster named racnher-01 with 3 master and 3 worker nodes. I also specify that it expose the ports I need to access Rancher. In K3d, agent refers to the worker node and server refers to the master.

➜ k3d cluster create rancher-01 \
  -p "8900:30080@agent:0" -p "8901:30081@agent:0" -p "8902:30082@agent:0" \ 
  --agents 3 \
  --servers 3 \
  --image rancher/k3s:v1.26.7-k3s1

I’m setting up cluster in specific version with v1.26.7-k3s1. This is because Rancher Stable version (2.7.5) requires kubernetes version < 1.27.0-0.

In a few seconds the cluster was created. Let’s see this.

➜ k3d cluster list
rancher-01   3/3       3/3      true

You can see all commands related to K3d here. It has a very minimal documentation and you can see what you can do with k3d in just a few minutes. If you are reading and applying this article, you can even add a new worker node to the cluster you created by looking at the document :)

Yes, let’s take a look at our cluster. πŸ™‹β€β™‚οΈ

➜ kubectl get nodes -o wide 
NAME                      STATUS   ROLES                       AGE   VERSION        INTERNAL-IP   EXTERNAL-IP   OS-IMAGE   KERNEL-VERSION        CONTAINER-RUNTIME
k3d-rancher-01-agent-0    Ready    <none>                      28s   v1.26.7+k3s1    <none>        K3s dev    5.15.49-linuxkit-pr   containerd://1.7.1-k3s1
k3d-rancher-01-agent-1    Ready    <none>                      28s   v1.26.7+k3s1    <none>        K3s dev    5.15.49-linuxkit-pr   containerd://1.7.1-k3s1
k3d-rancher-01-agent-2    Ready    <none>                      28s   v1.26.7+k3s1    <none>        K3s dev    5.15.49-linuxkit-pr   containerd://1.7.1-k3s1
k3d-rancher-01-server-0   Ready    control-plane,etcd,master   65s   v1.26.7+k3s1    <none>        K3s dev    5.15.49-linuxkit-pr   containerd://1.7.1-k3s1
k3d-rancher-01-server-1   Ready    control-plane,etcd,master   48s   v1.26.7+k3s1    <none>        K3s dev    5.15.49-linuxkit-pr   containerd://1.7.1-k3s1
k3d-rancher-01-server-2   Ready    control-plane,etcd,master   35s   v1.26.7+k3s1    <none>        K3s dev    5.15.49-linuxkit-pr   containerd://1.7.1-k3s1

Since there seems to be no problem, let’s start Rancher installation.

Deploy Rancher on K3d Cluster #

➜ helm repo add rancher-stable
➜ helm install rancher rancher-stable/rancher \
    --namespace cattle-system \
    --create-namespace \
    --set ingress.enabled=false \
    --set tls=external \
    --set replicas=3 

Here you can examine the chart version, values, document etc. The stable version was 2.7.5 when I installed it. Maybe we can look at cluster upgrade in another article :)

Let’s check with helm which version is installed.

➜ helm ls -a -n cattle-system
NAME           	NAMESPACE    	REVISION	UPDATED                                	STATUS  	CHART                        	APP VERSION
rancher        	cattle-system	1       	2023-08-11 21:58:39.511936 +0300 +03   	deployed	rancher-2.7.5                	v2.7.5
rancher-webhook	cattle-system	1       	2023-08-11 19:07:35.888035127 +0000 UTC	deployed	rancher-webhook-2.0.5+up0.3.5	0.3.5

It may take a while for the cluster to stand up. If we are sure that everything is ok with the cattle-system and cattle-fleet-system namespaces, let’s move on to the next step.

Expose Rancher with NodePort #

Create the NodePort service to access Rancher UI.

➜ kubectl apply -f - <<EOF
    apiVersion: v1
    kind: Service
        app: rancher
      name: ranchernp
      namespace: cattle-system
      - name: http
        nodePort: 30080
        port: 80
        protocol: TCP
        targetPort: 80
      - name: https-internal
        nodePort: 30081
        port: 443
        protocol: TCP
        targetPort: 443
        app: rancher
      type: NodePort

Now we can access the UI with localhost:8901. 🐍


as it also shows here

➜ kubectl get secret --namespace cattle-system bootstrap-secret -o go-template='{{.data.bootstrapPassword|base64decode}}{{"\n"}}'

Let’s get the default admin password with the command. then it will ask us to create a new password.


πŸ”₯ Now we can play with Racher inside. First let’s take a look at our local cluster.


That was the installation part. I am stopping the K3d cluster.

➜ k3d cluster stop rancher-01

You can delete it if you wish. πŸ”«

➜ k3d cluster delete rancher-01

Resources 🧐 #