Beginner’s Guide : Simple hello Kubernetes (all-in-one) on a single VPS
Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications. In this tutorial I’m using kubeadm to bootstrap a single-node Kubernetes cluster and then taint / force the control plane’s scheduler to schedule our pod’s deployment on control plane node. If u want to try kubernetes on your local PC, you can try using minikube.
VPS Requirements
You can use https://idcloudhost.com as your VPS provider. Their VPS’s cheap (estimated IDR 87,000 / month or $6 / month) option meets our minimum requirements. For 2 CPU cores, 2GB RAM, and 20GB storage, you just need to pay IDR 120/hour or $0.0083/hour.
The payment method is pay as you go
, means pay per hour. You can stop the VPS anytime to reduce the charge and if you stop the VPS, you will be charged for storage service only. You also can delete the VPS service that you just built, so you won’t be charged at all.
In this tutorial, I’m using
Ubuntu 18.04
as my VPS’s operating system
Objectives
We will setup a single-node Kubernetes cluster on our VPS, deploy a simple nodejs “Hello, World!” app to our Kubernetes, and finally be able to access it via our VPS’s public IP
Setup Kubernetes and Hello World App
Here are all the complete steps to set up a single-node Kubernetes cluster and make it accessible from outside of our VPS network. Please don’t leave any steps, and follow them step by step.
Disable Swap
We MUST disable swap in order for the kubelet to work properly.
https://discuss.kubernetes.io/t/swap-off-why-is-it-necessary/6879/4
First disable swap
sudo swapoff -a
And then to disable swap on startup in /etc/fstab
sudo sed -i '/ swap / s/^\\(.*\\)$/#\\1/g' /etc/fstab
Install docker
If you already install docker via install -y docker.io
uninstall it first.
And then, follow these tutorial https://docs.docker.com/engine/install/ubuntu/
Install cri-dockerd
Reference https://kubernetes.io/docs/setup/production-environment/container-runtimes/#docker
Complete all the steps by following the tutorial on the link below.
Install kubelet, kubeadm, and kubectl
Follow all the steps (in the link above) until you get this output at the final steps
Init kubeadm with calico network provider and cri-dockerd cri socket
First run
sudo su
then
kubeadm init --pod-network-cidr=192.168.0.0/16 --cri-socket=unix:///var/run/cri-dockerd.sock
After completely initiated kubeadm, then run the followings
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Install calico
Our Kubernetes is going to be ready
Run kubectl get pods -A --watch
to watch our pods status in a realtime
Wait for a minute until all Pod’s STATUS
is Running and the Ready
value is 1/1
. After a minute, let’s check whether our Kubernetes is ready or not by running the following command:
kubectl get pods -A
Try run kubectl get nodes
. If the STATUS
value is Ready, then our Kubernetes is ready to be used.
Deploy simple nodejs “Hello, World!” application to our Kubernetes
Run the following command to deploy simple “Hello, World!” application that runs inside the Kubernetes pod. reinhardjs/node-hello:1.0.0
is the docker image that I pushed to the docker hub.
kubectl create deployment hello-node --image=reinhardjs/node-hello:1.0.0
You can set whatever you want for the pod’s name, in this case, I’m using hello-node
as my pod’s name. Remember, it’s not Kubernetes node, but pod. We are using our VPS, as a single node cluster that will run the Kubernetes cluster (control-plane node) and run all of our pods on top of it. Instead of setting up multiple VPS to implement multi node (expensive), we just use a single Kubernetes cluster (all-in-one).
Then try to run kubectl get pods
. You will get your pod status always (stuck) in pending status. Our application will never be deployed, because of this, try run kubectl describe pods
As the docs said:
By default, your cluster will not schedule Pods
on the control plane nodes for security reasons
https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/#control-plane-node-isolation
So, to be able to use the Kubernetes cluster as our pod’s container, we need to taint the control-plane node
kubectl taint nodes — all node-role.kubernetes.io/control-plane-
After that, our Kubernetes will automatically try to deploy our node “Hello, World!” application
Wait until our application is running on top of created pods
In order to be able to access the pod from outside VPS’s network (remote), we need to expose our hello-node
pod. We are using Kubernetes’s load balancer to expose our pod.
The port 3000 below is the running port of our nodejs hello application
kubectl expose deployment hello-node --type=LoadBalancer --port=3000
Now, run the command below to get the list of running services
kubectl get services
And voila! We can access the simple “Hello, World!” application via our VPS’s public IP with the port of 30472, created by Kubernetes’s load balancers randomly.
If you want to access the pod using internal IP, you can try the following :