Docker Certified Associate Exam Course

Kubernetes

Demo Deploy voting app on Kubernetes with Deployments

Managing individual Pods poses challenges such as manual scaling and downtime during updates. Kubernetes Deployments resolve these by automating ReplicaSet management, rolling updates, and rollbacks.

Why Use Deployments?

Deployments provide declarative updates, automatic rollbacks, and easy scaling.
Learn more: Kubernetes Deployments.

Voting App Architecture

The image is a diagram of an example voting app architecture using Kubernetes, showing multiple pods for voting and result apps, along with Redis and database services. It illustrates the deployment and service connections between these components.

1. Define Deployment Manifests

Below is a summary of all Deployment YAML files:

Manifest FileComponentImage
voting-app-deploy.yamlVoting Frontendkodekloud/examplevotingapp_vote:v1
redis-deploy.yamlRedis Cacheredis
postgres-deploy.yamlPostgreSQL DBpostgres
worker-app-deploy.yamlBackground Workkodekloud/examplevotingapp_worker:v1
result-app-deploy.yamlResults Frontendkodekloud/examplevotingapp_result:v1

voting-app-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: voting-app-deploy
  labels:
    app: demo-voting-app
    component: voting
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-voting-app
      component: voting
  template:
    metadata:
      labels:
        app: demo-voting-app
        component: voting
    spec:
      containers:
      - name: voting-app
        image: kodekloud/examplevotingapp_vote:v1
        ports:
        - containerPort: 80

redis-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-deploy
  labels:
    app: demo-voting-app
    component: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-voting-app
      component: redis
  template:
    metadata:
      labels:
        app: demo-voting-app
        component: redis
    spec:
      containers:
      - name: redis
        image: redis
        ports:
        - containerPort: 6379

postgres-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deploy
  labels:
    app: demo-voting-app
    component: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-voting-app
      component: postgres
  template:
    metadata:
      labels:
        app: demo-voting-app
        component: postgres
    spec:
      containers:
      - name: postgres
        image: postgres
        ports:
        - containerPort: 5432
        env:
        - name: POSTGRES_USER
          value: "postgres"
        - name: POSTGRES_PASSWORD
          value: "postgres"

worker-app-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: worker-app-deploy
  labels:
    app: demo-voting-app
    component: worker
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-voting-app
      component: worker
  template:
    metadata:
      labels:
        app: demo-voting-app
        component: worker
    spec:
      containers:
      - name: worker-app
        image: kodekloud/examplevotingapp_worker:v1

result-app-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: result-app-deploy
  labels:
    app: demo-voting-app
    component: result
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-voting-app
      component: result
  template:
    metadata:
      labels:
        app: demo-voting-app
        component: result
    spec:
      containers:
      - name: result-app
        image: kodekloud/examplevotingapp_result:v1
        ports:
        - containerPort: 80

2. Apply Deployments and Services

First, ensure no leftover Pods or Services:

kubectl get pods,svc
# No resources found

Create each Deployment and its Service:

kubectl apply -f voting-app-deploy.yaml
kubectl apply -f voting-app-service.yaml

kubectl apply -f redis-deploy.yaml
kubectl apply -f redis-service.yaml

kubectl apply -f postgres-deploy.yaml
kubectl apply -f postgres-service.yaml

kubectl apply -f worker-app-deploy.yaml

kubectl apply -f result-app-deploy.yaml
kubectl apply -f result-app-service.yaml

3. Verify Deployments and Services

Check Deployments and Pods:

kubectl get deployments
kubectl get pods

Confirm Services:

kubectl get svc
SERVICETYPECLUSTER-IPPORT(S)
voting-serviceNodePort10.100.x.y80:30004/TCP
result-serviceNodePort10.105.x.z80:30005/TCP
redisClusterIP10.104.x.y6379/TCP
dbClusterIP10.107.x.z5432/TCP

4. Access Front-end Services

Retrieve and open URLs:

minikube service voting-service --url
minikube service result-service --url

Visit the voting app, cast a vote, then check the result app to verify it’s recorded.

5. Scale the Front-end

Scaling with Deployments is straightforward. Increase replicas for the voting app:

kubectl scale deployment voting-app-deploy --replicas=3

Verify three Pods are Running:

kubectl get pods -l component=voting

Refresh the voting URL to observe traffic served by multiple replicas—no downtime required!


Watch Video

Watch video content

Previous
Demo Deploy voting app on Kubernetes