Skip to content

Commit d52f697

Browse files
committed
Update Readme, add some comments
1 parent d31ccaf commit d52f697

File tree

6 files changed

+145
-13
lines changed

6 files changed

+145
-13
lines changed

.github/workflows/deploy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
with:
2222
context: .
2323
push: true
24-
tags: narsic/scaling_kubernetes_hpa_vpa:latest
24+
tags: sajadbahar/scaling_kubernetes_hpa_vpa:latest
2525

2626
deploy-to-k8s:
2727
runs-on: ubuntu-latest

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Sajad Baharvandi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Kubernetes Scaling with HPA and VPA
2+
3+
This repository demonstrates the setup and configuration of Kubernetes Horizontal Pod Autoscaler (HPA) and Vertical Pod Autoscaler (VPA) to achieve efficient scaling in a microservice environment. It includes manifests and configurations for deploying a sample application on Kubernetes with scaling capabilities.
4+
5+
## Overview
6+
7+
This project provides:
8+
- **Horizontal Pod Autoscaling (HPA)** to scale pods based on CPU usage.
9+
- **Vertical Pod Autoscaling (VPA)** to adjust pod resource requests automatically.
10+
- Examples of CI/CD integration using GitHub Actions and Docker images.
11+
12+
## Project Structure
13+
14+
```plaintext
15+
├── k8s-manifests # Kubernetes deployment and service manifests
16+
├── examples # Contain Exanple for VPA, HPA and combined one
17+
├── .github/workflows # GitHub Actions workflows for CI/CD pipeline
18+
└── README.md # Project documentation
19+
```
20+
21+
### Key Files
22+
- **k8s-manifests/deployment.yaml** - Configures deployment with autoscaling.
23+
- **k8s-manifests/hpa.yaml** - Sets up Horizontal Pod Autoscaling (HPA).
24+
- **k8s-manifests/vpa.yaml** - Sets up Vertical Pod Autoscaling (VPA).
25+
- **.github/workflows** - CI/CD pipeline to build and push Docker images.
26+
27+
## Features
28+
- Horizontal Pod Autoscaling (HPA): Automatically scales the number of pod replicas based on CPU utilization.
29+
- Vertical Pod Autoscaling (VPA): Dynamically adjusts CPU and memory resource requests and limits for containers.
30+
- Example manifests for deploying and scaling a sample application.
31+
32+
## Requirements
33+
34+
- **Kubernetes Cluster** - Minimum version 1.18.
35+
- **kubectl** - To apply manifests and manage the cluster.
36+
- **Docker** - For building images.
37+
- **GitHub CLI** - Recommended for managing GitHub Actions locally.
38+
39+
## Setup Instructions
40+
41+
### 1. Clone the Repository
42+
43+
```bash
44+
git clone https://github.com/sajadbahar/scaling_kubernetes_hpa_vpa.git
45+
cd scaling_kubernetes_hpa_vpa
46+
```
47+
48+
### 2. Configure Docker Registry and CI/CD
49+
50+
Make sure to update the Docker image reference in `.github/workflows` with your Docker registry credentials.
51+
52+
### 3. Deploy the Application
53+
54+
Use `kubectl` to apply the manifests:
55+
56+
```bash
57+
kubectl apply -f k8s-manifests/
58+
```
59+
60+
### 4. Verify Deployment and Scaling
61+
62+
Check the status of your deployments and autoscaling:
63+
64+
```bash
65+
kubectl get pods
66+
kubectl describe hpa
67+
kubectl describe vpa
68+
```
69+
70+
## Environment Variables
71+
72+
To facilitate flexible configuration, use environment variables for sensitive data and deployment-specific values.
73+
74+
## Metrics Collection
75+
76+
To enable metrics collection for autoscaling, you will need a metrics server in your Kubernetes cluster.
77+
You can deploy Prometheus as a Helm chart for collecting metrics or use any other preferred setup.
78+
79+
## CI/CD Pipeline
80+
81+
The repository is set up with GitHub Actions CI/CD, which automates the process of building and pushing the Docker image.
82+
Make sure to review and configure the `.github/workflows` file to set up your specific repository, image, and credentials.
83+
84+
## Environment Variables
85+
86+
To make image configuration dynamic, specify the Docker image repository as an environment variable:
87+
88+
```yaml
89+
containers:
90+
- name: worker
91+
image: ${DOCKER_REPO:-your-default-image}
92+
```
93+
Update the deployment YAML files with environment variables as required by your CI/CD setup.
94+
95+
## Contributing
96+
97+
Contributions are welcome! Please submit a pull request or open an issue for discussion.
98+
99+
## License
100+
101+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE.md) file for details.
102+
103+
---
104+
105+
This repository is a demonstration of Kubernetes autoscaling features and is intended for learning purposes.

