This project extends the original dockersamples/example-voting-app with advanced Kubernetes features and Infrastructure as Code (IaC) using Terraform for Azure deployment.
The Example Voting App is a distributed application running across multiple containers:
- A Python web app for voting between two options
- A Redis queue for collecting votes
- A .NET worker for processing votes
- A PostgreSQL database for storing vote results
- A Node.js web app for displaying results
The original Kubernetes manifests have been enhanced with the following features:
-
Resource Management
- Added resource requests and limits to all deployments
- Ensures proper resource allocation and prevents resource starvation
-
Health Monitoring
- Implemented liveness and readiness probes
- Improves reliability by automatically restarting unhealthy containers
-
Configuration Management
- Added ConfigMaps for application configuration
- Separates configuration from code for better maintainability
-
Security
- Implemented Secrets for sensitive information (database credentials)
- Improves security by keeping sensitive data separate from code
-
Auto-scaling
- Added Horizontal Pod Autoscaler (HPA)
- Automatically scales the application based on CPU/memory usage
-
Network Security
- Implemented Network Policies
- Restricts communication between pods for improved security
-
High Availability
- Added Pod Disruption Budget (PDB)
- Ensures application availability during cluster maintenance
-
Local Development
- Created script to build Docker images locally
- Modified deployment files to use local images with
imagePullPolicy: Never
Added Terraform configuration to deploy the application on Azure:
-
Azure Infrastructure
- Resource Group for organizing all resources
- Virtual Network and Subnet for network isolation
- Network Security Group with appropriate security rules
- Static Public IP for reliable access
- Storage Account and Managed Disk for persistent data
-
Virtual Machine
- Ubuntu VM with Docker pre-installed
- Cloud-init script for automatic application deployment
- Managed disk attachment for PostgreSQL data persistence
-
Deployment Options
- Single VM deployment for simplicity and cost-effectiveness
- (Optional) Multi-VM microservices deployment for production scenarios
For local development and testing:
docker compose upAccess the applications at:
- Vote app: http://localhost:8080
- Results app: http://localhost:8081
For a simple container orchestration:
docker swarm init
docker stack deploy --compose-file docker-stack.yml voteThe k8s-specifications folder contains enhanced YAML manifests with advanced Kubernetes features.
# Deploy all components
kubectl create -f k8s-specifications/
# Check the status of deployments
kubectl get pods
# Access the applications
# Vote app is available on port 31000 on each cluster node
# Result app is available on port 31001 on each cluster nodeTo remove the Kubernetes deployment:
kubectl delete -f k8s-specifications/-
View the HPA configuration:
kubectl get hpa
-
View the ConfigMap:
kubectl get configmap voting-app-configmap -o yaml
-
View the Secret:
kubectl get secret db-secret -o yaml
-
View Network Policies:
kubectl get networkpolicies
The terraform directory contains IaC configuration to deploy on Azure:
# Navigate to the terraform directory
cd terraform
# Initialize Terraform
terraform init
# Review the deployment plan
terraform plan
# Apply the configuration
terraform applyAfter deployment completes (5-10 minutes), Terraform will output:
- The public IP address of the VM
- SSH command to connect to the VM
Access the applications at:
- Vote app: http://<vm_public_ip>:8080
- Results app: http://<vm_public_ip>:8081
To destroy the Azure infrastructure:
terraform destroy- A front-end web app in Python which lets you vote between two options
- A Redis which collects new votes
- A .NET worker which consumes votes and stores them in…
- A Postgres database backed by a Docker volume
- A Node.js web app which shows the results of the voting in real time
├── k8s-specifications/ # Enhanced Kubernetes manifests
│ ├── db-deployment.yaml # PostgreSQL deployment
│ ├── db-pvc.yaml # Persistent Volume Claim for PostgreSQL
│ ├── db-secret.yaml # Secret for database credentials
│ ├── network-policy.yaml # Network security policies
│ ├── vote-hpa.yaml # Horizontal Pod Autoscaler
│ ├── vote-pdb.yaml # Pod Disruption Budget
│ └── voting-app-configmap.yaml # Application configuration
│
├── terraform/ # Azure Infrastructure as Code
│ ├── main.tf # Main Terraform configuration
│ ├── variables.tf # Variable definitions
│ ├── cloud-init.tpl # VM initialization script
│ └── README.md # Terraform-specific instructions
│
├── vote/ # Python frontend application
├── result/ # Node.js results application
├── worker/ # .NET vote processor
├── seed-data/ # Test data generation scripts
└── build-images.sh # Script to build local Docker images
- Resource Allocation: Modify CPU/memory requests and limits in deployment YAML files
- Scaling Parameters: Adjust HPA min/max replicas and CPU threshold in vote-hpa.yaml
- Network Policies: Customize allowed traffic patterns in network-policy.yaml
- Application Config: Update application settings in voting-app-configmap.yaml
- VM Size: Change the VM size in variables.tf for different performance tiers
- Region: Modify the Azure region in variables.tf for geographic deployment
- Storage: Adjust the disk size for PostgreSQL data in main.tf
- Network: Customize network rules in the Network Security Group section
The voting application only accepts one vote per client browser. It does not register additional votes if a vote has already been submitted from a client.
This enhanced version demonstrates how to apply DevOps best practices to a simple distributed application, showcasing both container orchestration with Kubernetes and infrastructure automation with Terraform.
