Getting Started with Local Kubernetes: Developing Microservices with MicroK8s
Summary
A guide to setting up a local Kubernetes environment using MicroK8s for developing and testing microservices, with the ability to transition to cloud deployment using Terraform. It covers the installation and configuration of MicroK8s, enabling essential Kubernetes features, setting up `kubectl`, and using Terraform to deploy resources locally.
Overview
In the world of modern software development, microservices architecture has become increasingly popular for its scalability and maintainability. As developers, we often need to run and test these microservices locally before deploying them to a cloud-based Kubernetes cluster. Let’s get started setting up a local Kubernetes environment using MicroK8s, a lightweight, single-package Kubernetes distribution, perfect for local development. We’ll also cover a basic setup with Terraform to help you seamlessly transition to cloud deployment.
Why MicroK8s?
MicroK8s is an easy-to-install, low-ops Kubernetes distribution that runs on your local machine. It provides a great way to develop and test microservices locally before pushing them to a production environment. Some benefits of using MicroK8s include:
- Simplicity: Installation is straightforward, and you can have a fully functional Kubernetes cluster running in minutes.
- Lightweight: It consumes fewer resources than full-blown Kubernetes setups, making it ideal for local development.
- Modular: You can enable and disable Kubernetes features like DNS, storage, and more with simple commands.
- Consistency: The setup mimics a cloud-based Kubernetes environment, ensuring that your local testing closely resembles the production environment.
Step 1: Installing MicroK8s
To get started, you’ll need to install MicroK8s on your development machine. Follow these steps:
- Install MicroK8s:
MicroK8s can be installed using a snap package, which is supported on most Linux distributions.
sudo snap install microk8s --classic
This command will install MicroK8s with all necessary dependencies. If you’re not developing on a Linux machine, you can find instructions for the installation in on the official site.
- Add Your User to the MicroK8s Group:
To avoid needingsudo
for every command, add your user to themicrok8s
group.
sudo usermod -aG microk8s $USER
sudo chown -f -R $USER ~/.kube
Log out and log back in to apply the changes.
- Enable Essential Add-ons:
MicroK8s comes with several add-ons that are disabled by default. Enable the ones you need with the following commands:
microk8s enable dns dashboard storage
dns
: Provides DNS resolution within your cluster.dashboard
: A web-based UI for Kubernetes.storage
: A local storage solution for persistent volumes.
- Verify Your Installation:
Check the status of your MicroK8s installation to ensure everything is running smoothly.
microk8s status --wait-ready
You should see that the Kubernetes services are active and ready.
Step 2: Configuring kubectl for MicroK8s
To interact with your Kubernetes cluster, you’ll use kubectl
. MicroK8s comes with its own version of kubectl
, but you can alias it to avoid conflicts with other Kubernetes installations.
alias kubectl='microk8s kubectl'
This alias ensures that all kubectl
commands target your MicroK8s cluster.
Step 3: Setting Up Terraform for Local Deployment
Terraform is a powerful tool for provisioning infrastructure as code. We can use it to deploy resources to your local MicroK8s cluster and later transition to a cloud provider like AWS, GCP, or Azure.
- Install Terraform:
If you haven’t already, download and install Terraform from the official website. - Create a Terraform Configuration:
Start by defining a basic Terraform configuration that will create Kubernetes resources locally. Here’s an examplemain.tf
:
provider "kubernetes" {
config_path = "~/.kube/config"
}
resource "kubernetes_namespace" "example" {
metadata {
name = "example-namespace"
}
}
resource "kubernetes_deployment" "nginx" {
metadata {
name = "nginx-deployment"
namespace = kubernetes_namespace.example.metadata[0].name
}
spec {
replicas = 2
selector {
match_labels = {
app = "nginx"
}
}
template {
metadata {
labels = {
app = "nginx"
}
}
spec {
container {
image = "nginx:latest"
name = "nginx"
}
}
}
}
}
This configuration defines a Kubernetes namespace and a simple Nginx deployment within that namespace.
- Initialize and Apply Terraform:
Initialize Terraform to download the necessary providers and then apply the configuration.
terraform init
terraform apply
Terraform will deploy the resources to your local MicroK8s cluster.
Step 4: Transitioning to Cloud Deployment
Once you’ve developed and tested your microservices locally, you can easily transition to deploying them on a cloud-based Kubernetes cluster. The main changes involve updating your Terraform provider configuration to point to your cloud provider’s Kubernetes service and ensuring your cloud infrastructure is set up accordingly.
For example, if you were to deploy to Google Kubernetes Engine (GKE), you would modify the provider block in your Terraform configuration:
provider "kubernetes" {
host = "<GKE API Server Endpoint>"
token = "<Your GCP Access Token>"
cluster_ca_certificate = file("<Path to CA Certificate>")
}
This allows you to use the same Terraform scripts to manage both your local and cloud deployments, providing a smooth and consistent development workflow.
Happy Coding
Running Kubernetes locally with MicroK8s is a great way to develop and test your microservices before deploying them to a production environment. By combining MicroK8s with Terraform, you can maintain a consistent infrastructure-as-code approach across both local and cloud environments, ensuring a seamless transition from development to production.
Just one more note, this type of architecture is not for everyone. If you are creating a single service, or starting a new project, I highly recommend you begin with something simple to deploy such as Google Firebase.