← Back to Course Index

Week 5 IconWeek 5 - Lab 3: Introduction to Kubernetes (Minikube)

Module: Operating Systems 3 (Virtualisation & Cloud Technologies)
Topic: Kubernetes Orchestration & Management
Estimated Time: 60 Minutes
Instructor: KT Nshimba


Lab Overview

In Lab 2, you deployed WordPress using Docker Compose. This was excellent for a single server. However, what if that server fails? What if you need 50 copies of WordPress to handle Black Friday traffic?

Docker Compose cannot handle multi-server scaling or self-healing automatically. Kubernetes (K8s) can.

In this lab, you will "graduate" your application from Docker Compose to Kubernetes. You will use Minikube (a local single-node Kubernetes cluster) to deploy the same WordPress stack, first using the CLI, and then managing it with Portainer.

Objectives:

  1. Understand the shift from Containers (Docker) to Pods (Kubernetes).
  2. Deploy MySQL and WordPress using K8s Manifests (YAML).
  3. Expose applications using K8s Services.
  4. Deploy Portainer into the cluster to manage Kubernetes visually.

Prerequisites: - The Ubuntu Server VM from previous labs. - Minikube is pre-installed on this VM. - Kubectl is pre-installed on this VM.


Part 1: The CLI Approach (Kubectl)

We will manually define our infrastructure using YAML manifests, similar to Docker Compose but with more power components.

Step 1: Start the Cluster

  1. Log in to your Ubuntu VM. Start Minikube:
minikube start --driver=docker

Note: This creates a Kubernetes cluster inside a Docker container! Verify connection:

kubectl get nodes

Field Report:

Step 2: Deploy MySQL (The Database)

In Kubernetes, we don't just run a container; we run a Deployment (which manages the Pods) and a Service (which gives networking). Create the file mysql-k8s.yaml:

apiVersion: v1
kind: Service
metadata:
  name: wordpress-db
spec:
  ports:
  - port: 3306
  selector:
    app: wordpress-db
  clusterIP: None  # Headless service for internal DB communication
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-db
spec:
  selector:
    matchLabels:
      app: wordpress-db
  template:
    metadata:
      labels:
        app: wordpress-db
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: somewordpress
        - name: MYSQL_DATABASE
          value: wordpress
        - name: MYSQL_USER
          value: wordpress
        - name: MYSQL_PASSWORD
          value: wordpress

Apply it:

kubectl apply -f mysql-k8s.yaml

Step 3: Deploy WordPress (The Frontend)

Now the web server. This needs a Service accessible from outside the cluster. Create wordpress-k8s.yaml:

apiVersion: v1
kind: Service
metadata:
  name: wordpress
spec:
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30000  # Expose on port 30000
  selector:
    app: wordpress
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: wordpress:latest
        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-db
        - name: WORDPRESS_DB_USER
          value: wordpress
        - name: WORDPRESS_DB_PASSWORD
          value: wordpress
        - name: WORDPRESS_DB_NAME
          value: wordpress

Apply it:

kubectl apply -f wordpress-k8s.yaml

Step 4: Verify and Access

Check the pods:

kubectl get pods

Wait until Status is Running.

Get the access URL:

minikube service wordpress --url

Field Report:

4. Open that URL in your browser. You should see the WordPress installer!

Step 5: Critical Thinking - Self Healing

Delete the WordPress pod manually:

# Replace pod-name with your actual pod name
kubectl delete pod <wordpress-pod-name>

Immediately check pods again:

kubectl get pods

Observation:

This is the power of a Deployment controller!


Part 2: Managing K8s with Portainer

Just like with Docker, CLI is good, but a Dashboard helps visualize complexity. We will deploy Portainer inside your Kubernetes cluster.

Step 1: Deploy Portainer

Download and apply the Portainer manifest:

kubectl apply -n portainer -f https://raw.githubusercontent.com/portainer/k8s/master/deploy/manifests/portainer/portainer.yaml

Note: If the namespace doesn't exist, create it first: kubectl create namespace portainer. Expose Portainer via NodePort (so we can access it):

kubectl expose deploy portainer -n portainer --type=NodePort --name=portainer-svc --port=9000

Step 2: Access Portainer

Get the URL:

minikube service portainer-svc -n portainer --url
  1. Open the URL in your browser.
  2. Create an admin user. 4. Select Get Started -> Connect to your local Kubernetes environment.

Step 3: Explore Kubernetes UI

  1. Click on your local cluster.
  2. Navigate to Namespaces -> default.
  3. Look at Applications. Do you see wordpress and wordpress-db? 4. Look at Cluster -> Nodes.

Field Report


Lab Checkpoint

Practical Skills Checklist

Theoretical Understanding Checklist

Troubleshooting Scenarios


Lab Reflection

← Back to Course Index