In this demo, we will walk through creating an Azure Postgres Flexible Server and Postgres database. We will create a simple application which records votes and stores them in the database.
- An Azure subscription to create Azure resources under.
- A Kubernetes cluster (at least version 1.21) created and running,
and
kubectl
configured to talk to it. (You can check your cluster version withkubectl version
.) This could be a local Kind cluster or an Azure Kubernetes Service cluster running in your subscription. - Azure Service Operator set up and running in your cluster of choice.
Follow the ASO v2 installation instructions and
ensure that you can create and delete a simple
ResourceGroup
as shown in the usage example.
Step 1: Create environment variables to hold a few key values
export SERVER=asodemo-postgres
export USERNAME=asoadmin
export PASSWORD=<yourpassword>
Step 2: Create your Azure Resources
We use envsubst
here as a quick and simple way to do basic variable replacement.
envsubst < manifests/postgres-votes-demo.yaml | kubectl apply -f -
This command will create a namespace, along with an Azure ResourceGroup
along with a PostgreSQL FlexibleServer
, FlexibleServerDatabase
and FlexibleServerFirewallRule
.
It also creates a Secret
for use later in binding some important connection information into our votes app.
Note: In a future iteration of ASO, the username and password fields of the FlexibleServer
will be specified via a linked Secret
. See #1471 for more details.
Step 3: Check the status of your resources and wait for them to finish provisioning
watch kubectl get resourcegroups,flexibleservers,flexibleserversdatabases,flexibleserversfirewallrules -n asodemo
It may take a few minutes for the FlexibleServer
to successfully deploy, during which time you will see:
NAME READY REASON MESSAGE
asodemo-postgres False Reconciling The resource is in the process of being reconciled by the operator
The FlexibleServersDatabase
cannot be successfully deployed until the FlexibleServer
it resides in has finished deploying.
While the FlexibleServer
is being deployed, you will see the database is not deployed and has the following message.
This is ok! Once the FlexibleServer
has been successfully created in Azure this will resolve itself automatically.
NAME READY REASON MESSAGE
sampledb False ResourceNotFound The specified resource asodemo-postgres was not found.
Step 4: Create a deployment with a single pod running the Azure PostgreSQL Votes App
kubectl apply -f manifests/deploy.yaml
Ensure that the pod is running:
kubectl get pods -n asodemo
Step 5: Port forward to the pod
kubectl port-forward -n asodemo deployment/azure-votes-postgresql-deployment 8080:8080
Step 6: Vote!
Visit localhost:8080
to vote.
Step 7: (Optional) look at the data in the database
You can use the psql command line to look at the "votes" table.
psql -h $SERVER.postgres.database.azure.com -p 5432 -U $USERNAME votedb
SELECT * FROM votes;
Step 7: Delete the asodemo namespace
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.
kubectl delete namespace asodemo
We publish the docker image for you, but if you'd like to build it yourself, run the following commands.
docker build -t your_registry.com/your_org_or_user/postgresql_azure_votes:1
docker push your_registry.com/your_org_or_user/postgresql_azure_votes:1