|
| 1 | +# Azure PostgreSQL Votes Demo App |
| 2 | + |
| 3 | +## About this demo |
| 4 | +In this demo, we will walk through creating an Azure Postgres Flexible Server and Postgres database. We will create a simple |
| 5 | +application which records votes and stores them in the database. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +1. An Azure subscription to create Azure resources under. |
| 10 | +2. A Kubernetes cluster (at least version 1.21) [created and running](https://kubernetes.io/docs/tutorials/kubernetes-basics/create-cluster/), |
| 11 | + and [`kubectl`](https://kubernetes.io/docs/tasks/tools/#kubectl) configured to talk to it. (You can check your cluster |
| 12 | + version with `kubectl version`.) This could be a local [Kind cluster](https://kind.sigs.k8s.io/docs/user/quick-start/) |
| 13 | + or an [Azure Kubernetes Service cluster](https://docs.microsoft.com/en-us/azure/aks/tutorial-kubernetes-deploy-cluster) |
| 14 | + running in your subscription. |
| 15 | +3. Azure Service Operator set up and running in your cluster of choice. |
| 16 | + Follow the [ASO v2 installation instructions](https://github.com/Azure/azure-service-operator/blob/master/v2/README.md#installation) and |
| 17 | + ensure that you can create and delete a simple `ResourceGroup` as shown in the |
| 18 | + [usage example](https://github.com/Azure/azure-service-operator/blob/master/v2/README.md#usage). |
| 19 | + |
| 20 | +## Demo |
| 21 | + |
| 22 | +**Step 1: Create environment variables to hold a few key values** |
| 23 | +```shell |
| 24 | +export SERVER=asodemo-postgres |
| 25 | +export USERNAME=asoadmin |
| 26 | +export PASSWORD=<yourpassword> |
| 27 | +``` |
| 28 | + |
| 29 | +**Step 2: Create your Azure Resources** |
| 30 | + |
| 31 | +We use `envsubst` here as a quick and simple way to do basic variable replacement. |
| 32 | + |
| 33 | +```shell |
| 34 | +envsubst < manifests/postgres-votes-demo.yaml | kubectl apply -f - |
| 35 | +``` |
| 36 | + |
| 37 | +This command will create a namespace, along with an Azure `ResourceGroup` along with a PostgreSQL `FlexibleServer`, `FlexibleServerDatabase` and `FlexibleServerFirewallRule`. |
| 38 | +It also creates a `Secret` for use later in binding some important connection information into our votes app. |
| 39 | + |
| 40 | +**Note:** In a future iteration of ASO, the username and password fields of the `FlexibleServer` will be specified via a linked `Secret`. See [#1471](https://github.com/Azure/azure-service-operator/issues/1471) for more details. |
| 41 | + |
| 42 | +**Step 3: Check the status of your resources and wait for them to finish provisioning** |
| 43 | +```shell |
| 44 | +watch kubectl get resourcegroups,flexibleservers,flexibleserversdatabases,flexibleserversfirewallrules -n asodemo |
| 45 | +``` |
| 46 | + |
| 47 | +It may take a few minutes for the `FlexibleServer` to successfully deploy, during which time you will see: |
| 48 | +```shell |
| 49 | +NAME READY REASON MESSAGE |
| 50 | +asodemo-postgres False Reconciling The resource is in the process of being reconciled by the operator |
| 51 | +``` |
| 52 | + |
| 53 | +The `FlexibleServersDatabase` cannot be successfully deployed until the `FlexibleServer` it resides in has finished deploying. |
| 54 | +While the `FlexibleServer` is being deployed, you will see the database is not deployed and has the following message. |
| 55 | +This is ok! Once the `FlexibleServer` has been successfully created in Azure this will resolve itself automatically. |
| 56 | + |
| 57 | +```shell |
| 58 | +NAME READY REASON MESSAGE |
| 59 | +sampledb False ResourceNotFound The specified resource asodemo-postgres was not found. |
| 60 | +``` |
| 61 | + |
| 62 | +**Step 4: Create a deployment with a single pod running the Azure PostgreSQL Votes App** |
| 63 | + |
| 64 | +```shell |
| 65 | +kubectl apply -f manifests/deploy.yaml |
| 66 | +``` |
| 67 | + |
| 68 | +Ensure that the pod is running: |
| 69 | +```shell |
| 70 | +kubectl get pods -n asodemo |
| 71 | +``` |
| 72 | + |
| 73 | +**Step 5: Port forward to the pod** |
| 74 | + |
| 75 | +```shell |
| 76 | +kubectl port-forward -n asodemo deployment/azure-votes-postgresql-deployment 8080:8080 |
| 77 | +``` |
| 78 | + |
| 79 | +**Step 6: Vote!** |
| 80 | + |
| 81 | +Visit `localhost:8080` to vote. |
| 82 | + |
| 83 | +**Step 7: (Optional) look at the data in the database** |
| 84 | + |
| 85 | +You can use the [psql command line](https://www.postgresql.org/docs/current/app-psql.html) to look at the "votes" table. |
| 86 | +```shell |
| 87 | +psql -h $SERVER.postgres.database.azure.com -p 5432 -U $USERNAME votedb |
| 88 | + |
| 89 | +SELECT * FROM votes; |
| 90 | +``` |
| 91 | + |
| 92 | +**Step 7: Delete the asodemo namespace** |
| 93 | + |
| 94 | +You don't need to delete the Azure resources individually. The Kubernetes ownership model ensures that when the namespace containing the resources is deleted, the delete is propagated to all resources. |
| 95 | + |
| 96 | +```shell |
| 97 | +kubectl delete namespace asodemo |
| 98 | +``` |
| 99 | + |
| 100 | +## Build the Docker Image |
| 101 | +We publish the docker image for you, but if you'd like to build it yourself, run the following commands. |
| 102 | + |
| 103 | +``` |
| 104 | +docker build -t your_registry.com/your_org_or_user/postgresql_azure_votes:1 |
| 105 | +docker push your_registry.com/your_org_or_user/postgresql_azure_votes:1 |
| 106 | +``` |
0 commit comments