A boilerplate application to start developing microservices in Python.
To run the tests, use the following command:
pytestTo run the tests with coverage, use the following command:
pytest --cov=app --cov-report=term-missingTo build the Dockerfile locally, use the following commands:
docker build . -t [image-name]:[tag] -f /path/to/Dockerfiledocker run -d tagThen open a browser and navigate to http://localhost:8000. If it opens "Hello world", then it works.
Two workflows are available in the project. One triggers on pull requests, and another triggers on merges. A workflow that triggers on every push is not created, as it may consume excessive resources and exceed the budget.
To run the fastapi_pr.yml pipeline, a pull request should be made to the main branch on GitHub. This will trigger the pipeline, which can be monitored in the GitHub -> Actions tab. The workflow includes the following checks:
- GitLeaks: Scans for sensitive information in the codebase.
- EditorConfig: Ensures consistent coding styles.
- Python PyLint: Checks for Python code quality.
- Python Black: Formats Python code to adhere to PEP 8 standards.
- MarkdownLint CLI: Validates Markdown files for proper formatting.
- Unit Tests: Runs the test suite to ensure code correctness.
To run the fastapi_merge.yml workflow, a pull request should be merged into the main branch on GitHub. This will trigger the pipeline, which can also be monitored in the GitHub -> Actions tab. The workflow includes the following steps:
- SonarCloud: Performs static code analysis for code quality and security.
- Snyk: Scans for vulnerabilities in dependencies.
- Trivy: Scans Docker images for vulnerabilities.
- Docker Image Build and Push: Builds the Docker image and pushes it to Docker Hub.
- Installed kubectl command-line tool.
- Installed Docker Desktop or another external Kubernetes cluster.
To install ArgoCD, create a namespace for it:
kubectl create namespace argocdThen apply the installation manifest:
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yamlbrew install argocdTo access the ArgoCD UI, forward the port:
kubectl port-forward svc/argocd-server -n argocd 8080:443Navigate to https://localhost:8080 in your browser. The certificate will be invalid, but you can proceed. Use the following credentials:
- Username:
admin - Temporary Password: Extract it using the command below (exclude the
%symbol at the end):
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -dUpdate the password:
argocd account update-password- Log in to the ArgoCD UI.
- Click the '+ New App' button.
- Provide the following details:
- Application Name:
app - Project:
default - Sync Policy:
Manual - Repository URL:
https://github.com/boyanaboneva/devops-gitops - Revision:
HEAD - Path:
devops-service-deployment - Destination Cluster:
https://kubernetes.default.svc - Namespace:
application
- Application Name:
- Click 'Create'.
If the application's health status is "Out of sync," click the 'Sync apps' button in the UI. Then, click 'Synchronize' to deploy the application. You can view more details by clicking on the application.
The deployment files are stored in a separate GitOps repository: https://github.com/boyanaboneva/devops-gitops.
Create a namespace for the application:
kubectl create namespace applicationNavigate to the GitOps repository and apply the environment variables ConfigMap:
kubectl apply -f env_fastapi_configmap.ymlNavigate to the Kubernetes deployment folder in the GitOps repository and apply the deployment manifest:
kubectl apply -f deployment.yml -n applicationExpected response:
deployment.apps/fast-api-deployment createdCheck the pods in the namespace:
kubectl get pods -n applicationExpected response:
NAME READY STATUS RESTARTS AGE
fast-api-deployment-66ff55bb64-cmkbl 1/1 Running 0 22sApply the service manifest:
kubectl apply -f service.yml -n applicationExpected response:
service/fastapi-service configuredThis project follows a modern DevOps architecture that integrates GitHub, GitHub Actions, ArgoCD, Docker Hub, and Kubernetes. Below is a detailed explanation of the architecture:
- GitHub: Hosts the application code and a separate GitOps repository for Kubernetes manifests.
- GitHub Actions: Automates CI/CD workflows, including building, testing, and pushing Docker images.
- Docker Hub: Stores Docker images for deployment.
- Kubernetes: Orchestrates containerized applications, ensuring scalability and reliability.
- ArgoCD: Implements GitOps principles by syncing Kubernetes manifests from the GitOps repository to the cluster.
- Developers push code changes to GitHub.
- GitHub Actions builds the application, runs tests, and pushes Docker images to Docker Hub.
- The GitOps repository is updated with the new image tag in the Kubernetes manifests.
- ArgoCD detects changes in the GitOps repository and syncs them to the Kubernetes cluster.
This architecture ensures a seamless CI/CD pipeline with automated deployments, version control, and rollback capabilities.
