Modal Title
Cloud Native Ecosystem / Kubernetes

Quickly Install a Kubernetes Cluster with KubeKey

KubeKey makes it really easy to spin up a Kubernetes deployment for development/testing purposes.
Mar 19th, 2022 9:00am by
Featued image for: Quickly Install a Kubernetes Cluster with KubeKey
Feature image by carmen6969 from Pixabay

Kubernetes can be a real challenge to get up and running properly. This is especially so when you really want it done quickly so you can create a testing or development environment or just get familiar with how it works. You might not have time to spin up a multinode Kubernetes cluster every time you need one. When that’s the case, you can always turn to KubeKey, which makes it really easy to spin up a Kubernetes deployment for development/testing purposes.

Don’t think, however, that using KubeKey in this fashion is a means to deploy your apps and services at scale. What I’m going to show you is primarily for testing and development purposes, so don’t even think about this as a solution for production-ready apps or services.

In order to make this work, you’ll need a running instance of Ubuntu Server 20.04 (or newer) and a user with sudo privileges. You might also want to have your instance of Ubuntu Server with a complete desktop environment (so you can deploy the Kubernetes Dashboard — which I’ll show you how to do as well).

With that said, let’s get to work.

Install Docker

The first thing we need to do is install Docker. Instead of installing the version of Docker found in the standard repositories, we’ll go the extra mile and install the community edition of Docker.

First, let’s install the necessary dependencies. Log into your server and issue the command:

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common -y

Next, add the official Docker GPG key with:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Add the Docker repository with the command:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu  $(lsb_release -cs)  stable"

Update apt and install Docker with:

sudo apt-get update

sudo apt-get install docker-ce -y

Start and enable the Docker service with:

sudo systemctl enable --now docker

Add your user to the docker group with:

sudo usermod -aG docker $USER

Make sure the changes take effect with:

newgrp docker

Install KubeKey

Before we download KubeKey, there’s another dependency to take care of, which can be done with the command:

sudo apt-get install conntrack -y

Download the KubeKey binary with:

curl -sfL https://get-kk.kubesphere.io | VERSION=v1.2.1 sh -

Once the file downloads, make it executable with:

chmod u+x kk

Let’s make the kk executable such that it can be run from any directory by copying the file to the /user/local/bin directory. Do that with:

sudo cp kk /usr/local/bin

Verify the installation with:

kk -h

If you see the help information printed out, the kk executable is ready to go.

Deploy the Cluster

We can now deploy the cluster with:

sudo kk create cluster

Unfortunately, you do have to use sudo privileges to run this. This is one of the reasons why I believe KubeKey’s best use case is for dev/testing/educational purposes only.

This process will take some time, as it has to download a number of binary files and other bits to make it work. Once it completes, you’ll get your terminal window back and you’re ready to continue on.

You should now be able to verify kubectl has been installed with the command:

kubectl --help

This time you should see the help information for the kubectl command.

Deploy the Kubernetes Dashboard

Let’s take this one step further. This isn’t necessary, but it’ll help make things easier down the road (for a more detailed look at deploying the Dashboard, check out “Deploy Microk8s and the Kubernetes Dashboard for K98s Development” ). Deploy the Kubernetes Dashboard with the command:

sudo kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml

Once the dashboard has deployed, you’ll need to locate the IP address of the cluster, which can be done with the command:

sudo kubectl get svc -n kubernetes-dashboard

You should see kubernetes-dashboard listed along with the CLUSTER-IP address and the port number in the form of:

kubernetes-dashboard        NodePort    10.233.33.37    <none>        443:31693/TCP   57m

You might want to expose the dashboard via NodePort instead of ClusterIP. Do that, issue the command:

sudo kubectl edit svc kubernetes-dashboard -o yaml -n kubernetes-dashboard

This will open the config file in the vi editor. You must first type i to get into interactive mode. Then scroll down until you see:

type: ClusterIP

Change that to:

type: NodePort

Hit escape to get out of interactive mode and then type :wq to save and close the file. Next, you must run the kubectl proxy command like so:

sudo kubectl proxy

While the proxy is running, open a web browser and point it to the IP address and port number listed in the results from the sudo kubectl get svc -n kubernetes-dashboard command. You will be presented with the login screen, where you’ll need an access token. To retrieve that token, issue the command:

sudo kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

This should print out a long string of random characters. Copy and paste that string into the token field and you’ll then be allowed access to the Kubernetes Dashboard.

And that’s how you can quickly spin up a Kubernetes development environment without having to go through all of the trouble of deploying an entire cluster. This isn’t production-ready, but it’s a great way to get up to speed with Kubernetes and even develop for the platform.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.
TNS owner Insight Partners is an investor in: Docker.