In this tutorial I show you how to deploy an application to MicroK8s. I will show you how to deploy a simple application to MicroK8s and access it from not only your local machine but make it accessible from the internet.
What is Microk8s?
MicroK8s is a lightweight, fast, and simple Kubernetes distribution that is perfect for development, prototyping, and testing. It is a single package that installs a Kubernetes cluster on your machine. This is a great way to get started with Kubernetes without needing to setup a full cluster.
Pre-requisites
Make sure you have MicroK8s installed on your machine. If you don’t have it installed, you can follow my tutorial on How to setup MicroK8s.
Step by Step Guide
1. Dockerize your application
This process is going to look a lot different depending on your application but if you’re here looking at MicroK8s you likely already know a fair amount about Docker. If you just want of follow along you can always just go grab a HelloWorld image from Docker Hub docker pull hello-world.
2. Deploy your application to MicroK8s
My favorite way to do this is with a pipeline. It’s more complicated but likely where you want to go but for now let’s just use kompose to generate our manifest files.
2.1: Ensure kompose is installed
Install kompose if not already installed
I’m on Mac using brew of course. Go check out Supercharge Your Mac with Brew if you haven’t already setup brew. Otherwise, head over to the installation instructions for your OS at the Offical Kompose Installation Docs.
1
brew install kompose
2.2: Convert Docker Compose file to Kubernetes manifests
Assuming you have a docker-compose.yml file in your current directory
1
kompose convert
2.3: Apply the generated manifests to the MicroK8s cluster
1
kubectl apply -f .
Note: The above command assumes that the generated manifests are in the current directory. If you specified a different output path then make sure to specify that in your apply command.
3. Expose your application to the internet with an ingress controller
This is where the rubber meets the road. Buckle up.
3.1 Install the ingress controller
See How to setup an NGINX Ingress Controller on Kubernetes. Once you’ve done that come back to this article.
3.2 Deploy your application with an ingress resource
1
2
3
4
5
6
7
8
9
10
11
12
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
namespace: default
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: myapp.com
http:
paths:
3.3 Configure your DNS
Go to your DNS provider and create an A record pointing to your public IP address of your firewall.
3.4 Configure your firewall NAT rules
Make sure you have a NAT rule in your firewall that forwards traffic from the public IP address to the private IP address of your MicroK8s cluster. In my case I’m forwarding port 80 and 443 to the private IP address of my MicroK8s cluster.
4. Create and apply Ingress manifests
1
kubectl apply -f my-ingress.yaml
5. Access your application from the internet
At this point you should be able to access your application from the internet by going to http://myapp.com.
Conclusion
Congratulations! 🎉 You’ve just deployed your application to MicroK8s, exposed it to the internet, and probably earned yourself a well-deserved coffee break. ☕️
If your application is running smoothly, give yourself a pat on the back. If it’s not, well, there’s always the classic “turn it off and on again” trick. Or, you know, you could just blame it on the network. That always works.
Remember, Kubernetes is like a cat: it does what it wants, when it wants. But with a bit of patience and a lot of YAML, you can tame it. Happy deploying! 🚀