Kubernetes

Kubernetes Home Lab Setup Step-by-Step

Kubernetes Home Lab Setup step-by-step with VMware vSphere virtual machines, Flannel networking, and Kubeadm

Have you wanted to get a Kubernetes Home lab setup to start learning Kubernetes? It can be an intimidating subject if you simply start Googling around and looking at various Kubernetes documentation. However, once you put together the actual steps required to put together a Kubernetes home lab setup using the kubeadm utility, it really isn’t that many steps. There is some prep work that you need to do beforehand however. In this lab setup, I am using VMware vSphere as the backend hypervisor to run the three virtual machines I am using as the Kubernetes nodes used as the master and worker nodes for the environment. Let’s take a look at this Kubernetes home lab setup step-by-step and see how it is put together.

VMware vSphere with Tanzu or not?

You may ask, why is this not using vSphere with Tanzu? The vSphere with Tanzu solution is an amazing solution, and I have Tanzu running in nested lab environments. This Kubernetes home lab though I wanted to be comprised of a more basic and vanilla setup to help learn the inner-workings of setting up Kubernetes. I highly recommend learning both configurations. VMware is the easy-button for multi-cloud environments. However, it is great to also learn a more vanilla implementation of Kubernetes as well.

Leverage group snapshots for learning

One of the things I like to do when I am learning something new is leverage snapshots. Have you had a moment where you had things setup perfectly and then run a command that trashes everything? Snapshots are ideal for learning something new. At each stage, when you have the environment working, take a snapshot.

For these types of situations, I like to leverage the “bulk snapshot” or mass snapshot if you like. Targeting a folder, you can create the same snapshot for all the VMs there. This works out great to keep the state of your lab in lock step. For my three Kubernetes VMs, I used the following:

Creating:
get-vm -location "KubernetesLab" | new-snapshot -Memory -quiesce -name "Before creating Kubernetes cluster - 6.17.2021"


Rolling back:
get-vm -location "KubernetesLab" | set-vm -snapshot "Before creating Kubernetes cluster - 6.17.2021" -confirm:$false

Kubernetes Home Lab Setup Step-by-Step

In this Kubernetes home lab setup, I will be using the following:

Vmware vsphere ubuntu virtual machines used as kubernetes control and worker nodes
Vmware vsphere ubuntu virtual machines used as kubernetes control and worker nodes
  • 3 Ubuntu 20.04 virtual machines – provisioned from template
    • See how to build an Ubuntu Packer template here
  • Kubeadm to initialize and provision the Kubernetes cluster
  • Flannel as the container network interface
  • Installing the Kubernetes Dashboard

Let’s look at the following steps to provision the Kubernetes home lab setup:

  1. Install DockerIO and change Docker to Systemd
  2. Install Kubeadm
  3. Initialize the Kubernetes cluster
    1. Export admin config
    2. Provision the network overlay
  4. Join worker nodes
  5. Install the Kubernetes Dashboard

1. Install Docker Container Runtime

You need to install a container runtime into each of your Kubernetes nodes for running Pods. Docker is the most popular option, so that is what I have installed in each of my Ubuntu 20.04 virtual machines. To install Docker in Ubuntu, use the following command:

sudo apt install docker.io

You will want to change Docker daemon to use systemd for the management of the containerโ€™s cgroups. To do this, after installing Docker, run the following:

cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF

Restart your Docker services

sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker

2. Install Kubeadm

Kubeadm is a tool that helps to install and configure a Kubernetes cluster in a much easier way than performing all steps manually. It helps to bootstrap a Kubernetes cluster with the necessary minimum configuration needed to get the Kubernetes cluster up and running. To install kubeadm, perform the following in Ubuntu. This updates the package index and installs packages needed to use Kubernetes apt repo, downloads the Google Cloud public signing key, adds the Kubernetes apt repo, and installs kubelet, kubeadm, and kubectl.

apt update && apt upgrade -y
sudo apt-get install -y apt-transport-https ca-certificates curl

sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg
https://packages.cloud.google.com/apt/doc/apt-key.gpg

echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

