This repository contains the source code for creating a local Kubernetes cluster setup using either K3d or Rancher Kubernetes Engine (RKE). It also contains a set of manifest files to deploy a basic application to your local K8s cluster.
The slide deck for the Introduction to Kubernetes
course can be found in the slide-deck folder.
Rancher Desktop, download the latest version of _Rancher Desktop_for your desktop operating system.
This link for kubectl will cover the full installation of kubectl
. Download and install kubectl
for your desktop operating system.
k3d is a lightweight wrapper to run k3s (Rancher Lab’s minimal Kubernetes distribution) in docker.
The first step is to ensure that the docker engine is running on your machine. Once you've verified that, you can proceed to create a cluster with the following command:
k3d cluster create example-cluster --servers 3 --agents 2
You can delete a cluster with the following command:
k3d cluster delete example-cluster
RKE is a CNCF-certified Kubernetes distribution that runs entirely within Docker containers.
To spin up the virtual machines, run the following command at the root level of the project directory:
vagrant up
Once the VMs are up and running, you can check their status with vagrant status
or by connecting to anyone of them using vagrant ssh [hostname]
. Once you've confirmed that all machines are running with no issues, copy over the generated SSH keys from your workstation/host to each guest machine with the following commands:
ssh-copy-id root@[relevant ip address]
When prompted, enter the root user password configured in the bootstrap node script.
To provision the cluster on the VMs, run the rke config
command. You will be presented with a series of questions to which the answers will be used to declare the cluster config in a generated cluster.yml
file upon completion. Alternatively, you can create the cluster.yml file and populate it with your desired configuration. Once you have the cluster.yml
file, run the following command:
rke up
When the cluster has been provisioned, the following files will be generated in the root directory:
- cluster.rkestate - the cluster state file
- kube_config_cluster.yml - kube config file
To add the cluster to your context, copy the kube config file:
cp kube_config_cluster.yml ~/.kube/config
If you do not have a ./kube
directory on your machine you will have to create one.
The last step will be to check that you can connect to your cluster:
kubectl cluster-info
or
kubectl config current-context
No matter what cluster you are using k3s
, k3d
, RKE
or even Rancher Desktop
, you should be able to leverage the manifests located in the manifests folder. These are good to test out the kubectl
and one of the aforementioned Kubernetes clusters.
This command will let you know what of the many clusters your kubectl
is pointed to:
kubectl config view
You should see something similar to this in the response:
current-context: rancher-desktop
In your terminal change directories to the manifests folder in this GitHub repository.
Once you are in this directory you can run the following command to create a pod:
kubectl apply -f pod.yaml
And you should see something like this as a response:
pod/express-test created
The describe
command will give you the details on a particular object in the cluster. We are going to describe the pod that we just created:
kubectl describe pod express-test
The responses should look like something below (might not be exact):
Name: express-test
Namespace: default
Priority: 0
Node: lima-rancher-desktop/192.168.5.15
Start Time: Mon, 25 Oct 2021 08:40:45 -0400
Labels: app=express-test
Annotations: <none>
Status: Running
IP: 10.42.0.36
IPs:
IP: 10.42.0.36
Containers:
express-test:
Container ID: containerd://f6d08505d3509140d575ff0e76762f622d26e8c3cc1d54b20713eb49ba91b3e2
Image: lukondefmwila/express-test:latest
Image ID: docker.io/lukondefmwila/express-test@sha256:c53f609cb6daadc3e161745c969b7446694205925fe2ce33f461b5c980cbd8ef
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 25 Oct 2021 08:40:54 -0400
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-2kwb2 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-2kwb2:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-2kwb2
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 8m12s default-scheduler Successfully assigned default/express-test to lima-rancher-desktop
Normal Pulling 8m12s kubelet Pulling image "lukondefmwila/express-test:latest"
Normal Pulled 8m3s kubelet Successfully pulled image "lukondefmwila/express-test:latest" in 8.463684s
Normal Created 8m3s kubelet Created container express-test
Normal Started 8m3s kubelet Started container express-test
The delete
command can delete an object from your cluster. In this we will delete the pod we just created:
kubectl delete pod express-test
The output should look something like this:
pod "express-test" deleted