Skip to content

Commit 74148b5

Browse files
committed
added
0 parents  commit 74148b5

File tree

11 files changed

+421
-0
lines changed

11 files changed

+421
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
on:
2+
push:
3+
branches: [main]
4+
jobs:
5+
test:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v4 # Each job runs in fresh VM, so checkout is needed
9+
10+
- name: Set up JDK 17
11+
uses: actions/setup-java@v3
12+
with:
13+
java-version: '17'
14+
distribution: 'temurin'
15+
16+
- name: Run tests
17+
run: ./mvnw test
18+
19+
- name: Run integration tests
20+
run: ./mvnw verify
21+
build:
22+
needs: test
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v4
26+
27+
- name: Set up Docker Buildx
28+
uses: docker/setup-buildx-action@v3
29+
30+
- name: Login to ECR
31+
uses: aws-actions/amazon-ecr-login@v1
32+
33+
- name: Build and push
34+
uses: docker/build-push-action@v5
35+
with:
36+
context: .
37+
push: true
38+
tags: |
39+
${{ secrets.ECR_REGISTRY }}/myapp:latest
40+
${{ secrets.ECR_REGISTRY }}/myapp:${{ github.sha }}
41+
# Ensure you've set secrets: ECR_REGISTRY=123456789012.dkr.ecr.us-west-2.amazonaws.com
42+
deploy:
43+
needs: build
44+
runs-on: ubuntu-latest
45+
steps:
46+
- uses: actions/checkout@v4
47+
48+
- name: Configure AWS credentials
49+
uses: aws-actions/configure-aws-credentials@v4
50+
with:
51+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
52+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
53+
aws-region: us-west-2
54+
55+
- name: Deploy to ECS
56+
run: |
57+
# Update ECS service with new image (requires AWS CLI v2+)
58+
aws ecs update-service \
59+
--cluster production-cluster \
60+
--service app-service \
61+
--force-new-deployment
62+
63+
aws ecs wait services-stable \
64+
--cluster production-cluster \
65+
--services app-service

Surviving 10K Users/IAC/main.tf

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 5.0"
6+
}
7+
}
8+
}
9+
10+
provider "aws" {
11+
region = var.aws_region
12+
}
13+
14+
# Get available AZs
15+
data "aws_availability_zones" "available" {}
16+
17+
# VPC - Your isolated network
18+
resource "aws_vpc" "main" {
19+
cidr_block = "10.0.0.0/16"
20+
enable_dns_hostnames = true
21+
enable_dns_support = true
22+
23+
tags = {
24+
Name = "main-vpc"
25+
}
26+
}
27+
28+
# Internet Gateway for public access
29+
resource "aws_internet_gateway" "main" {
30+
vpc_id = aws_vpc.main.id
31+
32+
tags = {
33+
Name = "main-igw"
34+
}
35+
}
36+
37+
# Public subnets for load balancer
38+
resource "aws_subnet" "public" {
39+
count = 2
40+
vpc_id = aws_vpc.main.id
41+
cidr_block = "10.0.${count.index + 1}.0/24"
42+
availability_zone = data.aws_availability_zones.available.names[count.index]
43+
44+
map_public_ip_on_launch = true
45+
46+
tags = {
47+
Name = "public-subnet-${count.index + 1}"
48+
}
49+
}
50+
51+
# Private subnets for application servers
52+
resource "aws_subnet" "private" {
53+
count = 2
54+
vpc_id = aws_vpc.main.id
55+
cidr_block = "10.0.${count.index + 10}.0/24"
56+
availability_zone = data.aws_availability_zones.available.names[count.index]
57+
58+
tags = {
59+
Name = "private-subnet-${count.index + 1}"
60+
}
61+
}
62+
63+
# Security group for ALB
64+
resource "aws_security_group" "alb" {
65+
name_prefix = "alb-sg"
66+
vpc_id = aws_vpc.main.id
67+
68+
ingress {
69+
from_port = 80
70+
to_port = 80
71+
protocol = "tcp"
72+
cidr_blocks = ["0.0.0.0/0"]
73+
}
74+
75+
egress {
76+
from_port = 0
77+
to_port = 0
78+
protocol = "-1"
79+
cidr_blocks = ["0.0.0.0/0"]
80+
}
81+
}
82+
83+
# Application Load Balancer
84+
resource "aws_lb" "main" {
85+
name = "main-alb"
86+
internal = false
87+
load_balancer_type = "application"
88+
security_groups = [aws_security_group.alb.id]
89+
subnets = aws_subnet.public[*].id
90+
91+
enable_deletion_protection = false
92+
93+
tags = {
94+
Name = "main-alb"
95+
}
96+
}

Surviving 10K Users/IAC/var.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
variable "aws_region" {
2+
description = "AWS region"
3+
type = string
4+
default = "us-west-2"
5+
}
6+
7+
variable "instance_type" {
8+
description = "EC2 instance type"
9+
type = string
10+
default = "t3.medium"
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM openjdk:17-jdk-slim
2+
3+
# Create a non-root user for security
4+
RUN groupadd -r appuser && useradd -r -g appuser appuser
5+
6+
WORKDIR /app
7+
8+
# Copy and cache dependencies first (speeds up rebuilds)
9+
COPY pom.xml .
10+
COPY mvnw .
11+
COPY .mvn .mvn
12+
RUN ./mvnw dependency:go-offline
13+
14+
# Then copy source code
15+
COPY src src
16+
RUN ./mvnw package -DskipTests
17+
18+
USER appuser
19+
20+
EXPOSE 8080
21+
22+
# Use exec form for proper signal handling
23+
CMD ["java", "-jar", "-Xmx512m", "-XX:+UseContainerSupport", "target/app.jar"]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
version: '3.8'
2+
3+
services:
4+
app:
5+
build: .
6+
ports:
7+
- "8080:8080"
8+
environment:
9+
- SPRING_PROFILES_ACTIVE=production
10+
- DATABASE_URL=jdbc:postgresql://db:5432/myapp
11+
depends_on:
12+
- db
13+
- redis
14+
restart: unless-stopped
15+
16+
# Note: deploy settings only apply in swarm mode
17+
# For plain docker-compose, use mem_limit and cpus at service level
18+
mem_limit: 1G
19+
cpus: 0.5
20+
21+
healthcheck:
22+
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
23+
interval: 30s
24+
timeout: 10s
25+
retries: 3
26+
27+
db:
28+
image: postgres:15
29+
environment:
30+
POSTGRES_DB: myapp
31+
POSTGRES_USER: dbuser
32+
POSTGRES_PASSWORD: ${DB_PASSWORD} # Pass securely via .env file
33+
volumes:
34+
- postgres_data:/var/lib/postgresql/data
35+
restart: unless-stopped
36+
37+
redis:
38+
image: redis:7-alpine
39+
command: redis-server --appendonly yes
40+
volumes:
41+
- redis_data:/data
42+
restart: unless-stopped
43+
44+
volumes:
45+
postgres_data:
46+
redis_data:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: myapp
5+
namespace: production
6+
spec:
7+
replicas: 3
8+
strategy:
9+
type: RollingUpdate
10+
rollingUpdate:
11+
maxSurge: 1
12+
maxUnavailable: 0 # Zero downtime deployments
13+
selector:
14+
matchLabels:
15+
app: myapp
16+
template:
17+
metadata:
18+
labels:
19+
app: myapp
20+
spec:
21+
containers:
22+
- name: myapp
23+
image: myregistry/myapp:latest
24+
ports:
25+
- containerPort: 8080
26+
env:
27+
- name: DATABASE_URL
28+
valueFrom:
29+
secretKeyRef:
30+
name: db-secret
31+
key: url
32+
resources:
33+
requests:
34+
memory: "256Mi"
35+
cpu: "250m"
36+
limits:
37+
memory: "512Mi" # Prevents OOMKilled errors
38+
cpu: "500m"
39+
livenessProbe:
40+
httpGet:
41+
path: /health
42+
port: 8080
43+
initialDelaySeconds: 30 # Adjust based on your app's cold start time
44+
periodSeconds: 10
45+
readinessProbe:
46+
httpGet:
47+
path: /health/ready
48+
port: 8080
49+
initialDelaySeconds: 5
50+
periodSeconds: 5

Surviving 10K Users/k8s/hpa.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apiVersion: autoscaling/v2 # Ensure your cluster supports v2 HPA (1.23+)
2+
kind: HorizontalPodAutoscaler
3+
metadata:
4+
name: myapp-hpa
5+
namespace: production
6+
spec:
7+
scaleTargetRef:
8+
apiVersion: apps/v1
9+
kind: Deployment
10+
name: myapp
11+
minReplicas: 3
12+
maxReplicas: 50
13+
metrics:
14+
- type: Resource
15+
resource:
16+
name: cpu
17+
target:
18+
type: Utilization
19+
averageUtilization: 70
20+
- type: Resource
21+
resource:
22+
name: memory
23+
target:
24+
type: Utilization
25+
averageUtilization: 80
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
groups:
2+
- name: critical_alerts
3+
rules:
4+
- alert: HighErrorRate
5+
expr: rate(http_requests_total{job="app",status=~"5.."}[5m]) > 0.1
6+
for: 2m
7+
labels:
8+
severity: critical
9+
annotations:
10+
summary: "High error rate detected"
11+
description: "Error rate is {{ $value }} errors per second"
12+
13+
- alert: ResponseTimeHigh
14+
expr: histogram_quantile(0.95, rate(http_request_duration_seconds_bucket{job="app"}[5m])) > 0.5
15+
for: 5m
16+
labels:
17+
severity: warning
18+
annotations:
19+
summary: "High latency detected"
20+
description: "95th percentile latency is {{ $value }}s"
21+
22+
- alert: DatabaseConnectionsHigh
23+
expr: db_connection_pool_active{job="app"} / db_connection_pool_max{job="app"} > 0.8
24+
for: 3m
25+
labels:
26+
severity: warning
27+
annotations:
28+
summary: "Database connection pool nearly exhausted"

0 commit comments

Comments
 (0)