Skip to main content

Lab 1.1 - First Deployment on Kubernetes

Lab Objectives

By the end of this lab, you will be able to:

  • Deploy your first application on Kubernetes
  • Use kubectl to create resources
  • Understand Pods and Deployments
  • Verify the state of your deployment
  • Access your application

Estimated Duration

30-45 minutes

Prerequisites

  • kubectl installed and configured
  • Functional local Kubernetes cluster (minikube or kind)
  • Internet connection (to download Docker images)

Environment Verification

Before starting, let's verify that everything is ready:

# 1. Verify kubectl
kubectl version --client

# 2. Verify cluster connection
kubectl cluster-info

# 3. List nodes (should show at least 1 node)
kubectl get nodes

Expected result:

NAME           STATUS   ROLES           AGE   VERSION
minikube Ready control-plane 5m v1.28.0

If you see an error, return to Chapter 1 to configure your environment.


Part 1: Simple Deployment with kubectl

Step 1: Create a Simple Pod

Let's start by creating a simple Pod that runs nginx:

kubectl run nginx-pod --image=nginx:latest

Explanation:

  • kubectl run: Command to create a Pod
  • nginx-pod: Name of the Pod
  • --image=nginx:latest: Docker image to use

Step 2: Verify the Pod

Let's verify that the Pod was created:

kubectl get pods

Expected result:

NAME        READY   STATUS    RESTARTS   AGE
nginx-pod 1/1 Running 0 30s

Column explanation:

  • NAME: Name of the Pod
  • READY: Number of ready containers / total (1/1 = ready)
  • STATUS: Pod state (Running = executing)
  • RESTARTS: Number of restarts
  • AGE: Time since creation

Step 3: Get More Details

To see more information about the Pod:

kubectl describe pod nginx-pod

This command displays:

  • Pod events
  • Container state
  • Allocated resources
  • Labels and annotations

Step 4: View Logs

Check the Pod logs:

kubectl logs nginx-pod

You should see the nginx server logs.

Step 5: Execute a Command in the Pod

Let's run a command inside the Pod:

kubectl exec -it nginx-pod -- /bin/bash

Explanation:

  • exec: Execute a command in a container
  • -it: Interactive mode with terminal
  • -- /bin/bash: Command to execute

Once inside the Pod, try:

# View directory contents
ls -la

# Verify that nginx is running
curl localhost

# Exit the Pod
exit

Step 6: Delete the Pod

Let's delete the Pod we just created:

kubectl delete pod nginx-pod

Verify that it was deleted:

kubectl get pods

Part 2: Deployment with a YAML File

Now, let's create a more professional deployment using a YAML file.

Step 1: Create the YAML File

Create a file nginx-deployment.yaml:

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

File explanation:

  • apiVersion: Kubernetes API version
  • kind: Resource type (Deployment)
  • metadata: Metadata (name, labels)
  • spec: Deployment specification
    • replicas: Number of instances (3)
    • selector: How to identify Pods
    • template: Template for creating Pods

Step 2: Apply the Deployment

Apply the YAML file:

kubectl apply -f nginx-deployment.yaml

Expected result:

deployment.apps/nginx-deployment created

Step 3: Verify the Deployment

Verify that the deployment was created:

kubectl get deployments

Expected result:

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment 3/3 3 3 30s

Explanation:

  • READY: 3/3 = 3 Pods ready out of 3 desired
  • UP-TO-DATE: 3 up-to-date Pods
  • AVAILABLE: 3 available Pods

Step 4: See the Created Pods

The Deployment created 3 Pods:

kubectl get pods -l app=nginx

Expected result:

NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-7d4b8c9f5c-abc12 1/1 Running 0 1m
nginx-deployment-7d4b8c9f5c-def34 1/1 Running 0 1m
nginx-deployment-7d4b8c9f5c-ghi56 1/1 Running 0 1m

Note: Pod names are automatically generated with a unique hash.

Step 5: Deployment Details

Get more information about the deployment:

kubectl describe deployment nginx-deployment

This command displays:

  • Number of replicas
  • Deployment strategy
  • Events
  • Conditions

Part 3: Exposing the Application

Now, let's expose the application to access it from outside.

Step 1: Create a Service

Create a file 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

Explanation:

  • kind: Service: Creates a Service
  • type: NodePort: Exposes the service on a port of each node
  • selector: Selects Pods with the label app: nginx
  • nodePort: 30080: Port accessible from outside

Step 2: Apply the Service

kubectl apply -f nginx-service.yaml

Step 3: Verify the Service

kubectl get services

Expected result:

NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
nginx-service NodePort 10.96.123.45 <none> 80:30080/TCP 30s

Step 4: Access the Application

With minikube:

# Get the service URL
minikube service nginx-service --url

# Or open directly in the browser
minikube service nginx-service

With kind:

# Find the node IP address
kubectl get nodes -o wide

# Access via http://<NODE-IP>:30080

Test with curl:

# From your machine
curl http://localhost:30080

# Or from a Pod
kubectl run curl-test --image=curlimages/curl --rm -it -- curl http://nginx-service

You should see the default nginx welcome page!


Part 4: Scaling and Updating

Step 1: Increase the Number of Replicas

Let's increase the number of Pods from 3 to 5:

kubectl scale deployment nginx-deployment --replicas=5

Verify:

kubectl get pods -l app=nginx

You should now see 5 Pods!

Step 2: Reduce the Number of Replicas

Let's reduce to 2 Pods:

kubectl scale deployment nginx-deployment --replicas=2

Step 3: Update the Image

Let's update the nginx image to a more recent version:

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

Verify the update:

kubectl rollout status deployment/nginx-deployment

This command shows the rolling update progress.

Step 4: View Deployment History

kubectl rollout history deployment/nginx-deployment

Step 5: Rollback (Optional)

If something goes wrong, we can roll back:

kubectl rollout undo deployment/nginx-deployment

Part 5: Cleanup

Let's clean up the created resources:

# Delete the Service
kubectl delete -f nginx-service.yaml

# Delete the Deployment (also deletes the Pods)
kubectl delete -f nginx-deployment.yaml

# Verify everything is deleted
kubectl get all

Lab Summary

In this lab, you:

Created your first Pod with kubectl
Created a Deployment with a YAML file
Exposed the application with a NodePort Service
Scaled the deployment (increased/reduced the number of replicas)
Updated the Docker image
Cleaned up the resources


Reflection Questions

  1. What is the difference between a Pod and a Deployment?

    • A Pod is a single instance of a container
    • A Deployment manages multiple Pods (replicas) and their lifecycle
  2. Why use a Deployment rather than a Pod directly?

    • The Deployment automatically manages replicas
    • It enables rolling updates
    • It automatically restarts Pods that crash
  3. What is a Service and why do we need one?

    • A Service exposes a set of Pods in a stable manner
    • It provides load balancing
    • It allows access from outside the cluster

Next Steps

Now that you have deployed your first application, you are ready for:

Chapter 2: In-depth exploration of Pods
Module 2: Detailed Kubernetes architecture


Resources


Lab created: December 2024