|
| 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. |
0 commit comments