top of page

Roll Update and Roll back with Kubernetes


Project: student-feedback-app

This is a simple Flask application that:

  • Shows Application Version

  • Shows Environment Mode

  • Runs inside Docker

  • Deployed in Kubernetes

  • Uses Rolling Update

  • Uses ConfigMap

Everything with real names.

PART 1 – Create Real Application

Step 1: Create Project Folder

On your system:

mkdir student-feedback-app
cd student-feedback-app

Step 2: Create Flask Application

Create file:

from flask import Flask
import os

app = Flask(__name__)

@app.route("/")
def home():
    version = os.getenv("APP_VERSION")
    mode = os.getenv("APP_MODE")
    return f"""
    <h2>Student Feedback Application</h2>
    <p>Application Version: {version}</p>
    <p>Environment Mode: {mode}</p>
    """

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

What we are doing here?

  • os.getenv() reads environment variables

  • We are NOT hardcoding version

  • We are NOT hardcoding mode

  • These values will come from Kubernetes

This is real DevOps practice.

Step 3: Create requirements.txt

flask

Step 4: Create Dockerfile

FROM python:3.9-slim

WORKDIR /app

COPY . .

RUN pip install -r requirements.txt

EXPOSE 5000

CMD ["python", "app.py"]

What each line does?

Line

Meaning

FROM

Base image

WORKDIR

Creates working directory

COPY

Copies project files

RUN

Installs flask

EXPOSE

Opens port 5000

CMD

Starts app

PART 2 – Build Docker Image

Step 5: Build Image

docker build -t student-feedback-app:v1 .

What happens here?

  • Docker reads Dockerfile

  • Creates image named student-feedback-app

  • Version tagged as v1

PART 3 – Deploy to Kubernetes

Step 6: Create Deployment YAML

Create file:

student-feedback-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: student-feedback-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: student-feedback
  template:
    metadata:
      labels:
        app: student-feedback
    spec:
      containers:
      - name: student-feedback-container
        image: student-feedback-app:v1
        imagePullPolicy: Never
        ports:
        - containerPort: 5000
        env:
        - name: APP_VERSION
          value: "1.0"
        - name: APP_MODE
          value: "development"

Explanation of Important Sections

Real name of deployment:

student-feedback-deployment

replicas: 3

Creates 3 Pods → High Availability

matchLabels

Connects Deployment to Pods

imagePullPolicy: Never

Since we built image locally

env:

We are setting:

APP_VERSION = 1.0
APP_MODE = development

These go inside container as environment variables.

Step 7: Apply Deployment

kubectl apply -f student-feedback-deployment.yaml

Check:

kubectl get pods

You should see 3 running pods.

PART 4 – Expose Application

Step 8: Create Service

kubectl expose deployment student-feedback-deployment --type=NodePort --port=5000

Check service:

kubectl get svc

Open:

You will see:

Student Feedback ApplicationApplication Version: 1.0Environment Mode: development


PART 5 – Rolling Update (Real Example)

Now imagine we improved UI and want version 2.

Step 9: Modify app.py

Change heading to:

<h2>Student Feedback Application - Updated UI</h2>

Build new image:

docker build -t student-feedback-app:v2 .

Step 10: Update Deployment Image

kubectl set image deployment/student-feedback-deployment \
student-feedback-container=student-feedback-app:v2

What happens?

  • Kubernetes does NOT delete all pods

  • It replaces pods one by one

  • Zero downtime

  • This is Rolling Update

Check:

kubectl rollout status deployment student-feedback-deployment

Rollback (If Something Breaks)

kubectl rollout undo deployment student-feedback-deployment

Kubernetes returns to previous version automatically.

This is real production practice.


PART 6 – Use ConfigMap (Real DevOps Practice)

Now instead of hardcoding APP_MODE inside YAML, we externalize it.

Step 11: Create ConfigMap

kubectl create configmap student-feedback-config \
--from-literal=APP_MODE=production

Check:

kubectl get configmap

Step 12: Update Deployment YAML

Replace APP_MODE section:

- name: APP_MODE
  valueFrom:
    configMapKeyRef:
      name: student-feedback-config
      key: APP_MODE

Apply again:

kubectl apply -f student-feedback-deployment.yaml

What happened?

  • We did NOT rebuild Docker image

  • We changed only configuration

  • App now shows:

Environment Mode: production

That is clean DevOps architecture.

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.

Recommended Products For This Post
 
 
 

Recent Posts

See All
How to Use Blackbox AI for Free in DevOps

DevOps engineers constantly write Dockerfiles, Kubernetes YAMLs, CI/CD pipelines, Terraform scripts, and shell automation . Writing all of this manually takes time and increases the chance of configur

 
 
 

Comments


© 2023 by newittrendzzz.com 

  • Facebook
  • Twitter
  • Instagram
bottom of page