Kubernetes Deployment Practical: Hands-On Tutorial with YAML & kubectl Commands

Practical Kubernetes Deployment tutorial with step-by-step examples, YAML configuration, kubectl commands, and self-healing demonstrations.

Deployment Example

Download the YAML configuration file for the Deployment example

Download deploy-example.yaml

Create the Deployment

kubectl apply -f deploy-example.yaml

Applies the configuration from the YAML file to create the Deployment.

Get the pods list

kubectl get pods -o wide

Lists all pods with additional details including the node they're running on.

Describe the pod

kubectl describe pod deploy-example

Shows detailed information about a specific pod created by the Deployment.

Get the Deployment info

kubectl get deploy
kubectl describe deploy deploy-example

Lists Deployments and shows detailed information about a specific Deployment.

Get the ReplicaSet name

kubectl get rs

Lists ReplicaSets created by the Deployment to manage the pods.

Describe the ReplicaSet

kubectl describe rs

Shows detailed information about the ReplicaSet managing the pods.

Cleanup

kubectl delete -f deploy-example.yaml

Deletes all resources defined in the YAML configuration file.

YAML Configuration File

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-example
spec:
  replicas: 3
  revisionHistoryLimit: 3
  selector:
    matchLabels:
      app: nginx
      env: prod
  template:
    metadata:
      labels:
        app: nginx
        env: prod
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi        
        ports:
        - containerPort: 80

YAML Configuration Explanation:

Deployment Structure:

  • apiVersion: apps/v1 → Specifies the Kubernetes API version for Deployments
  • kind: Deployment → Defines this as a Deployment resource
  • metadata.name: deploy-example → Names the Deployment "deploy-example"

Deployment Specifications:

  • replicas: 3 → Maintains 3 identical pod instances
  • revisionHistoryLimit: 3 → Keeps 3 old ReplicaSets for rollback capability
  • selector.matchLabels → Defines how the Deployment finds which Pods to manage

Pod Template:

  • template.metadata.labels → Labels applied to created Pods
  • template.spec → Pod specification for the containers to run

Container Specifications:

  • name: nginx → Names the container "nginx"
  • image: nginx:alpine → Uses the lightweight Alpine-based Nginx image
  • resources → Defines CPU and memory requests/limits
  • containerPort: 80 → Exposes port 80 for web traffic

How It Works:

The Deployment creates a ReplicaSet which then creates and manages 3 identical Pods. If a Pod fails, the ReplicaSet automatically replaces it to maintain the desired number of replicas. The Deployment provides declarative updates, rollback capabilities, and scaling features.