Cloud Native PostgreSQLを設定してみよう

◆ Live配信スケジュール ◆
サイオステクノロジーでは、Microsoft MVPの武井による「わかりみの深いシリーズ」など、定期的なLive配信を行っています。
⇒ 詳細スケジュールはこちらから
⇒ 見逃してしまった方はYoutubeチャンネルをご覧ください
【5/21開催】Azure OpenAI ServiceによるRAG実装ガイドを公開しました
生成AIを活用したユースケースで最も一番熱いと言われているRAGの実装ガイドを公開しました。そのガイドの紹介をおこなうイベントです!!
https://tech-lab.connpass.com/event/315703/

 

はじめに

説明

Cloud Native PostgreSQLとはEDB社の提供しているPostgreSQLのKubernates Operatorになります。

Kubernates Operatorを初耳の方も多いと思いますが、一言で申しますとクラスタ管理・構築ツールになります。

今回は設定について解説してきます。

また、スーパーユーザの接続は前回の記事に変更しております。

構築所要時間

15分程度を想定しております。

詳細情報の確認

PostgreSQLの構築情報は下記コマンドで確認できます。

  • データベース(設定ファイル)の格納場所
  • パスワードの格納場所
  • 初期DB
  • 初期ユーザ
$ kubectl describe pods cluster-example-1  

Name:         cluster-example-1
Namespace:    default
Priority:     0
Node:         minikube/192.168.64.6
Start Time:   Fri, 28 May 2021 13:34:31 +0900
Labels:       postgresql=cluster-example
              role=primary
Annotations:  k8s.enterprisedb.io/nodeSerial: 1
              k8s.enterprisedb.io/operatorVersion: 1.4.0
Status:       Running
IP:           172.17.0.7
IPs:
  IP:           172.17.0.7
