Skip to content

Latest commit

 

History

History
76 lines (52 loc) · 5.73 KB

File metadata and controls

76 lines (52 loc) · 5.73 KB

Vacation Planner: Azure SQL Database

A .NET version of this sample lives in ../dotnet.

This sample demonstrates a Python Flask single-page web application called Vacation Planner hosted on an Azure Kubernetes Service (AKS) cluster in the cloud on Azure or locally in the LocalStack emulator for Azure. The app runs in a dedicated namespace and stores activity data in the dbo.Activities table of the PlannerDB database on an Azure SQL Database.

The application connects to Azure SQL using a dedicated SQL login (rather than the server admin), and the deployment scripts seed the Activities table with a handful of sample plans so the app shows data on first load.

Before installing the sample, make sure to create an Azure Kubernetes Service (AKS) cluster by using one of the following scripts:

All commands below are run from this sample's scripts/ folder.

Running on LocalStack? Install the lstk CLI and run lstk az start-interception to route Azure CLI calls to the emulator. See Run against LocalStack for the full setup.

Architecture

The following diagram illustrates the architecture of the solution:

Architecture Diagram

Deployment workflow

Run the numbered scripts in order from the scripts/ folder:

cd scripts
./01-deploy-resources.sh
./02-build-docker-image.sh
./03-run-docker-container.sh   # optional local smoke test
./04-push-docker-image.sh
./05-deploy-app.sh

Scripts and manifests

File Description
00-variables.sh Defines the variables shared across the other scripts (resource names, image tag, SQL credentials, Kubernetes namespace, …). The other scripts load these values by sourcing this file.
01-deploy-resources.sh Deploys the Azure resources used by this sample: the resource group, the Azure Container Registry (ACR), the Azure SQL Database logical server and database, a permissive firewall rule (dev/test only), a dedicated SQL login and database user with the appropriate roles, and the Activities table, which it also seeds with sample data. Requires sqlcmd on the host.
02-build-docker-image.sh Builds the Docker image for the web app from the src/ folder.
03-run-docker-container.sh Runs the web app in a local Docker container (no Kubernetes) to validate that it starts and connects to the database as expected.
04-push-docker-image.sh Tags and pushes the Docker image to the Azure Container Registry, on Azure or in the LocalStack emulator.
05-deploy-app.sh Uses the YAML manifests below (templated with yq) to deploy the app to the AKS cluster.
Dockerfile Builds the Docker image of the web app.
namespace.yml Creates the Kubernetes namespace.
configmap.yml Creates the ConfigMap holding non-secret input values (SQL server FQDN, database, username, login name) passed to the app as environment variables.
secret.yml Creates the Secret holding sensitive values (the SQL password and the Flask secret key) passed to the app as environment variables.
deployment.yml Creates the Kubernetes Deployment, including the pod specification for the web app. The liveness and readiness probes call GET /health.
service.yml Creates the ClusterIP Service that exposes the web app inside the cluster.

Accessing the web app

The app is exposed through a ClusterIP service, which is only reachable from inside the cluster. Port-forward it to a local port to open it from your machine:

kubectl port-forward service/vacation-planner-sql 8080:80 -n vacation-planner-sql

Then browse to http://localhost:8080. Alternatively, use a tool such as k9s to start the port-forward interactively.

The app also exposes GET /health, the endpoint the liveness and readiness probes call: it returns {"status": "ok"} when the Azure SQL database is reachable and 503 with {"status": "unavailable"} otherwise.

curl http://localhost:8080/health

Logs

The app logs one line per request — gunicorn writes an access log line for every call, the probes included, because its command passes --access-logfile - — plus one line per database read and write and one line for every activity added, updated or deleted. Every entry carries a timestamp, the logger name and the level; the Azure SDK and urllib3 stay at warning level. The .NET version writes the same trace.

kubectl logs deployment/vacation-planner-sql -n vacation-planner-sql --tail=50