This project demonstrates deploying a FastAPI application onto an Azure Kubernetes Cluster. Azure Pipelines have been created to build and push the service docker image to an Azure Container Registry. And also deploy the service to a Kubernetes cluster. Azure resources created have been written in Terraform can be found in Azure DevOps repository here 1.
Note
This repository was original created within Azure DevOps and is now being mirrored to this GitHub repository. Source of truth will always be the Azure DevOps repository.
-
Install python packages used for the service
pip install - requirements.txt
-
Run the FastAPI server, which will run on port 8000
python app/main.py
Endpoint documentation are available on http://127.0.0.1:8000/docs
Running the docker-compose.yml
, will build a new image python-fastapi-azure-k8s-cluster-fastapi:latest which will be used for the fastapi
service
within the container.
docker-compose up -d
Unit tests are located in /tests
directory.
pytest tests/
This project has two pipelines see files:
/azure-pipelines-docker-k8s-deploy.yml
/azure-pipelines-run-unit-tests.yml
The tables below detail pipeline variables required, most of the values required for deploying FastAPI to Azure Kubernetes cluster, can be found with the terraform repository here. Additionally, as the orginasation / projected created, within Azure DevOps is private, these pipelines have been written to run on a self-host build agent so YMMV.
Variable | Description | Default value | Required? |
---|---|---|---|
projectAzureSubscriptionConnection | The Azure Resource Manager service connection ID | N/A | Yes |
projectAzureResourceGroup | The Azure resource group were the cluster exists in | N/A | Yes |
projectKubernetesCluster | The Azure kubernetes cluster name | N/A | Yes |
projectImagePullSecret | The Kubernetes secret name to be generated / used for pulling images from ACR | N/A | Yes |
k8sNamespace | The Kubernetes name space to deploy the service | default | No |
projectPoolName | The Azure agent pool that the job will run on | N/A | Yes |
projectContainerRegistry | The Azure container registry login server | N/A | Yes |
projectDockerRegistryServiceConnection | The service connection with Docker Registry (using basic authentication) | N/A | Yes |
projectImageRepository | The docker image name to push to registry | N/A | Yes |
Variable | Description | Default value | Required? |
---|---|---|---|
projectPoolName | The Azure agent pool that the job will run on | N/A | Yes |