3. Initialize the Kubernetes Cluster

After installing kubeadm, we can use it to initialize the Kubernetes cluster. To do that, use the following command:

kubeadm init --pod-network-cidr=<your pods CIDR>
Example:  kubeadm init --pod-network-cidr=10.244.0.0/16

Export your admin.conf if you are logged in as root this way:

export KUBECONFIG=/etc/kubernetes/admin.conf
Exporting the config and copying the command to join kubernetes worker nodes
Exporting the config and copying the command to join kubernetes worker nodes

There are many networking solutions available for Kubernetes. You can find the full list and links here: Cluster Networking | Kubernetes. The next step is to provision the network overlay. I am using the Flannel network. To use it, run the following:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Setting up the flannel container network plugin
Setting up the flannel container network plugin

After joining the workers, as you can see below, you will see Flannel pods for your workers running.

Flannel pods up and running
Flannel pods up and running

4. Join Worker Nodes

To join Kubernetes worker nodes to the Kubernetes cluster, simply run the command that is displayed after you initialize the Kubernetes cluster with kubeadm. **Note** This will be run from the worker nodes. It will look something like this:

kubeadm join 10.1.149.140:6443 --token yx8nnx.sf7ep8657f3j5j6r \
        --discovery-token-ca-cert-hash sha256:8185d65970297cce220dc35f549d6343ad12a385f2b71b925bf21dae24916921
Joining a worker node to the kubernetes cluster
Joining a worker node to the kubernetes cluster
Master and two worker nodes running in the kubernetes cluster
Master and two worker nodes running in the kubernetes cluster

5. Install the Kubernetes Dashboard

You most likely will want to install the Kubernetes dashboard. To install the Kubernetes Dashboard, grab the latest YAML file from here: Web UI (Dashboard) | Kubernetes

The script will look like this:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml
Deploy the kubernetes dashboard using the official yaml script
Deploy the kubernetes dashboard using the official yaml script

Setup Nodeport networking by editing the kubernetes-dashboard svc. To do that, run the command:

kubectl -n kubernetes-dashboard edit svc kubernetes-dashboard

Edit the type to NodePort and add the nodePort section under ports. Choose an arbitrary port been 30000-32767.

Editing the kubernetes dashboard service for nodeport connectivity
Editing the kubernetes dashboard service for nodeport connectivity

Verify the Kubernetes-dashboard service using:

kubectl -n kubernetes-dashboard get svc
Verifying the kubernetes dashboard service afterwards
Verifying the kubernetes dashboard service afterwards
Accessing the kubernetes dashboard from the nodeport
Accessing the kubernetes dashboard from the nodeport

Kubernetes Home Lab from scratch video walkthrough

Wrapping up

This post has walked through a basic quick configuration of getting a Kubernetes home lab setup step-by-step with a few commands to run. Keep in mind using this approach you need to have your own VMs that will serve as Kubernetes worker nodes as well as the control node or master. Look for additional Kubernetes home lab setup step-by-step guides as I will cover the Dashboard further, as well as bringing in additional workloads.

Read how to place an Avi Load Balancer (NSX Advanced Load Balancer) in Standalone mode in front of the Kubernetes cluster here:

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

3 Comments

  1. Hi, perfect guide to k8s for beginning.
    I’ve just one problem – I run it on Ubuntu 2004 and after a reboot the cluster doesn’t come up with a lot of errors “Failed to create sandbox for pod” or “failed to setup network for sandbox”
    Have I forgot something?
    thx, Robert

    1. Hi,
      it was my fault.. the only thing that was missing, was the adminconfig in the home directory. So the Cluster is working and the Logs are from the startup.
      For the others, if you can’t connect via kubectl to your cluster – check if you have the config under ~/.kube/. Else you should copy it to the dir:
      cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
      br, Robert

  2. Instead of using your command
    sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg
    https://packages.cloud.google.com/apt/doc/apt-key.gpg
    which is looking for key in /usr/share/ use the below command to download the key first.
    curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg –dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
    You were using apt.kubernetes.io which is replaced with pkgs.k8s.io. and then add it source.list.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.