Skip to content

codezelaca/devops-start-kubernetes

Repository files navigation

Kubernetes Workshop: Minikube + kubectl Hands-On

This repository contains hands-on practicals for learning Kubernetes locally with Minikube.

Workshop Overview

  • Session 1: Environment Setup and Basic Concepts (30 minutes)
  • Session 2: Deploy Your First Application (45 minutes)
  • Session 3: Scaling and Updates (30 minutes)
  • Session 4: Troubleshooting and Best Practices (15 minutes)

Files in This Practical

  • nginx-pod.yaml
  • nginx-deployment.yaml
  • nginx-service.yaml
  • mongodb-deployment.yaml
  • mongodb-service.yaml

Prerequisites

  • Windows machine
  • PowerShell or Command Prompt (run as Administrator for setup tasks)
  • Minikube installed
  • Docker Desktop running
  • VS Code installed

Session 1: Environment Setup and Basic Concepts (30 minutes)

Step 1: Start Minikube Cluster

1.1 Verify Minikube Installation

minikube version

Expected output (example):

minikube version: v1.36.0
commit: f8f52f5de11fc6ad8244afac475e1d0f96841df1-dirty

1.2 Start Minikube with Docker Driver

minikube start --driver=docker

This command downloads Kubernetes images and starts the local cluster. The first run may take 3-5 minutes.

1.3 Verify Cluster Status

minikube status

Expected output (example):

minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

Step 2: Set Up VS Code for Kubernetes

2.1 Open VS Code

Launch Visual Studio Code.

2.2 Install Kubernetes Extension (Optional but Recommended)

  1. Open Extensions (Ctrl+Shift+X)
  2. Search for Kubernetes by Microsoft
  3. Click Install

2.3 Create Project Directory

If starting from scratch in terminal:

mkdir k8s-workshop
cd k8s-workshop

Note: For this repository, you are already inside the workshop folder.

Step 3: Verify kubectl Installation

kubectl version --client
kubectl cluster-info

Session 2: Deploy Your First Application (45 minutes)

Project Goal: Deploy a simple NGINX web server.

Step 1: Create a Simple Pod

1.1 YAML (nginx-pod.yaml)

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
    - name: nginx
      image: nginx:latest
      ports:
        - containerPort: 80

YAML meaning:

  • apiVersion: Kubernetes API version
  • kind: Resource type (Pod)
  • metadata: Name and labels
  • spec: Pod container configuration

1.2 Deploy Pod

kubectl apply -f nginx-pod.yaml

1.3 Verify Pod

kubectl get pods
kubectl describe pod nginx-pod
kubectl logs nginx-pod

Step 2: Create a Deployment

Deployments manage replicas and rolling updates.

2.1 YAML (nginx-deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.25
          ports:
            - containerPort: 80

Key differences vs Pod:

  • replicas: 3 creates 3 identical pods
  • selector decides which pods are managed
  • template defines pod blueprint

2.2 Delete Previous Pod

kubectl delete pod nginx-pod

2.3 Deploy Deployment

kubectl apply -f nginx-deployment.yaml

2.4 Monitor Deployment

kubectl get deployments
kubectl get pods
kubectl get pods -w

Step 3: Expose Application with a Service

Services provide stable network access to pods.

3.1 YAML (nginx-service.yaml)

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30080

Service types:

  • ClusterIP: Internal only (default)
  • NodePort: Exposes on node IP + static port
  • LoadBalancer: Cloud external load balancer

3.2 Deploy Service

kubectl apply -f nginx-service.yaml

3.3 Verify Service

kubectl get services

3.4 Access Application

minikube service nginx-service --url
minikube service nginx-service

Session 3: Scaling and Updates (30 minutes)

Step 1: Scale the Deployment

1.1 Scale Up

kubectl scale deployment nginx-deployment --replicas=5
kubectl get pods -w

1.2 Scale Down

kubectl scale deployment nginx-deployment --replicas=2

Step 2: Rolling Update and Rollback

2.1 Update Image Version

kubectl set image deployment/nginx-deployment nginx=nginx:1.26
kubectl rollout status deployment/nginx-deployment

2.2 View Rollout History

kubectl rollout history deployment/nginx-deployment

2.3 Rollback

kubectl rollout undo deployment/nginx-deployment
kubectl rollout undo deployment/nginx-deployment --to-revision=1

Step 3: Deploy a Multi-Container Style Application (Web + Database)

In this practical, you add MongoDB as a second application component.

3.1 MongoDB Deployment (mongodb-deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
        - name: mongodb
          image: mongo:7.0
          ports:
            - containerPort: 27017
          env:
            - name: MONGO_INITDB_ROOT_USERNAME
              value: admin
            - name: MONGO_INITDB_ROOT_PASSWORD
              value: password123

3.2 MongoDB Service (mongodb-service.yaml)

apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
spec:
  type: ClusterIP
  selector:
    app: mongodb
  ports:
    - protocol: TCP
      port: 27017
      targetPort: 27017

3.3 Deploy MongoDB

kubectl apply -f mongodb-deployment.yaml
kubectl apply -f mongodb-service.yaml
kubectl get all

Session 4: Troubleshooting and Best Practices (15 minutes)

Essential Troubleshooting Commands

Command Purpose
kubectl get pods List all pods
kubectl describe pod <name> Detailed pod info
kubectl logs <pod-name> View container logs
kubectl exec -it <pod> -- /bin/bash Open shell in pod
kubectl delete pod <name> Delete a pod
kubectl get events View cluster events
kubectl top pods Resource usage (needs Metrics Server)

Common Pod States

Status Meaning Action
Pending Waiting for resources Check kubectl describe
ImagePullBackOff Image pull failed Verify image name and network
CrashLoopBackOff Container keeps crashing Check kubectl logs <pod>
Running Healthy No action needed

Best Practices

  • Use Deployments instead of standalone Pods in production
  • Label resources consistently
  • Add CPU and memory resource requests/limits
  • Use liveness and readiness probes
  • Keep YAML files versioned in Git
  • Use namespaces for environment separation
  • Do not hardcode secrets in YAML; use Kubernetes Secrets/ConfigMaps

Clean Up and Next Steps

Clean Up Resources

kubectl delete deployment --all
kubectl delete service --all
minikube stop
minikube delete

What You Learned

  • Set up local Kubernetes with Minikube
  • Created and deployed Pods, Deployments, and Services
  • Scaled workloads up and down
  • Performed rolling updates and rollbacks
  • Used kubectl for troubleshooting
  • Deployed an app component with MongoDB

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors