Setup a Kubernetes cluster in under 2 minutes using microk8s

Jayesh Kulkarni
3 min readJan 31, 2020

--

This is the first part of a series “Step by step guide on deploying python app to Kubernetes cluster using Microk8s”. In this part we will setup a single node kubernetes cluster using microk8s.

Access the overview of complete series here:

I am using AWS to launch ec2 instances required for setting up k8s cluster. Microk8s is not production ready as of yet but can be used to test a fully functional k8s cluster.

There are other alternatives to microk8s such as minikube, however I prefer microk8s as it is very lightweight and fast. For production deployments, managed kubernetes services such as AWS EKS, GCP GKE or on premises kubernetes deployment can be used.

Create a Kubernetes cluster using microk8s

Step 1: Launch a linux EC2 instance using Ubuntu ami.

You can use any linux distribution — supported linux distros are specified on microk8s docs. I am using an ec2 of type t2.large with 20 GB disk for this setup.

Step 2: Access EC2 instance using ssh and update package cache using “sudo apt update”

Step 3: Install and setup microk8s

Installing microk8s can be done through snap package manager. Run below command to install microk8s.

sudo snap install microk8s --classic --channel=1.16/stable

Run below additional commands to add your user to microk8s group so as to get required privileges.

sudo usermod -a -G microk8s $USER

You’ll need to reenter into ssh session to make this change take into effect. Microk8s has a command “microk8s.status”, this can be used to check status of all microk8s services.

Microk8s packages kubernetes features as its add-ons and by default only enables k8s services : api-server, controller manager, scheduler, kubelet, kube-proxy, cni. These services are currently sufficient for us to deploy a simple application and to expose it using node-port service. I will be creating a separate article on exposing service using ingress-controller.

microk8s.status

Output will show you that kubernetes services are enabled. This means that our single node k8s cluster is up and running.

Microk8s comes with in-built kubectl and it can be accessed using “microk8s.kubectl”.

Let’s quickly check the running pods using this kubectl.

microk8s.kubectl get pods

Instead of running microk8s.kubectl each time we want to run a kubectl command, we can add an alias to our .bashrc file like below. .bashrc file is located in your home directory.

alias kubectl=’microk8s.kubectl’

add above line at the end of .bashrc file and save the file. You need to re-enter the ssh session for that to take effect or you can run below command to make it effective immediately.

source ~/.bashrc

After adding this alias you can directly use kubectl to send commands to k8s api server.

Now, one of the important step is to enable the add-ons required. It is very important to enable kube-dns service as it is required for service discovery in k8s cluster. Run below command to enable kube-dns.

microk8s.enable dns

We are good till now. Now that we have a k8s cluster running, we can now focus on deploying our simple python application.

Here’s the link for 2nd part of this article. In second part we will build docker image of our python app and will push the image to Docker hub.

--

--

Jayesh Kulkarni
Jayesh Kulkarni

Written by Jayesh Kulkarni

DevOps Engineer | Developer | A geek and a writer!

No responses yet