|
| 1 | +--- |
| 2 | +title: Deploy a Highly Available PostgreSQL Database on AKS using CloudNativePG Operator |
| 3 | +description: This Exec Doc demonstrates how to deploy a highly available PostgreSQL database on an Azure Kubernetes Service (AKS) cluster using the CloudNativePG operator. It covers creating the necessary Azure resources, installing the operator via Helm, and deploying a multi-instance PostgreSQL cluster. |
| 4 | +ms.topic: quickstart |
| 5 | +ms.date: 10/12/2023 |
| 6 | +author: yourgithubusername |
| 7 | +ms.author: youralias |
| 8 | +ms.custom: innovation-engine, akshighavailability, cloudnativepg |
| 9 | +--- |
| 10 | + |
| 11 | +# Deploy a Highly Available PostgreSQL Database on AKS using CloudNativePG Operator |
| 12 | + |
| 13 | +This document guides you through deploying a highly available PostgreSQL database on an AKS cluster using the CloudNativePG operator. You will create an Azure resource group and an AKS cluster with a random suffix for uniqueness, install the CloudNativePG operator using Helm, and then deploy a PostgreSQL cluster configured for high availability. |
| 14 | + |
| 15 | +The following steps include environment variable declarations, Azure CLI commands, and Kubernetes commands executed via bash code blocks. Each code block includes an accompanying result block to verify that the commands execute with the expected output. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Step 1: Create an Azure Resource Group |
| 20 | + |
| 21 | +In this section, we declare environment variables for the deployment. The resource group name will have a random suffix appended to ensure uniqueness. We then create the resource group in the designated region (WestUS2). |
| 22 | + |
| 23 | +```bash |
| 24 | +export RANDOM_SUFFIX=$(openssl rand -hex 3) |
| 25 | +export REGION="WestUS2" |
| 26 | +export RESOURCE_GROUP="cnpg-rg$RANDOM_SUFFIX" |
| 27 | +az group create --name $RESOURCE_GROUP --location $REGION |
| 28 | +``` |
| 29 | + |
| 30 | +Results: |
| 31 | + |
| 32 | +<!-- expected_similarity=0.3 --> |
| 33 | + |
| 34 | +```JSON |
| 35 | +{ |
| 36 | + "id": "/subscriptions/xxxxx-xxxxx-xxxxx-xxxxx/resourceGroups/cnpg-rgxxxxxxxxx", |
| 37 | + "location": "WestUS2", |
| 38 | + "name": "cnpg-rgxxxxxxxxx", |
| 39 | + "properties": { |
| 40 | + "provisioningState": "Succeeded" |
| 41 | + }, |
| 42 | + "tags": {} |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Step 2: Create an AKS Cluster |
| 49 | + |
| 50 | +Now we create an AKS cluster in the resource group. The cluster name is also appended with a random suffix. This cluster will have 3 nodes to support deployment of a highly available PostgreSQL database. |
| 51 | + |
| 52 | +```bash |
| 53 | +export AKS_CLUSTER="cnpg-aks$RANDOM_SUFFIX" |
| 54 | +az aks create --resource-group $RESOURCE_GROUP --name $AKS_CLUSTER --node-count 3 --enable-addons monitoring --generate-ssh-keys --location $REGION |
| 55 | +``` |
| 56 | + |
| 57 | +Results: |
| 58 | + |
| 59 | +<!-- expected_similarity=0.3 --> |
| 60 | + |
| 61 | +```JSON |
| 62 | +{ |
| 63 | + "fqdn": "cnpg-aksxxxxxxxxx.hcp.westus2.azmk8s.io", |
| 64 | + "id": "/subscriptions/xxxxx-xxxxx-xxxxx-xxxxx/resourceGroups/cnpg-rgxxxxxxxxx/providers/Microsoft.ContainerService/managedClusters/cnpg-aksxxxxxxxxx", |
| 65 | + "location": "WestUS2", |
| 66 | + "name": "cnpg-aksxxxxxxxxx", |
| 67 | + "provisioningState": "Succeeded", |
| 68 | + "tags": {} |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +After creating the cluster, download its credentials so that kubectl can interact with it: |
| 73 | + |
| 74 | +```bash |
| 75 | +az aks get-credentials --resource-group $RESOURCE_GROUP --name $AKS_CLUSTER |
| 76 | +``` |
| 77 | + |
| 78 | +Results: |
| 79 | + |
| 80 | +<!-- expected_similarity=0.3 --> |
| 81 | + |
| 82 | +```console |
| 83 | +Merged "cnpg-aksxxxxxxxxx" as current context in /home/xxxxx/.kube/config |
| 84 | +``` |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## Step 3: Install the CloudNativePG Operator |
| 89 | + |
| 90 | +The CloudNativePG operator is installed via Helm. This section adds the CloudNativePG Helm repository and deploys the operator into its own namespace (cnpg-system). |
| 91 | + |
| 92 | +```bash |
| 93 | +helm repo add cloudnative-pg https://cloudnative-pg.io/charts |
| 94 | +helm repo update |
| 95 | +helm install cnpg cloudnative-pg/cnpg --namespace cnpg-system --create-namespace |
| 96 | +``` |
| 97 | + |
| 98 | +Results: |
| 99 | + |
| 100 | +<!-- expected_similarity=0.3 --> |
| 101 | + |
| 102 | +```console |
| 103 | +NAME: cnpg |
| 104 | +LAST DEPLOYED: Wed Oct 11 2023 12:34:56 PM |
| 105 | +NAMESPACE: cnpg-system |
| 106 | +STATUS: deployed |
| 107 | +REVISION: 1 |
| 108 | +``` |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +## Step 4: Deploy a Highly Available PostgreSQL Cluster |
| 113 | + |
| 114 | +In this step, you'll deploy a PostgreSQL cluster using CloudNativePG. The configuration specifies three instances to achieve high availability, and a minimal storage allocation is used for demonstration purposes. |
| 115 | + |
| 116 | +First, create the PostgreSQL cluster manifest file named "ha-postgresql.yaml". This file should reside in the same folder as this Exec Doc. |
| 117 | + |
| 118 | +```bash |
| 119 | +cat << 'EOF' > ha-postgresql.yaml |
| 120 | +apiVersion: postgresql.cnpg.io/v1 |
| 121 | +kind: Cluster |
| 122 | +metadata: |
| 123 | + name: ha-postgres |
| 124 | +spec: |
| 125 | + instances: 3 |
| 126 | + storage: |
| 127 | + size: 1Gi |
| 128 | + postgresVersion: 14 |
| 129 | +EOF |
| 130 | +``` |
| 131 | + |
| 132 | +Results: |
| 133 | + |
| 134 | +<!-- expected_similarity=0.3 --> |
| 135 | + |
| 136 | +```console |
| 137 | +ha-postgresql.yaml created |
| 138 | +``` |
| 139 | + |
| 140 | +Now, apply the YAML file to deploy the PostgreSQL cluster. |
| 141 | + |
| 142 | +```bash |
| 143 | +kubectl apply -f ha-postgresql.yaml |
| 144 | +``` |
| 145 | + |
| 146 | +Results: |
| 147 | + |
| 148 | +<!-- expected_similarity=0.3 --> |
| 149 | + |
| 150 | +```console |
| 151 | +cluster.postgresql.cnpg.io/ha-postgres created |
| 152 | +``` |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +In this Exec Doc, you've created an Azure resource group and an AKS cluster, installed the CloudNativePG operator using Helm, and deployed a highly available PostgreSQL database on the cluster using a custom YAML manifest. This automated, one-click deployment is repeatable and ensures that the resources are unique for every run. |
0 commit comments