k8s-manifests/deployment.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
# Kubernetes Deployment for worker service
2+
# Defines a Deployment for autoscaling tests, running a worker service with specified resource requests and limits.
13
apiVersion: apps/v1
24
kind: Deployment
35
metadata:
46
name: worker
57
spec:
6-
replicas: 1
8+
replicas: 1 # Initial number of replicas, set by Horizontal Pod Autoscaler (HPA)
79
selector:
810
matchLabels:
911
app: worker
@@ -14,14 +16,14 @@ spec:
1416
spec:
1517
containers:
1618
- name: worker
17-
image: narsic/scaling_kubernetes_hpa_vpa:latest
19+
image: ${WORKER_IMAGE:-sajadbahar/scaling_kubernetes_hpa_vpa:latest} # Default if env variable is not set
1820
resources:
1921
requests:
20-
cpu: "10m" # Define CPU requests
21-
memory: "12Mi" # Define memory requests (optional)
22+
cpu: "10m" # Minimum CPU requested for Pod scheduling
23+
memory: "12Mi" # Minimum memory requested for Pod scheduling
2224
limits:
23-
cpu: "50m" # Define CPU limits
24-
memory: "25Mi" # Define memory limits (optional)
25+
cpu: "50m" # Maximum CPU allowed per Pod
26+
memory: "25Mi" # Maximum memory allowed per Pod
2527
env:
2628
- name: REDIS_HOST
2729
value: $(REDIS_SERVICE_HOST) # Adjust based on your service name

k8s-manifests/hpa.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Horizontal Pod Autoscaler (HPA) for the worker Deployment
2+
# Adjusts the number of replicas based on CPU utilization thresholds.
13
apiVersion: autoscaling/v2
24
kind: HorizontalPodAutoscaler
35
metadata:
@@ -6,13 +8,13 @@ spec:
68
scaleTargetRef:
79
apiVersion: apps/v1
810
kind: Deployment
9-
name: worker
10-
minReplicas: 1
11-
maxReplicas: 5
11+
name: worker # Name of the deployment to scale
12+
minReplicas: 1 # Minimum number of Pods
13+
maxReplicas: 5 # Maximum number of Pods
1214
metrics:
1315
- type: Resource
1416
resource:
1517
name: cpu
1618
target:
1719
type: Utilization
18-
averageUtilization: 70
20+
averageUtilization: 70 # Target average CPU utilization per Pod

k8s-manifests/vpa.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Vertical Pod Autoscaler (VPA) for the worker Deployment
2+
# Adjusts CPU and memory requests for Pods based on observed usage patterns.
13
apiVersion: autoscaling.k8s.io/v1
24
kind: VerticalPodAutoscaler
35
metadata:
@@ -6,6 +8,6 @@ spec:
68
targetRef:
79
apiVersion: "apps/v1"
810
kind: Deployment
9-
name: worker
11+
name: worker # Deployment to which the VPA applies
1012
updatePolicy:
11-
updateMode: "Auto"
13+
updateMode: "Auto" # Mode of VPA; Auto allows automatic resource adjustments

0 commit comments

Comments
 (0)