Linux

How to Set Up a MicroK8s Kubernetes Cluster on Ubuntu 22.04

How to Set Up a MicroK8s Kubernetes Cluster on Ubuntu 22.04

MicroK8s is a lightweight, efficient, and easy-to-install Kubernetes distribution developed by Canonical. It is ideal for developers, IoT devices, and edge computing applications, offering a simple way to run Kubernetes clusters on local machines. In this guide, we will go through the steps to set up a MicroK8s Kubernetes cluster on Ubuntu 22.04, ensuring that you have a functional and ready-to-use environment.

Prerequisites
Before you begin, ensure that you have the following:

A machine running Ubuntu 22.04 with at least 2 GB of RAM (4 GB recommended).
A user account with sudo privileges.
An active internet connection for downloading packages.
Step 1: Update Your System
Start by updating your system to ensure all existing packages are current. Open your terminal and run:

sudo apt update && sudo apt upgrade -y

This command refreshes your package index and upgrades installed packages.

Step 2: Install MicroK8s
MicroK8s can be installed using the snap package manager, which comes pre-installed on Ubuntu. To install MicroK8s, run the following command:

sudo snap install microk8s –classic

The –classic flag allows MicroK8s to access the necessary system resources. The installation process will take a few moments, during which it will download and set up MicroK8s.

Step 3: Add Your User to the MicroK8s Group
By default, MicroK8s commands require root privileges. To avoid using sudo every time you run a MicroK8s command, add your user to the MicroK8s group:

 

sudo usermod -a -G microk8s $USER

After running this command, you need to log out and back in for the group changes to take effect. Alternatively, you can run:

newgrp microk8s

This command will switch your active group to microk8s without logging out.

Step 4: Verify the Installation
To verify that MicroK8s is installed and running correctly, you can check the status:

microk8s status –wait-ready

This command checks the status of the MicroK8s services and ensures they are running correctly. If everything is working fine, you will see a message confirming that the services are up and ready.

Step 5: Enable Core Add-ons
MicroK8s comes with a variety of add-ons that can be enabled to extend its functionality. Some essential add-ons include the Kubernetes dashboard, DNS, and metrics server. To enable these add-ons, run the following commands:

microk8s enable dns
microk8s enable dashboard
microk8s enable metrics-server

These commands will set up the necessary components for a fully functional Kubernetes environment. The DNS add-on is crucial for service discovery within the cluster, while the dashboard provides a web-based interface for managing your Kubernetes resources.

Step 6: Accessing the Kubernetes Dashboard
Once the dashboard add-on is enabled, you can access it using the following command to get the access token:

microk8s dashboard-proxy

This command will provide you with a URL to access the dashboard, typically http://127.0.0.1:10443. You’ll also need the access token that is displayed in the terminal to log in.

Open your web browser and navigate to the provided URL. When prompted for a token, paste the access token from your terminal.

Step 7: Deploying Your First Application
With MicroK8s set up, you can now deploy your first application. For this example, we will deploy a simple Nginx server. Create a deployment using the following command:

microk8s kubectl create deployment nginx –image=nginx

After the deployment is created, you can expose it using a service:

microk8s kubectl expose deployment nginx –type=NodePort –port=80

This command creates a service that exposes the Nginx deployment on port 80. To verify the deployment and service, use:

microk8s kubectl get deployments
microk8s kubectl get services

You should see the Nginx deployment and service listed.

Step 8: Accessing Your Application
To access your deployed Nginx application, you need to retrieve the NodePort assigned to the service. Run the following command:

microk8s kubectl get services

Look for the nginx service, and note the port listed under the PORT(S) column. You can access your Nginx server using the IP address of your machine and the NodePort you found. For example:

http://:

Replace with the actual IP address of your machine and with the port number.

Step 9: Managing Your MicroK8s Cluster
You can manage your MicroK8s cluster using various commands. For example, to view the current status of your cluster, run:

microk8s kubectl get nodes

To scale your Nginx deployment, you can use:

microk8s kubectl scale deployment nginx –replicas=3

This command will scale your deployment to three replicas, allowing you to handle more traffic.

Step 10: Stopping and Starting MicroK8s
If you need to stop the MicroK8s cluster, use:

microk8s stop

To start it again, simply run:

microk8s start

This feature is useful for conserving resources when you are not using the cluster.

Thank you for reading our article. If you would like to read our articles about Linux systems and kind,eksctl you can take a look at the link below 🙂

How to Install and Use Kind on Linux

How to Install eksctl CLI Tool on Ubuntu Linux

If you want to improve yourself in servers, you can try it by purchasing a suitable and reliable server from our site. I wish you good luck 🙂

America Location Virtual Dedicated Server

Conclusion
By following the steps outlined above, you can set up a fully functional MicroK8s Kubernetes cluster on Ubuntu 22.04. This lightweight solution is perfect for development, testing, and learning purposes, enabling you to leverage Kubernetes capabilities on your local machine. For more information and advanced configurations, you can refer to the MicroK8s documentation.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button