Skip to content

This project demonstrates Infrastructure as Code (IaC) to deploy Kubernetes worker nodes on AWS EC2 instances using Terraform, Ansible, Docker, and Kubernetes. It also deploys a sample FastAPI application inside Kubernetes automatically.

Notifications You must be signed in to change notification settings

Vrana710/k8s-ec2-autoscaling-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kubernetes EC2 Autoscaling with FastAPI on Kubernetes

📚 Project Overview

This project demonstrates Infrastructure as Code (IaC) to deploy Kubernetes worker nodes on AWS EC2 instances using Terraform, Ansible, Docker, and Kubernetes. It also deploys a sample FastAPI application inside Kubernetes automatically.


🛠 How to Run

1. Prerequisites

  • AWS Account
  • IAM User with EC2 permissions
  • Terraform (>= 1.7.0)
  • Ansible (9.3.0)
  • Python3 environment
  • SSH Key Pair for EC2 access

2. Setup

git clone https://github.com/Vrana710/k8s-ec2-autoscaling-demo.git
cd k8s-ec2-autoscaling

Install requirements:

pip install -r requirements.txt

3. Provision EC2 Instance

cd infrastructure/terraform
terraform init
terraform apply -auto-approve

Get the Public IP from output.

4. Configure Ansible Inventory

Edit config/inventory.ini:

[all]
<public_ip> ansible_user=ubuntu ansible_ssh_private_key_file=../your_key_name.pem

5. Configure EC2 Server

cd infrastructure/ansible
ansible-playbook playbook.yml -i ../../config/inventory.ini

This installs Docker, Kubernetes components, and CloudWatch agent.

6. Initialize Kubernetes Cluster

SSH into EC2:

ssh -i ../your_key_name.pem ubuntu@<public_ip>

Initialize Kubernetes:

sudo kubeadm init --pod-network-cidr=10.244.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Install Flannel CNI:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

7. Build FastAPI Docker Image

cd ~/app
sudo docker build -t fastapi-app:latest .

8. Deploy FastAPI App to Kubernetes

kubectl apply -f app/deployment.yaml

Check deployment:

kubectl get pods
kubectl get svc

Access FastAPI app via public IP.

9. Monitor and Scale

Monitor CPU manually:

bash scripts/monitor_and_scale.sh

If CPU exceeds threshold, new nodes are provisioned.


🎯 Outcome

  • Fully provisioned EC2 instance
  • Configured Kubernetes + CloudWatch
  • FastAPI app deployed inside Kubernetes
  • Ready for manual or automatic scaling
  • GitHub Actions pipeline ready for full automation

📈 Architecture Diagram

Architecture Diagram


📦 Project Structure

k8s-ec2-autoscaling/
├── app/
│   ├── main.py
│   ├── Dockerfile
│   └── deployment.yaml
├── config/
│   ├── inventory.ini
│   └── terraform.tfvars (with updated values)
├── infrastructure/
│   ├── terraform/
│   └── ansible/
├── scripts/
│   └── monitor_and_scale.sh
├── .github/workflows/deploy.yml
├── requirements.txt
└── docs/
    └── README.md

⚙️ Terraform Variable Configuration

Edit config/terraform.tfvars:

region = "your name of region"
ami_id = "your ami id"
key_name = "your key name"
instance_type = "your instance type"

Important: Key name is the AWS EC2 Key Pair name (without .pem).


🛡️ Security

  • SSH via key-pair only
  • Minimum necessary ports open (22, 80)
  • Terraform-managed security groups
  • CloudWatch Agent installed for monitoring

About

This project demonstrates Infrastructure as Code (IaC) to deploy Kubernetes worker nodes on AWS EC2 instances using Terraform, Ansible, Docker, and Kubernetes. It also deploys a sample FastAPI application inside Kubernetes automatically.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published