How to Deploy and Manage Microservices with Docker Containers in MicroK8s

Posted by Harish Kumar, Updated on Jan 03,2024

To run four application microservices as Docker containers within MicroK8s (a lightweight Kubernetes distribution), you'll need to follow several steps. Here's a basic guide to help you get started:


Step 1: Install MicroK8s

Ensure MicroK8s is installed on your machine. You can follow the official documentation for installation instructions: MicroK8s Installation


Step 2: Enable Necessary Add-ons

MicroK8s provides several add-ons that you may need for your setup. Enable the required ones by running:


bash microk8s enable dns dashboard ingress
Step 3: Build Docker Images

Build Docker images for your four microservices. Each microservice should have its Dockerfile. Move to the directory where your Dockerfile is located and run:


bash docker build -t .

Repeat this step for each microservice, giving each one a unique .


Step 4: Push Docker Images to a Container Registry (Optional)

If your MicroK8s cluster is not running on the same machine where you built the images, consider pushing them to a container registry [Either Docker Hub or Azure registry]. Popular choices include Docker Hub, Google Container Registry, or others.


bash docker login docker tag / docker push /
Step 5: Create Kubernetes Deployment YAML Files

For each microservice, create a Kubernetes Deployment YAML file. Below is an example for one microservice (example-deployment.yaml). Repeat this for each microservice, modifying the file accordingly.


yaml apiVersion: apps/v1 kind: Deployment metadata: name: microservice-1 spec: replicas: 1 selector: matchLabels: app: microservice-1 template: metadata: labels: app: microservice-1 spec: containers: - name: microservice-1 image: / ports: - containerPort: 80

Replace and with your container registry and image names.


Step 6: Apply Deployments

Apply the Deployment YAML files to your MicroK8s cluster:


bash microk8s kubectl apply -f example-deployment.yaml

Repeat this step for each microservice.


Step 7: Expose Microservices

Expose your microservices if needed. For example, if you want to expose them via Ingress, create an Ingress YAML file:


yaml apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: microservices-ingress spec: rules: - host: your-domain.com http: paths: - path: /microservice-1 pathType: Prefix backend: service: name: microservice-1 port: number: 80

Apply the Ingress:


bash microk8s kubectl apply -f ingress.yaml

Get a 30-minute, no-cost strategy session with a Cloud Services expert