Anto Subash.

MicroK8s - Getting Started -Part : 1

Setup MicroK8s With Ubuntu

Table of contents

Introduction

In this post we will see how to create a single node kubernetes cluster using microk8s. We will use hetzner cloud to create the server and install ubuntu server on it. We will then install microk8s and connect to the cluster using kubectl.

Parts

Part 1. Setup MicroK8s With Ubuntu (this post)

Part 2. Setup Nginx and cert-manager in MicroK8s

Part 3. Deploy docker registry and postgres database in MicroK8s

Part 4. Create and deploy .Net application in MicroK8s

Creating the Ubuntu Server

Prerequisites

  • Hetzner Account

I'm going to use hetzner cloud to create this machine if you don't have one then you can use the referral link https://hetzner.cloud/?ref=ENhA4rCZ5JUM and get 20€ for free to try it out.

Creating a project

Once you have the hetzner account. login and create a project.

Create Project

Setup SSH

The first step in the project is to SSH for the project.

SSH key

Setup Firewall

Now lets setup the firewall for our server.

Firewall

Create server with firewall and ssh key

Create server

Add DNS entry

Now you have to add a DNS entry for your newly created machine. you can fine the the IP address of the machine in the Hetzner server page.

We need to add A record and a CNAME record.

1A XX.XX.XX.XX yourdomain.com
2CNAME * yourdomain.com

For example this is how my sample entry looks like

Record Host Value A youtube1 95.217.191.119 CNAME * kdev1.antosubash.com

This might take some time to reflect so wait for a while to verify it.

Verify DNS entry

To verify dns entry we will use a tool called dig.

you can find it here https://toolbox.googleapps.com/apps/dig/#A/

Make sure your domain is pointing to your ip and your subdomain is pointing to your domain

yourdomain.com -> XX.XX.XX.XX subdomain.yourdomain.com -> XX.XX.XX.XX

So both your domain and subdomain should point to the same IP Which is the IP of the machine we just created.

Update ssh config

ssh config file can be "~/.ssh/config"

In the config file add a entry for the newly created server.

1Host kdev1.yourdomain.com
2   IdentitiesOnly yes
3   IdentityFile ~/.ssh/yournewlycreatedkey # Not the pub file

Update the server

Run the following command to update the server

1sudo apt update
2sudo apt upgrade

Install Snapd daemon

We will use the snap to microk8s. so we will install the snapd first.

1sudo apt install snapd

once the it is installed verify it by checking the version

1snap version

Install MicroK8s

The current version of the kubernetes is 1.25 so we are using the following command. To verify the current version take a look https://microk8s.io/docs/getting-started

1sudo snap install microk8s --classic --channel=1.25

Join the group

1sudo usermod -a -G microk8s $USER
2
3sudo chown -f -R $USER ~/.kube
4
5su - $USER

Check the status

1microk8s status --wait-ready

Access Kubernetes

1microk8s kubectl get nodes

Create alias

1alias kubectl='microk8s kubectl'

View Cluster Info

1kubectl cluster-info

View Kube config

1microk8s config

This config which is displayed here is the one we will use to connect to the cluster. copy it and configure your kubectl in your machine.

If you want to know how to install kubectl look here https://kubernetes.io/docs/tasks/tools/

Enable addons

1microk8s enable dns ingress prometheus cert-manager hostpath-storage

Verify addons

1microk8s kubectl get all --all-namespaces

Create a setup script

you can create a setup script to automate the setup process. I have created a setup script which you can find below. this script will install the microk8s and enable the addons.

let's create a file called setup.sh and add the following content.

1sudo nano setup.sh

lets set the permissions for the script

1sudo chmod +x setup.sh

Now we have the file and the permissions. lets add the following content to the file.

1#!/bin/bash
2sudo apt update
3sudo apt upgrade
4sudo apt install snapd -y
5snap version
6sudo snap install microk8s --classic --channel=1.25
7sudo usermod -a -G microk8s $USER
8sudo chown -f -R $USER ~/.kube
9su - $USER
10microk8s status --wait-ready
11alias kubectl='microk8s kubectl'
12kubectl cluster-info
13microk8s enable dns ingress prometheus cert-manager hostpath-storage
14microk8s kubectl get all --all-namespaces
15microk8s kubectl get nodes

Run the setup script

1sudo bash setup.sh

this will take some time to complete. once it is completed you can verify the status of the cluster by running the following command.

1microk8s status

Access the cluster

To access the cluster we will use the kubectl command. you can find the installation guide for kubectl here

Once you have the kubectl installed you can run the following command to access the cluster.

1microk8s config > ~/.kube/config

you can copy the content of the config file and paste it in your ~/.kube/config file.

Using Lens to access the cluster

Lens is a tool to access the kubernetes cluster. you can find the installation guide here. once you have installed the lens you can add the cluster by clicking the + button. and paste the config file in the kubeconfig field and click the connect button.

Prepare the terminal with oh-my-zsh

I am using the oh-my-zsh to prepare my terminal. you can find the installation guide here

Install oh-my-zsh

1sudo apt install zsh
2sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Install autosuggestions and syntax highlighting

1git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
2git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

Update the .zshrc file

1nano ~/.zshrc

add the following lines to the file

1plugins=(git kubectl zsh-autosuggestions zsh-syntax-highlighting)

Also update the theme to agnoster

1ZSH_THEME="agnoster"

add the kubectl alias to the file

1alias kubectl='microk8s kubectl'

Update the terminal

1source ~/.zshrc

Conclusion

This is it for this post. We have created a server and installed microk8s and enabled few addons. we have also configured the kubectl to access the cluster. Now we are ready for the next step. In the next step we will see how to setup nginx and cert manager. so that we can deploy our first app.

Part 2. Setup Nginx and cert-manager in MicroK8s

Buy Me a Coffee at ko-fi.com