A Kubernetes tutorial for absolute beginners
In this tutorial, we will install a Kubernetes cluster on your laptop and deploy an Nginx Pod.
System requirements
You need a Linux, Mac(Intel or Apple Silicon), or Windows laptop with at least 6GB of memory to complete this tutorial.
Outline of steps
- Install Multipass
- Set up K0s
- Install and use kubectl
- Deploy Nginx Pods
- What’s next
Install Multipass
Multipass is a tool for running Ubuntu virtual machines on Linux, Mac (Intel or Apple Silicon), or Windows. We are going to install a Kubernetes cluster on a virtual machine created with Multipass on your laptop.
Install Multipass by following the installation instructions.
Set up K0s
K0s is a lightweight Kubernetes distribution optimized to be deployed on resource-constrained environments like laptops.
Launch a new virtual machine
Open the terminal on your laptop and launch a new virtual machine named k0s10
.
multipass launch -d 5G -m 2G -c 2 -n k0s10 22.04
This virtual machine has 5GB storage, 2GB memory, and 2 CPU cores. We use Ubuntu Jammy as the OS.
Log in to the virtual machine.
multipass shell k0s10
This takes us to the bash terminal of the k0s10
virtual machine.
This virtual machine is the host for our K0s cluster. All next steps must be done on the terminal of this virtual machine and not the terminal of your laptop unless specifically mentioned.
Install K0s
On the virtual machine k0s10
, run the K0s installation script.
The script will detect your CPU architecture (amd64
or arm64
), download the latest version of K0s, and install it at /usr/local/bin
.
curl -sSLf https://get.k0s.sh | sudo sh
Run k0s
as a service.
sudo k0s install controller --single
Start k0s
.
sudo k0s start
Check the status of k0s
service with systemctl
.
sudo systemctl status k0scontroller.service
The service should be in active
state.
Check k0s
status with k0s status
command.
sudo k0s status
Version: v1.28.4+k0s.0
Process ID: 1887
Role: controller
Workloads: true
SingleNode: true
Kube-api probing successful: true
Kube-api probing last error:
We have installed Kubernetes 1.28 version.
Now we have a Kubernetes cluster… up and running.
Kubernetes is an open-source project and the code is hosted on a GitHub. The Kubernetes project also releases binaries of Kubernetes which you can install by following their installation instructions. But, that’s a lenghthy process.
So we chose to install K0s instead. K0s is a Kubernetes distribution from Mirantis. K0s includes a set of installation scripts that makes it easy to install.
There are many Kubernetes distributions from vendors like Canonical, RedHat, etc. Most of those Kubernetes distributions are intended to be installed on multi-server environments. Since we are installing Kubernetes on a laptop, we opted for K0s which is more of a lightweight Kubernetes distribution.
Install and use kubectl
Our Kubernetes cluster is ready. To interact with the Kubernetes cluster, we use kubectl
- the Kubernetes CLI tool.
kubectl
need not be installed on the same server that runs Kubernetes. But, let’s install kubectl
also in the k0s10
virtual machine in this tutorial.
The kubectl
version must match the Kubernetes cluster version. Since we have installed Kubernetes 1.28
, let’s install the same version of kubectl
.
The kubectl
binaries are available for both amd64
and arm64
architecture. Download the appropriate binaries for your architecture by replacing <arch>
with either amd64
or arm64
.
curl -LO https://dl.k8s.io/release/v1.28.0/bin/linux/<arch>/kubectl
If you are unsure of what architecture your Ubuntu virtual machine you can check with dpkg --print-architecture
command.
Install kubectl
.
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
Verify that you’ve successfully installed kubectl
.
kubectl version --client
Client Version: v1.28.0
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
kubectl
need to know the URL and authentication information of the cluster. By default kubectl
look for these information in ~./kube/config
. This file is also known as the kubeconfig
file.
Let’s generate a kubeconfig
file for our K0s cluster.
sudo k0s kubeconfig admin > ~/.kube/config
Now we should be able to se kubectl
.
Print the nodes in the cluster with kubectl
.
kubectl get nodes
NAME STATUS ROLES AGE VERSION
k0s10 Ready control-plane 15h v1.28.4+k0s
We have just one node in the cluster.
We can use kubectl
to manage any resource in a Kubernetes cluster.
List the Pods.
kubectl get pods
No resources found in default namespace.
We don’t have any Pods in the default namespace, since we haven’t deployed any application on this Kubernetes cluster.
But we have Pods running in other namespaces which we can get useing -A
option (all namespaces).
kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system kube-proxy-zlnnv 1/1 Running 0 15h
kube-system kube-router-dxl8m 1/1 Running 0 15h
kube-system coredns-85df575cdb-wzq96 1/1 Running 0 15h
kube-system metrics-server-7556957bb7-4z4rj 1/1 Running 0 15h
The Pods in the kube-system
namespace belong to Kubernetes control plane.
Deploy Nginx Pods
We deploy an application on Kubernetes by creating Pods. A Pod runs one or more containers. A software application running on Kubernetes is a collection of Pods.
Let’s create a Pod that runs an Nginx web server.
kubectl run nginx-app --image=nginx --port=80
This command downloads the container image Nginx from Docker Hub and runs a single Pod in the default
namespace. The port
parameter specified the port that the Pod is listening on.
List the Pod.
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-app 1/1 Running 0 97s
Our Nginx Pod is running.
To get more details in the kubectl
output we can use the -o
option.
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-app 1/1 Running 0 2m50s 10.244.0.5 k0s10 <none> <none>
Let’s create a second Pod. Objects of the same type in Kubernetes must have unique names, so we name this Pod as nginx-app-2
.
kubectl run nginx-app-2 --image=nginx --port=80
List the Pods.
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-app 1/1 Running 1 (11m ago) 6h30m 10.244.0.6 k0s10 <none> <none>
nginx-app-2 1/1 Running 0 4s 10.244.0.9 k0s10 <none> <none>
The Pods are assigned with unique IP addresses. These IP addresses are internal to the cluster. To test the Nginx web server with curl
we need to use port forwarding.
Let’s forward port 4000 in the localhost to port 80 in nginx-app
Pod.
kubectl port-forward pod/nginx-app 4000:80
The port-forward
command does not return immediately.
So open a second terminal in the laptop and type in multipass shell k0s10
to open another termianl to the virtual machine k0s10
.
Test the Nginx web app from the virtual machine terminal.
curl 127.0.0.1:4000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
curl
returns the response with the home page of our Nginx web server.
In the terminal where the port-forward
command is running press Ctrl+c
to stop the running command.
Delete both Pods.
kubectl delete pod nginx-app
kubectl delete pod nginx-app-2
What’s next
In this tutorial, we installed a Kubernetes cluster on our laptop and deployed an Nginx web server using kubectl
. Now we are ready to explore more about Kubernetes.
Check out this article on introduction to Kubernetes to get an understanding on the architectural concept of Kubernetes.