helmチャートを使用して自宅K8Sに postgresql をデプロイ

概要

上記で構築したKubernetesクラスターに対し、デプロイをしてみます。

ここでは、bitnami の helmチャートを使用して、postgresqlをデプロイします。

事前準備

Storage を使用しますので、default の StorageClass が存在することを確認します。

$ k get sc
NAME                   PROVISIONER                     RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
ceph-block (default)   rook-ceph.rbd.csi.ceph.com      Delete          Immediate           true                   12d
ceph-bucket            rook-ceph.ceph.rook.io/bucket   Delete          Immediate           false                  12d
ceph-filesystem        rook-ceph.cephfs.csi.ceph.com   Delete          Immediate           true                   12d

手順

リポジトリの登録

$ helm repo list
NAME    URL                              
metallb https://metallb.github.io/metallb

$ helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories

3:~$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "metallb" chart repository
...Successfully got an update from the "bitnami" chart repository
Update Complete. ⎈Happy Helming!⎈

バージョンの確認

2024.7.20現在、(postgresqlとしての)最新は 16.3.0

$ helm search repo bitnami/postgresql --versions
NAME                    CHART VERSION   APP VERSION     DESCRIPTION                                       
bitnami/postgresql      15.5.17         16.3.0          PostgreSQL (Postgres) is an open source object-...
bitnami/postgresql      15.5.16         16.3.0          PostgreSQL (Postgres) is an open source object-...
bitnami/postgresql      15.5.15         16.3.0          PostgreSQL (Postgres) is an open source object-...
.
.
.

こちらはHA版

$ helm search repo bitnami/postgresql-ha --versions
NAME                    CHART VERSION   APP VERSION     DESCRIPTION                                       
bitnami/postgresql-ha   14.2.12         16.3.0          This PostgreSQL cluster solution includes the P...
bitnami/postgresql-ha   14.2.11         16.3.0          This PostgreSQL cluster solution includes the P...
bitnami/postgresql-ha   14.2.10         16.3.0          This PostgreSQL cluster solution includes the P...
.
.
.

helm show values bitnami/postgresql --version 15.5.17 > values.yaml

インストール

$ helm install my-postgresql bitnami/postgresql --version 15.5.17 --namespace postgresql --create-namespace --set global.defaultStorageClass=ceph-block

$ helm install my-postgresql bitnami/postgresql --version 15.5.17 --namespace postgresql --create-namespace --set global.defaultStorageClass=ceph-block
NAME: my-postgresql
LAST DEPLOYED: Sat Jul 20 16:20:58 2024
NAMESPACE: postgresql
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: postgresql
CHART VERSION: 15.5.17
APP VERSION: 16.3.0

\** Please be patient while the chart is being deployed **

PostgreSQL can be accessed via port 5432 on the following DNS names from within your cluster:

    my-postgresql.postgresql.svc.cluster.local - Read/Write connection

To get the password for "postgres" run:

    export POSTGRES_PASSWORD=$(kubectl get secret --namespace postgresql my-postgresql -o jsonpath="{.data.postgres-password}" | base64 -d)

To connect to your database run the following command:

    kubectl run my-postgresql-client --rm --tty -i --restart='Never' --namespace postgresql --image docker.io/bitnami/postgresql:16.3.0-debian-12-r19 --env="PGPASSWORD=$POSTGRES_PASSWORD" \
      --command -- psql --host my-postgresql -U postgres -d postgres -p 5432

    > NOTE: If you access the container using bash, make sure that you execute "/opt/bitnami/scripts/postgresql/entrypoint.sh /bin/bash" in order to avoid the error "psql: local user with ID 1001} does not exist"

To connect to your database from outside the cluster execute the following commands:

    kubectl port-forward --namespace postgresql svc/my-postgresql 5432:5432 &
    PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -d postgres -p 5432

WARNING: The configured password will be ignored on new installation in case when previous PostgreSQL release was deleted through the helm command. In that case, old PVC will have an old password, and setting it through helm won't take effect. Deleting persistent volumes (PVs) will solve the issue.

WARNING: There are "resources" sections in the chart not set. Using "resourcesPreset" is not recommended for production.
 For production installations, please set the following values according to your workload needs:
  - primary.resources
  - readReplicas.resources
+info https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/

後で確かめましたが、ceph-block StorageClass の default として設定されていますので、明示的に指定しなくても helm install できました。

$ helm install my-postgresql bitnami/postgresql --version 15.5.17 --namespace postgresql --create-namespace

確認

$ kubens postgresql 
Context "kubernetes-admin@kubernetes" modified.
Active namespace is "postgresql".

$ k get pod
NAME              READY   STATUS    RESTARTS   AGE
my-postgresql-0   1/1     Running   0          49s

$ k get svc
NAME               TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
my-postgresql      ClusterIP   10.98.136.15   <none>        5432/TCP   55s
my-postgresql-hl   ClusterIP   None           <none>        5432/TCP   55s

$ k get pvc
NAME                   STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
data-my-postgresql-0   Bound    pvc-33d8cd14-fcad-4dc0-b1c7-590b884c2b73   8Gi        RWO            ceph-block     <unset>                 65s

$ k get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                             STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
pvc-33d8cd14-fcad-4dc0-b1c7-590b884c2b73   8Gi        RWO            Delete           Bound    postgresql/data-my-postgresql-0   ceph-block     <unset>                          66s

$ k get deployment
No resources found in postgresql namespace.

$ k get statefulsets
NAME            READY   AGE
my-postgresql   1/1     89s

$ helm get values my-postgresql
USER-SUPPLIED VALUES:
global:
  defaultStorageClass: ceph-block

アクセス

インストール

$ sudo apt install postgresql-client postgresql-client-common

ポートフォワード

$ kubectl port-forward svc/my-postgresql 5432:5432

パスワードの取得

$ kubectl get secret --namespace postgresql my-postgresql -o jsonpath="{.data.postgres-password}" | base64 -d

psqlコマンドによるアクセス

$ psql -h localhost -p 5432 -U postgres -W

実行例

$ psql -h localhost -p 5432 -U postgres -W
Password: 
psql (14.12 (Ubuntu 14.12-0ubuntu0.22.04.1), server 16.3)
WARNING: psql major version 14, server major version 16.
         Some psql features might not work.
Type "help" for help.

postgres=# \d
Did not find any relations.

アンインストール

$ helm uninstall my-postgresql
$ kubectl delete namespace postgresql
Copied title and URL