LoginSignup
3
2

More than 3 years have passed since last update.

高可用性・耐障害性のデータベースを構築(Azure+PostgreSQL)

Last updated at Posted at 2021-01-31

Azureリージョン(東日本)の可用性ゾーンにて高可用性データベース(PostgreSQL)を構築

AKS上にCrunchy PostgreSQL Operatorを構築する

システムイメージ

【アクティブ/スタンバイ構成】
システムは通常時に使用する稼働系(アクティブ)と通常時は使わずに待機している待機系(スタンドバイ)で構成され、稼動系に障害が発生して処理の続行が不可能になると、直ちに待機系が起動する。
Azure可用性ゾーンSAL:99.99
image.png

メリット

バックアップ・リカバリの自動化
負荷に応じて、レプリカの台数を増減可能
Namespace により、PostgreSQL クラスタをサービスごとに隔離可能 (Multi-Tenant)
高可用性・耐障害性の向上 (Multi-Cluster、Multi-Zone)
High Availability & Automated Failover Backup
Restore and Disaster Recovery
Geospatial Data Management
Integrated Administration Dashboard
Database Monitoring
image.png

事前準備

クライアントにインストールする必要なライブライブ
1.azure-cli
2.jq
3.libpq

macのインストールコマンド

brew update && brew install azure-cli
brew install jq
brew install libpq
brew link --force libpq

デプロイ手順

(1)AKSのデプロイ

詳細説明について、下記のリンクを参照
https://docs.microsoft.com/ja-jp/azure/aks/availability-zones

①リソースグループ作成
az group create --name rg_aks --location japaneast
②複数の可用性ゾーンでの AKS クラスターの作成

az aks create \
    --resource-group rg_aks \
    --name aksClusterPG \
    --generate-ssh-keys \
    --vm-set-type VirtualMachineScaleSets \
    --load-balancer-sku standard \
    --node-count 3 \
    --zones 1 2 3

③複数のゾーンへのノードの分散を確認する

az aks get-credentials --resource-group rg_esd_west_aks --name aksClusterPG

kubectl describe nodes | grep -e "Name:" -e "failure-domain.beta.kubernetes.io/zone"

image.png

kubectl get nodes -o custom-columns=NAME:'{.metadata.name}',REGION:'{.metadata.labels.topology\.kubernetes\.io/region}',ZONE:'{metadata.labels.topology\.kubernetes\.io/zone}'

image.png

(2)Crunchy PostgreSQL Operatorのインストール

補足:v4.5.1はMACで動かなかったので、v4.4.1を使用

kubectl create namespace pgo
kubectl create -f https://raw.githubusercontent.com/CrunchyData/postgres-operator/v4.4.1/installers/kubectl/postgres-operator.yml
kubectl wait --for=condition=complete --timeout=180s -n pgo job/pgo-deploy

curl https://raw.githubusercontent.com/CrunchyData/postgres-operator/master/installers/kubectl/client-setup.sh > client-setup.sh
chmod +x client-setup.sh
./client-setup.sh

cat <<EOF >> ~/.bashrc
export PGOUSER="${HOME?}/.pgo/pgo/pgouser"
export PGO_CA_CERT="${HOME?}/.pgo/pgo/client.crt"
export PGO_CLIENT_CERT="${HOME?}/.pgo/pgo/client.crt"
export PGO_CLIENT_KEY="${HOME?}/.pgo/pgo/client.key"
export PGO_APISERVER_URL='https://127.0.0.1:8443'
export PGO_NAMESPACE=pgo
EOF

source ~/.bashrc

備考:他のシェールを使っている場合、例)ZSH、bashrcをzshrcを変更してください

デプロイメントの確認

kubectl -n pgo get deployments

NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
postgres-operator             1/1     1            1           5m

違うタブ/ウインドウで下記コマンドを実行

kubectl -n pgo port-forward svc/postgres-operator 8443:8443

元タブ/ウインドウで下記コマンドを実行

pgo version

image.png

(3)データベースクラスター作成

違うタブ/ウインドウで下記コマンドを実行

kubectl -n pgo port-forward svc/postgres-operator 8443:8443

元タブ/ウインドウで下記コマンドを実行

pgo create cluster iot-db \
  --service-type=LoadBalancer \
  --replica-count 3 \
  --pvc-size 100.10Gi

備考:クラスター名、レプリカ数、サイズを必要に応じて変更してください
※完了するまでに少し時間かかる

