|
1 |
| -# Demo Devops Python |
2 |
| - |
3 |
| -This is a simple application to be used in the technical test of DevOps. |
4 |
| - |
5 |
| -## Getting Started |
6 |
| - |
7 |
| -### Prerequisites |
8 |
| - |
9 |
| -- Python 3.11.3 |
10 |
| - |
11 |
| -### Installation |
12 |
| - |
13 |
| -Clone this repo. |
14 |
| - |
15 |
| -```bash |
16 |
| -git clone https://bitbucket.org/devsu/demo-devops-python.git |
17 |
| -``` |
18 |
| - |
19 |
| -Install dependencies. |
20 |
| - |
21 |
| -```bash |
22 |
| -pip install -r requirements.txt |
23 |
| -``` |
24 |
| - |
25 |
| -Migrate database |
26 |
| - |
27 |
| -```bash |
28 |
| -py manage.py makemigrations |
29 |
| -py manage.py migrate |
30 |
| -``` |
31 |
| - |
32 |
| -### Database |
33 |
| - |
34 |
| -The database is generated as a file in the main path when the project is first run, and its name is `db.sqlite3`. |
35 |
| - |
36 |
| -Consider giving access permissions to the file for proper functioning. |
37 |
| - |
38 |
| -## Usage |
39 |
| - |
40 |
| -To run tests you can use this command. |
41 |
| - |
42 |
| -```bash |
43 |
| -py manage.py test |
44 |
| -``` |
45 |
| - |
46 |
| -To run locally the project you can use this command. |
47 |
| - |
48 |
| -```bash |
49 |
| -py manage.py runserver |
50 |
| -``` |
51 |
| - |
52 |
| -Open http://localhost:8000/api/ with your browser to see the result. |
53 |
| - |
54 |
| -### Features |
55 |
| - |
56 |
| -These services can perform, |
57 |
| - |
58 |
| -#### Create User |
59 |
| - |
60 |
| -To create a user, the endpoint **/api/users/** must be consumed with the following parameters: |
61 |
| - |
62 |
| -```bash |
63 |
| - Method: POST |
64 |
| -``` |
65 |
| - |
66 |
| -```json |
67 |
| -{ |
68 |
| - "dni": "dni", |
69 |
| - "name": "name" |
70 |
| -} |
71 |
| -``` |
72 |
| - |
73 |
| -If the response is successful, the service will return an HTTP Status 200 and a message with the following structure: |
74 |
| - |
75 |
| -```json |
76 |
| -{ |
77 |
| - "id": 1, |
78 |
| - "dni": "dni", |
79 |
| - "name": "name" |
80 |
| -} |
81 |
| -``` |
82 |
| - |
83 |
| -If the response is unsuccessful, we will receive status 400 and the following message: |
84 |
| - |
85 |
| -```json |
86 |
| -{ |
87 |
| - "detail": "error" |
88 |
| -} |
89 |
| -``` |
90 |
| - |
91 |
| -#### Get Users |
92 |
| - |
93 |
| -To get all users, the endpoint **/api/users** must be consumed with the following parameters: |
94 |
| - |
95 |
| -```bash |
96 |
| - Method: GET |
97 |
| -``` |
98 |
| - |
99 |
| -If the response is successful, the service will return an HTTP Status 200 and a message with the following structure: |
100 |
| - |
101 |
| -```json |
102 |
| -[ |
103 |
| - { |
104 |
| - "id": 1, |
105 |
| - "dni": "dni", |
106 |
| - "name": "name" |
107 |
| - } |
108 |
| -] |
109 |
| -``` |
110 |
| - |
111 |
| -#### Get User |
112 |
| - |
113 |
| -To get an user, the endpoint **/api/users/<id>** must be consumed with the following parameters: |
114 |
| - |
115 |
| -```bash |
116 |
| - Method: GET |
117 |
| -``` |
118 |
| - |
119 |
| -If the response is successful, the service will return an HTTP Status 200 and a message with the following structure: |
120 |
| - |
121 |
| -```json |
122 |
| -{ |
123 |
| - "id": 1, |
124 |
| - "dni": "dni", |
125 |
| - "name": "name" |
126 |
| -} |
127 |
| -``` |
128 |
| - |
129 |
| -If the user id does not exist, we will receive status 404 and the following message: |
130 |
| - |
131 |
| -```json |
132 |
| -{ |
133 |
| - "detail": "Not found." |
134 |
| -} |
135 |
| -``` |
| 1 | +# Demo DevOps Python Application |
| 2 | + |
| 3 | +This repository contains a simple Django application that demonstrates various DevOps practices. The application is deployed on a Kubernetes cluster using Helm and GitHub Actions for CI/CD pipelines. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | +- [Architecture](#architecture) |
| 7 | +- [Tools](#Tools) |
| 8 | +- [Setup Instructions](#setup-instructions) |
| 9 | +- [CI/CD Pipelines](#ci-cd-pipelines) |
| 10 | +- [Kubernetes Deployment](#kubernetes-deployment) |
| 11 | +- [Contributing](#contributing) |
| 12 | +- [License](#license) |
| 13 | + |
| 14 | +## Architecture |
| 15 | + |
| 16 | +The Django API is deployed on a DigitalOcean Kubernetes cluster with the following components: |
| 17 | +- **Deployment**: The API is deployed with 2 replicas, managed by a Horizontal Pod Autoscaler (HPA) that can scale up to 3 replicas. |
| 18 | +- **Service Account**: A basic service account is used to follow security standards. |
| 19 | +- **LoadBalancer**: Exposes the API to the world, accessible via the domain [`api.algova.dev`](https://api.algova.dev). It may take about 1 minute. |
| 20 | +- **DNS and SSL**: Managed using Route53 and Let's Encrypt. The domain [`api.algova.dev`](https://api.algova.dev) points to the LoadBalancer with automatic SSL certificate renewal. |
| 21 | +- **Static Files**: Served from an S3 bucket with proper ACL configurations. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## Tools |
| 26 | +The following tools were used to develop this project: |
| 27 | +- Django: Python library used to power the backend. |
| 28 | +- Helm: Used to manage Kubernetes deployments in an organized and clean manner. |
| 29 | +- Kubernetes: Where our API is deployed, providing scalability and security. |
| 30 | +- Docker: Our API is containerized for easy deployment in any environment. |
| 31 | +- Github Actions: Used to deploy our API and to test our PRs before merging. |
| 32 | +- Pylint: Keeps our code following high standards. |
| 33 | +- Pytest: Ensures proper testing of our API to avoid downtimes. |
| 34 | +- Trivy: Keeps us alert to vulnerabilities in our Docker images. |
| 35 | + |
| 36 | +## Setup Instructions |
| 37 | +The project was divided according to the task to be performed: |
| 38 | +**In all cases, we must clone the repo** |
| 39 | + ```bash |
| 40 | + git clone https://github.com/Adriana618/devops-deemo.git |
| 41 | + cd devops-deemo |
| 42 | + ``` |
| 43 | +# Develop: |
| 44 | +You must have Minikube, Docker, and Helm installed. |
| 45 | + ```bash |
| 46 | + sudo apt-get update |
| 47 | + sudo apt-get install -y python3 python3-pip docker.io kubectl minikube |
| 48 | + pip3 install -r requirements/dev.txt |
| 49 | + ``` |
| 50 | +# Test: |
| 51 | +You must have Minikube, Docker, and Helm installed. |
| 52 | + ```bash |
| 53 | + sudo apt-get update |
| 54 | + sudo apt-get install -y python3 python3-pip docker.io kubectl minikube |
| 55 | + pip3 install -r requirements/test.txt |
| 56 | + ``` |
| 57 | +# Deploy: |
| 58 | +You must have a Kubernetes cluster with 2 nodes with 1CPU and 2GB of memory at a minimum. |
| 59 | + |
| 60 | +After having the necessary setup for each stage, proceed to fill in the Helm values.yaml file or overwrite the variables at the time of installing the app. |
| 61 | +- DJANGO_SECRET_KEY |
| 62 | +- DATABASE_NAME='db.sqlite3' |
| 63 | +- DJANGO_ALLOWED_HOSTS |
| 64 | +- AWS_STORAGE_BUCKET_NAME |
| 65 | +- AWS_ACCESS_KEY_ID |
| 66 | +- AWS_SECRET_ACCESS_KEY |
| 67 | +- AWS_DEFAULT_REGION |
| 68 | + |
| 69 | +2. **Dockerize the Application**: |
| 70 | + - Dockerfile located at <a href="https://github.com/Adriana618/devops-deemo/blob/master/docker-conf/Dockerfile-django-api" target="_blank">docker-conf/Dockerfile-django-api</a> |
| 71 | + |
| 72 | +## CI/CD Pipelines |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +### Deploy API |
| 77 | +This GitHub Action deploys commits merged into the `master` branch to a Kubernetes cluster on DigitalOcean. |
| 78 | + |
| 79 | +See the file <a href="https://github.com/Adriana618/devops-deemo/blob/master/.github/workflows/deploy-api.yml" target="_blank">.github/workflows/deploy-api.yml</a> |
| 80 | + |
| 81 | +### Test PR |
| 82 | +This GitHub Action tests pull requests by running static code analysis, code coverage, and vulnerability scans. |
| 83 | + |
| 84 | +See the file <a href="https://github.com/Adriana618/devops-deemo/blob/master/.github/workflows/deploy-pr.yml" target="_blank">.github/workflows/deploy-pr.yml</a> |
| 85 | + |
| 86 | +## Kubernetes Deployment |
| 87 | + |
| 88 | +### Deployment Configuration |
| 89 | +See the file <a href="https://github.com/Adriana618/devops-deemo/blob/master/devops-deemo/templates/api/api-deployment.yaml" target="_blank">devops-deemo/templates/api/api-deployment.yaml</a> |
| 90 | + |
| 91 | +### Service Configuration |
| 92 | +See the file <a href="https://github.com/Adriana618/devops-deemo/blob/master/devops-deemo/templates/api/api-services.yaml" target="_blank">devops-deemo/templates/api/api-services.yaml</a> |
| 93 | + |
| 94 | +### Secrets |
| 95 | +See the files <a href="https://github.com/Adriana618/devops-deemo/blob/master/devops-deemo/templates/api/api-secrets.yaml" target="_blank">devops-deemo/templates/api/api-secrets.yaml</a> |
| 96 | + |
| 97 | +## Contributing |
| 98 | + |
| 99 | +Please follow the <a href="https://github.com/Adriana618/devops-deemo/blob/master/CONTRIBUTING.md" target="_blank">contribution guidelines</a> to propose improvements or report issues. |
136 | 100 |
|
137 | 101 | ## License
|
138 | 102 |
|
139 |
| -Copyright © 2023 Devsu. All rights reserved. |
| 103 | +This project is licensed under the MIT License - see the <a href="https://github.com/Adriana618/devops-deemo/blob/master/LICENSE" target="_blank">LICENSE</a> file for details. |
0 commit comments