Creating Kubernetes cluster on DigitalOcean

First of all, why this article? This article is for those people like me, who want to quickly create a simple Kubernetes cluster to learn and try out things. Also, use it for some medium size applications. But you got lost in jargon, documentation, and terminologies surrounding it.

There are several ways to create a Kubernetes cluster – AWS, Azure, DigitalOcean, etc. The list continues. What I find good about DigitalOcean is – it is one of the cheapest and quickest using DigitalOcean. Yes, there are more complex solutions and more features in other options, but that is not my aim as a beginner.

What we know about the Kubernetes cluster, it has one or more master nodes and one or more worker nodes. And there is a CLI available called kubectl. Using kubectl, we inform Kubernetes cluster how many pods to run, what configmaps and services to be present, etc. The Kubernetes cluster accordingly should do the job. So, my option has been DigitalOcean.

The setup includes two parts.

  1. Kubernetes cluster with two worker nodes on DigitalOcean.
  2.  Ubuntu machine I have taken on AWS where I will install kubectl.

The second point can be any machine, even your laptop. But I prefer keeping all things on the cloud.

With no more time spent on theory, let’s start with the steps.

1. Sign up in DigitalOcean and fill in your billing details

Below is the first page you see after signing up. They would not charge you unless you are going to use any resources. Click on the “Explore our control panel” link on this page.

2. First project page is displayed

3. Click on the “Create” dropdown

You would see the option “Create Kubernetes clusters” in the dropdown of Create as below.

4. The “Create a Kubernetes cluster” page is displayed

5. Fill up each of the settings for the cluster creation

The entire page with the options I filled is as in the below image. We will discuss each setting in detail.

VPC Network: You can fill in the nearest or your choice of location for this option. We have filled NYC.

Kubernetes Version: We left the recommended latest version as it is in the selection.

Cluster capacity: We have chosen Fixed as we want the most simple possible configuration. We selected the machine type and node plan for the most basic ones present. The aim is to get minimal and cheapest options as we are just using them for learning or demo purpose. In an actual production system, you can choose more high-end options.

Nodes: We have chosen the number of worker nodes as 2. We want multiple nodes to test out things, but we do not want to scale too much. Two nodes are enough.

The rest of the options are left as they are. And we can click on the “Create cluster” button.

6. The node pool status portion shows the status as “Provisioning”

7. Node status becomes “Running”

8. The “Resources” tab shows the node pool name and each of the nodes

9. Click on Dashboard for the node pool to see the workloads page

10. Click on Nodes in the left tab to see details of each of the worker nodes

11. Click on the Overview tab and click on “Get Started”

12. “Connecting to Kubernetes” page displays connection options

Usually, we consider the Kubernetes cluster as a set of one or more worker nodes, one or more master nodes, and one admin node where the control plane and coredns can reside. kubectl is a CLI utility provided on the admin node. Using this CLI, you can spawn the instructions to the Kubernetes cluster.

DigitalOcean setup provides only worker nodes and keeps Core DNS and Control Plane on separate nodes.

As seen in the below image, it provides two CLIs – doctl and kubectl options. We should install either of these on our machines and send instructions to the Kubernetes cluster. We have chosen the kubectl option as it is more standard. Also, we want full control of each step while doing operations on the Kubernetes cluster.

13. Download the kubeconfig YAML and issue kubectl commands

Login to your machine. Download kubectl as per the OS. In the above case, I took an AWS-hosted Ubuntu machine, and installed kubectl using instructions from the link in the above image that says “Kubernetes official client”.

The individual steps followed for the same are as below.

$ curl -LO "$(curl -L -s"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   138  100   138    0     0    579      0 --:--:-- --:--:-- --:--:--   577
100 45.7M  100 45.7M    0     0  39.4M      0  0:00:01  0:00:01 --:--:--  112M

$ curl -LO "$(curl -L -s"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   138  100   138    0     0    585      0 --:--:-- --:--:-- --:--:--   587
100    64  100    64    0     0    134      0 --:--:-- --:--:-- --:--:--   134

$ echo "$(cat kubectl.sha256)  kubectl" | sha256sum --check
kubectl: OK

$ sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

$ kubectl version --client
WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short.  Use --output=yaml|json to get the full version.
Client Version: version.Info{Major:"1", Minor:"26", GitVersion:"v1.26.0", GitCommit:"b46a3f887ca979b1a5d14fd39cb1af43e7e5d12d", GitTreeState:"clean", BuildDate:"2022-12-08T19:58:30Z", GoVersion:"go1.19.4", Compiler:"gc", Platform:"linux/amd64"}
Kustomize Version: v4.5.7

Finally, we are displaying the version of kubectl by using “kubectl version –client” command. The version of kustomize is v4.5.7, go is go1.19.4, version of kubectl is 1.26.

With the downloaded kubeconfig file, we can issue kubectl commands to know the nodes or cluster info as below. We can use the config file in the argument as below.

$ mkdir kubernetes

$ cd kubernetes/

~/kubernetes$ pwd

Download the kubeconfig file here.

~/kubernetes$ kubectl --kubeconfig=/home/ubuntu/kubernetes/k8s-1-25-4-do-0-nyc1-xyz-kubeconfig get nodes
NAME                   STATUS   ROLES    AGE   VERSION
pool-s6agz78ze-m6ae5   Ready    <none>   29m   v1.25.4
pool-s6agz78ze-m6aes   Ready    <none>   29m   v1.25.4

There is an alternative method. Keep the KUBECONFIG environment variable in the .bashrc file to point to the kubeconfig file, as shown below. Now, we can issue kubectl commands without passing the kubeconfig file each time as an argument.

~/kubernetes$ cat ~/.bashrc | grep KUBE
export KUBECONFIG=/home/ubuntu/kubernetes/k8s-1-25-4-do-0-nyc1-xyz-kubeconfig

~/kubernetes$ source ~/.bashrc

~/kubernetes$ kubectl get nodes
NAME                   STATUS   ROLES    AGE   VERSION
pool-s6agz78ze-m6ae5   Ready    <none>   32m   v1.25.4
pool-s6agz78ze-m6aes   Ready    <none>   33m   v1.25.4

That’s it.

The Kubernetes cluster with two worker nodes and our machine as the client is ready. We can have more variations like volumes, secrets setup, and database setup automated in the above process. But to learn Kubernetes in fully detailed ways, it is good to do them without using any automation provided by respective third-party tools.