kubectl -n pgo get svc
NAME                          TYPE           CLUSTER-IP    EXTERNAL-IP     PORT(S)                         AGE
iot-db                        LoadBalancer   10.0.194.127   20.48.81.148   2022:32095/TCP,5432:30140/TCP   77s
iot-db-backrest-shared-repo   ClusterIP      10.0.48.50     <none>         2022/TCP                        77s
postgres-operator             ClusterIP      10.0.220.87    <none>         8443/TCP,4171/TCP,4150/TCP      15h

20.48.81.148がクライアントからアクセスするためのパブリックIPアドレスです。
元タブ/ウインドウで下記コマンドを実行してデータベースユーザー情報を取得する。

pgo show user -n pgo iot-db --show-system-accounts

CLUSTER USERNAME    PASSWORD                 EXPIRES STATUS ERROR 
------- ----------- ------------------------ ------- ------ -----
iot-db  crunchyadm                           never   ok           
iot-db  postgres    xxxxxxxxxxxxxxxxxxxxxxxx never   ok           
iot-db  primaryuser xxxxxxxxxxxxxxxxxxxxxxxx never   ok           
iot-db  testuser    xxxxxxxxxxxxxxxxxxxxxxxx never   ok  

接続テスト

psql -h 20.48.81.148 -U testuser iot-db
xxxxxxxxxxxxxxxxxxxxxxxx

image.png


下記のコマンドでデータベースのインスタンスが
3つのゾーンに展開されていることを確認できる

kubectl describe pod -n pgo| grep -e "^Name:" -e "^Node:"

Name:               backrest-backup-hippo-vk6ng
Node:               aks-nodepool1-18262813-vmss000001/10.240.0.5
Name:               backrest-backup-iot-db-bttmp
Node:               aks-nodepool1-18262813-vmss000001/10.240.0.5
Name:               hippo-68d4998d46-d5nqf
Node:               aks-nodepool1-18262813-vmss000000/10.240.0.4
Name:               hippo-backrest-shared-repo-76d8bf98b4-2dmrc
Name:               backrest-backup-iot-db-8qz6v
Node:               aks-nodepool1-18262813-vmss000001/10.240.0.5
Name:               iot-db-5dc7cfcf99-cq5h7
Node:               aks-nodepool1-18262813-vmss000002/10.240.0.6
Name:               iot-db-backrest-shared-repo-598f765bc5-h6pjh
Node:               aks-nodepool1-18262813-vmss000001/10.240.0.5
Name:               iot-db-hbvz-59df58c58c-krnw4
Node:               aks-nodepool1-18262813-vmss000001/10.240.0.5
Name:               iot-db-kioo-7b79cbbd59-q2g77
Node:               aks-nodepool1-18262813-vmss000000/10.240.0.4
Name:               iot-db-seee-77f788fc9f-jhcc9
Node:               aks-nodepool1-18262813-vmss000002/10.240.0.6
Name:               iot-db-stanza-create-vm2jm
Node:               aks-nodepool1-18262813-vmss000001/10.240.0.5
Name:               pgo-deploy-p47hz
Node:               aks-nodepool1-18262813-vmss000000/10.240.0.4
Name:               pgo-metrics-deploy-wxntk
Node:               aks-nodepool1-18262813-vmss000001/10.240.0.5
Name:               postgres-operator-689598d795-w4qcm
Node:               aks-nodepool1-18262813-vmss000000/10.240.0.4

おまけに

pgadminをインストールする場合
元タブに下記コマンドを実行する

pgo create pgadmin -n pgo iot-db

別タブに下記コマンドを実行

kubectl -n pgo port-forward svc/iot-db-pgadmin 5050:5050

ブラウザに「http://localhost:5050」にアクセスして、testuserでログインする。
image.png

モニタリングについて

Azureモニタが簡単
クラスター:
クラスター
ノード:
ノード
コントローラー:
コントローラー
コンテナー:
コンテナー

メトリクスもインストールできる
クラスター作成時に「--metrics」フラグをつけて、
別タブで下記コマンドを実行

kubectl -n pgo port-forward svc/crunchy-grafana 3000:3000

ブラウザに「http://localhost:3000」にアクセスして、admin/adminでログインする。
ダッシュボードウィザードを使って、PostgreSQLを追加する。

image.png

■大きいデータベースを作成してテストする場合
https://wiki.postgresql.org/wiki/Sample_Databases
https://postgrespro.com/docs/postgrespro/10/demodb-bookings-installation.html

リソース:
PGO QuickStart: https://access.crunchydata.com/documentation/postgres-operator/4.5.1/quickstart/
Installation (general steps): https://crunchydata.github.io/postgres-operator/3.5.1/installation/
Connecting to a cluster: https://access.crunchydata.com/documentation/postgres-operator/4.5.1/tutorial/connect-cluster/

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2