Step-by-Step Guide to Deploy a Scalable Voting App Using Kubernetes
- 5 minsDeploy Voting App on K8s (Microservices)
Introduction
This guide explains how to deploy a Voting App with a microservices architecture in a Kubernetes cluster. The application consists of five components: the Voting App, Worker, Redis, PostgreSQL, and Result App. Each component is containerized and communicates via Kubernetes services. This tutorial covers prerequisites, deployment steps, and service configurations for access from local.
Prerequisites
- Kubernetes Cluster: A running Kubernetes cluster.
- kubectl: Installed and configured to manage the Kubernetes cluster.
- YAML Files: Deployment and Service configuration files for each component. (You can use my Public GitHub Repo)
Summary of Components
-
Pods: Redis, PostgreSQL, Voting App, Worker App, Result App (5 total).
-
Services: Redis (ClusterIP): Internal access for Voting App and Worker App. PostgreSQL (ClusterIP): Internal access for Worker App and Result App. Voting App (NodePort): External access for users to cast votes. Result App (NodePort): External access for users to view results.
-
Worker App: No service required; it communicates directly with Redis and PostgreSQL.
Deployment Steps
Step 1: Deploy Redis Database
-
Create Redis Deployment: Deploy the Redis database using a Kubernetes Deployment YAML file. Ensure Redis listens on port 6379.
-
Expose Redis: Create a ClusterIP service named redis. This service allows the Voting App and Worker App to access Redis.
Step 2: Deploy PostgreSQL Database
-
Create PostgreSQL Deployment: Deploy PostgreSQL with a Deployment YAML file. Configure it to listen on port 5432. Set environment variables for POSTGRES_USER and POSTGRES_PASSWORD as postgres.
-
Expose PostgreSQL: Create a ClusterIP service named db. This service enables the Worker App and Result App to connect to the database.
Step 3: Deploy the Worker App
-
Create Worker Deployment: Deploy the Worker App using a Deployment YAML file. The Worker App processes votes from Redis and updates the PostgreSQL database.
-
No Service Needed: The Worker App does not require a Kubernetes service as it is not accessed by other components or users.
Step 4: Deploy the Voting App
-
Create Voting App Deployment: Deploy the Voting App with a Deployment YAML file. Ensure it runs a Python web server on port 80.
-
Expose Voting App: Create a NodePort service to provide external access. Specify a port for external users to cast votes.
Step 5: Deploy the Result App
-
Create Result App Deployment: Deploy the Result App using a Deployment YAML file. Ensure it runs a web server on port 80 to display results.
-
Expose Result App: Create a NodePort service for external access. Specify a port for users to view results.
Step 3: Deployments and Services YAML Files
You can use my public repo’s yaml files to create deployments and services.
- URL of My GitHub Repo for K8s yamls -
Once You pull repo , You will have yaml files , You do not need to use pod yamls , I have already converted them to deployment yamls so , You can create deployments and also regarding services.
- Create Redis deployment and service
kubectl create -f redis-deployment.yaml kubectl create -f pod-service-yamls/redis-service.yaml
- Create Postgres DB deployment and service
kubectl create -f deployment-yamls/postgres-deployment.yaml kubectl create -f pod-service-yamls/postgres-service.yaml
- Create Worker App deployment, only deployment because worker app no need to listen or expose any ports
kubectl create -f deployment-yamls/worker-app-deployment.yaml
- Create Voting App deployment and service
kubectl create -f deployment-yamls/result-app-deployment.yaml kubectl create -f pod-service-yamls/voting-app-service.yaml
- Create Results App deployment and service
kubectl create -f deployment-yamls/voting-app-deployment.yaml kubectl create -f pod-service-yamls/result-app-service.yaml
Note: Since We are using deployments We can edit replicas of pods , I prefered to increase voting app replicas to 3, You can edit your replicas after you create deployment but in that case You need to run kubectl appy command like below:
kubectl apply -f deployment-yamls/voting-app-deployment.yaml
Step 4: Checking Cluster Resources
We have created all deployments and service now We can check What we have cluster , Depends on your choise You can check resources seperatly , I check all resources with kubectl
kubectl get all
- We have 2 service are using NodePort so that means We can access these Pods from our local.
- Note: ClusterIp is default service for communication between Pods/Containers
- If you have K8s cluster in VM or/in diffrent network , You may need to do Port Forwarding
-
Port Forward to K8s host/s
ssh -L 9000:10.101.202.69:80 guney@192.168.1.159(k8s worker or master) ssh -L 9001:10.99.113.120:80 guney@192.168.1.159(k8s worker or master)
- You can vote animals fro voting app and check result app.
Conclusion
Today, we explored the process of creating Kubernetes (K8s) deployments and services in the context of microservices. Kubernetes offers a wide variety of resources and services, each playing a unique role in managing containerized applications. As part of our learning experience, we focused on collecting and organizing these components into a single file for easier management and understanding.
This article is aimed at beginners looking to understand how Kubernetes works, specifically in the deployment and orchestration of microservices. We will guide you through the various Kubernetes resources, their functions, and how they work together to support scalable, reliable applications.
Thanks for reading…
Guneycan Sanli.