DEV Community

janak0ff
janak0ff

Posted on

Day 49: Deploy Applications with Kubernetes Deployments

The Nautilus DevOps team is delving into Kubernetes for app management. One team member needs to create a deployment following these details:
Create a deployment named httpd to deploy the application httpd using the image httpd:latest (ensure to specify the tag)


📋 Create a Kubernetes Deployment

Challenge Overview

  • Deployment Name: httpd
  • Image: httpd:latest
  • Container Port: 80 (default for httpd)

🔧 Step-by-Step Solution

Step 1: Check Kubernetes Cluster Status

# Check cluster information
kubectl cluster-info

# Verify current context
kubectl config current-context

# Check nodes in the cluster
kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Deployment

Method 1: Using Imperative Command (Quick way)

# Create deployment using kubectl create deployment
kubectl create deployment httpd --image=httpd:latest

# Verify the deployment
kubectl get deployments
kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Method 2: Using YAML Manifest (Recommended)

Create a YAML file:

vi httpd-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Add the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd
  labels:
    app: httpd
spec:
  replicas: 1
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: httpd-container
        image: httpd:latest
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Apply the YAML file:

kubectl apply -f httpd-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

📝 Complete Commands (Copy & Paste)

Method 1: Quick Imperative Command

# Create the deployment
kubectl create deployment httpd --image=httpd:latest

# Verify the deployment
kubectl get deployments
kubectl get pods
kubectl get replicasets

# Check deployment details
kubectl describe deployment httpd
Enter fullscreen mode Exit fullscreen mode

Method 2: Using YAML (Recommended)

# Create the YAML file
cat > httpd-deployment.yaml << 'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd
  labels:
    app: httpd
spec:
  replicas: 1
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: httpd-container
        image: httpd:latest
        ports:
        - containerPort: 80
EOF

# Apply the configuration
kubectl apply -f httpd-deployment.yaml

# Verify
kubectl get deployments
kubectl get pods
kubectl describe deployment httpd
Enter fullscreen mode Exit fullscreen mode

📊 Expected Output

Creating Deployment (Imperative):

thor@jump-host ~$ kubectl create deployment httpd --image=httpd:latest
deployment.apps/httpd created
Enter fullscreen mode Exit fullscreen mode

Creating Deployment (YAML):

thor@jump-host ~$ kubectl apply -f httpd-deployment.yaml
deployment.apps/httpd created
Enter fullscreen mode Exit fullscreen mode

Verifying Deployment:

thor@jump-host ~$ kubectl get deployments
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
httpd   1/1     1            1           30s

thor@jump-host ~$ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
httpd-xxxxxxxxxx-xxxxx   1/1     Running   0          30s

thor@jump-host ~$ kubectl get replicasets
NAME               DESIRED   CURRENT   READY   AGE
httpd-xxxxxxxxxx   1         1         1       30s
Enter fullscreen mode Exit fullscreen mode

Describing Deployment:

thor@jump-host ~$ kubectl describe deployment httpd
Name:                   httpd
Namespace:              default
CreationTimestamp:      Tue, 11 Aug 2026 10:00:00 +0000
Labels:                 app=httpd
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=httpd
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=httpd
  Containers:
   httpd-container:
    Image:        httpd:latest
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   httpd-xxxxxxxxxx (1/1 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  30s   deployment-controller  Scaled up replica set httpd-xxxxxxxxxx to 1
Enter fullscreen mode Exit fullscreen mode

📋 YAML Explanation

Field Value Purpose
apiVersion apps/v1 Kubernetes API version for Deployments
kind Deployment Resource type
metadata.name httpd Name of the deployment
metadata.labels.app httpd Label for identifying the deployment
spec.replicas 1 Number of Pod replicas
spec.selector.matchLabels.app httpd Selector for matching Pods
spec.template.metadata.labels.app httpd Labels for the Pod template
spec.template.spec.containers[0].name httpd-container Name of the container
spec.template.spec.containers[0].image httpd:latest Container image
spec.template.spec.containers[0].ports[0].containerPort 80 Port the container listens on

🔍 Additional Verification

# Get detailed deployment information
kubectl describe deployment httpd

# Get pod details
kubectl get pods -o wide

# Check pod logs
kubectl logs -l app=httpd

# Check deployment status
kubectl rollout status deployment httpd

# Check rollout history
kubectl rollout history deployment httpd

# Check events
kubectl get events --field-selector involvedObject.name=httpd

# Check deployment in JSON format
kubectl get deployment httpd -o json

# Check deployment in YAML format
kubectl get deployment httpd -o yaml
Enter fullscreen mode Exit fullscreen mode

🛠️ Troubleshooting

If Pod is in Pending State:

# Check pod details
kubectl describe pod <pod-name>

# Check node resources
kubectl describe nodes
Enter fullscreen mode Exit fullscreen mode

If Pod is in CrashLoopBackOff:

# Check logs
kubectl logs <pod-name>

# Check previous logs
kubectl logs <pod-name> --previous
Enter fullscreen mode Exit fullscreen mode

If Image is not pulling:

# Check pod details for image pull errors
kubectl describe pod <pod-name> | grep -A 5 "Failed"
Enter fullscreen mode Exit fullscreen mode

If Deployment is not scaling:

# Check deployment status
kubectl rollout status deployment httpd

# Check replica set
kubectl get replicasets
Enter fullscreen mode Exit fullscreen mode

📊 Deployment Commands Reference

Command Description
kubectl create deployment httpd --image=httpd:latest Create deployment imperatively
kubectl apply -f httpd-deployment.yaml Create deployment from YAML
kubectl get deployments List deployments
kubectl get pods List pods
kubectl get replicasets List replica sets
kubectl describe deployment httpd Detailed deployment info
kubectl logs -l app=httpd View logs for all pods with label
kubectl rollout status deployment httpd Check rollout status
kubectl rollout history deployment httpd View rollout history
kubectl delete deployment httpd Delete deployment

🎯 Key Concepts

What is a Deployment?

A Deployment in Kubernetes manages ReplicaSets and Pods, providing:

  • Declarative updates – Define the desired state
  • Rolling updates – Update without downtime
  • Rollback – Revert to previous versions
  • Scaling – Increase or decrease replicas
  • Self-healing – Replace failed pods

Deployment vs Pod

Feature Pod Deployment
Purpose Run containers Manage pods
Self-healing Limited ✅ Yes
Scaling Manual ✅ Yes
Rolling updates No ✅ Yes
Rollback No ✅ Yes

Components Created

┌─────────────────────────────────────────────────────┐
│                  Deployment: httpd                   │
│  ┌─────────────────────────────────────────────────┐ │
│  │              ReplicaSet: httpd-xxx              │ │
│  │  ┌───────────────────────────────────────────┐ │ │
│  │  │           Pod: httpd-xxx-yyy              │ │ │
│  │  │  ┌─────────────────────────────────────┐  │ │ │
│  │  │  │    Container: httpd-container       │  │ │ │
│  │  │  │    Image: httpd:latest              │  │ │ │
│  │  │  │    Port: 80                         │  │ │ │
│  │  │  └─────────────────────────────────────┘  │ │ │
│  │  └───────────────────────────────────────────┘ │ │
│  └─────────────────────────────────────────────────┘ │
│                                                     │
│  Labels: app=httpd                                  │
└─────────────────────────────────────────────────────┘
Enter fullscreen mode Exit fullscreen mode

✅ Task Summary

Requirement Status
Deployment name httpd
Image httpd:latest
Deployment created successfully
Pod(s) running
ReplicaSet created

Top comments (0)