Controlled By:  Cluster/cluster-example
Init Containers:
  bootstrap-controller:
    Container ID:  docker://322ed7252fb091a9d56cd48f163a7ce10d8ca587f58953954c749852f59b6cd5
    Image:         quay.io/enterprisedb/cloud-native-postgresql:1.4.0
    Image ID:      docker-pullable://quay.io/enterprisedb/cloud-native-postgresql@sha256:5ca024b334f8a63f5a1e97503764681f158378d4b1cf338747c3bd8ed45274cb
    Port:          <none>
    Host Port:     <none>
    Command:
      /manager
      bootstrap
      /controller/manager
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Sun, 30 May 2021 14:26:44 +0900
      Finished:     Sun, 30 May 2021 14:26:46 +0900
    Ready:          True
    Restart Count:  1
    Environment:    <none>
    Mounts:
      /controller from scratch-data (rw)
      /etc/app-secret from app-secret (rw)
      /etc/superuser-secret from superuser-secret (rw)
      /run from scratch-data (rw)
      /var/lib/postgresql/data from pgdata (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from cluster-example-token-4rpd7 (ro)
Containers:
  postgres:
    Container ID:  docker://a4389055ac9115b13c3fe4bbd40edae5e46a6b1e5d836e0918d33e5b74b0029d
    Image:         quay.io/enterprisedb/postgresql:13.2
    Image ID:      docker-pullable://quay.io/enterprisedb/postgresql@sha256:5aa9a5dd0a69f9511963ed4985e1a54db16192d8061fcf713ec10b440b2d73c5
    Ports:         5432/TCP, 9187/TCP
    Host Ports:    0/TCP, 0/TCP
    Command:
      /controller/manager
      instance
      run
      --pw-file
      /etc/superuser-secret/password
    State:          Running
      Started:      Sun, 30 May 2021 14:26:48 +0900
    Last State:     Terminated
      Reason:       Error
      Exit Code:    255
      Started:      Fri, 28 May 2021 13:34:33 +0900
      Finished:     Sun, 30 May 2021 14:26:18 +0900
    Ready:          True
    Restart Count:  1
    Liveness:       http-get http://:8000/healthz delay=30s timeout=5s period=10s #success=1 #failure=3
    Readiness:      http-get http://:8000/readyz delay=0s timeout=5s period=10s #success=1 #failure=3
    Environment:
      PGDATA:        /var/lib/postgresql/data/pgdata
      POD_NAME:      cluster-example-1
      NAMESPACE:     default
      CLUSTER_NAME:  cluster-example
      PGPORT:        5432
      PGHOST:        /controller/run
    Mounts:
      /controller from scratch-data (rw)
      /etc/app-secret from app-secret (rw)
      /etc/superuser-secret from superuser-secret (rw)
      /run from scratch-data (rw)
      /var/lib/postgresql/data from pgdata (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from cluster-example-token-4rpd7 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  pgdata:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  cluster-example-1
    ReadOnly:   false
  superuser-secret:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  cluster-example-superuser
    Optional:    false
  scratch-data:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:    
    SizeLimit:  <unset>
  app-secret:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  cluster-example-app
    Optional:    false
  cluster-example-token-4rpd7:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  cluster-example-token-4rpd7
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason          Age   From     Message
  ----    ------          ----  ----     -------
  Normal  SandboxChanged  25m   kubelet  Pod sandbox changed, it will be killed and re-created.
  Normal  Pulled          25m   kubelet  Container image "quay.io/enterprisedb/cloud-native-postgresql:1.4.0" already present on machine
  Normal  Created         25m   kubelet  Created container bootstrap-controller
  Normal  Started         25m   kubelet  Started container bootstrap-controller
  Normal  Pulled          25m   kubelet  Container image "quay.io/enterprisedb/postgresql:13.2" already present on machine
  Normal  Created         25m   kubelet  Created container postgres
  Normal  Started         25m   kubelet  Started container postgres

ドキュメントでは記載箇所がわかりにくいですがsecretの設定は下記ルールがございます。

  • [cluster name]-superuser
  • [cluster name]-app

詳細設定の確認

デフォルトの設定ファイルがどうなっているかは下記サイトに記載がございますが

https://www.enterprisedb.com/docs/kubernetes/cloud_native_postgresql/postgresql_conf/

postgresql.confやhba.confの編集は下記を参考に設定下さい。

https://www.enterprisedb.com/docs/kubernetes/cloud_native_postgresql/samples/cluster-example-custom.yaml

設定反映は前回の続きであれば下記コマンドで行います。

コンテナやDBの変更の場合、先に`minikube delete pod cluster`が必要になるでしょう

$ kubectl apply -f cluster-example.yaml

実際にPodにログインして確認してみましょう

$ kubectl exec -it cluster-example-1 -- /bin/bash

postgresql.confは下記で確認できます。

また、custom.confという追加設定が格納されているファイルがあります。

さらに、postgresq.auto.confというOperatorが管理するファイルも別途存在します。

$ grep -v -e '^\s*#' -e '^\s*$' /var/lib/postgresql/data/pgdata/postgresql.conf
$ grep -v -e '^\s*#' -e '^\s*$' /var/lib/postgresql/data/pgdata/custom.conf
$ grep -v -e '^\s*#' -e '^\s*$' /var/lib/postgresql/data/pgdata/postgresql.auto.conf

pg_hba.confは下記となっております

$ grep -v -e '^\s*#' -e '^\s*$' /var/lib/postgresql/data/pgdata/pg_hba.conf 

あとがき

今回は接続に知りたい情報を中心に記載致しました。

次回の内容はライセンス認証を予定してます。


ここまでお読みいただき、ありがとうございました!

アバター画像
About サイオステクノロジーの中の人です 88 Articles
サイオステクノロジーで働く中の人です。
ご覧いただきありがとうございます! この投稿はお役に立ちましたか?

役に立った 役に立たなかった

0人がこの投稿は役に立ったと言っています。


ご覧いただきありがとうございます。
ブログの最新情報はSNSでも発信しております。
ぜひTwitterのフォロー&Facebookページにいいねをお願い致します!



>> 雑誌等の執筆依頼を受付しております。
   ご希望の方はお気軽にお問い合わせください!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


質問はこちら 閉じる