はじめに
HelmとHelmfileを使ってPostgreSQLをサクッと構築してみたいと思います。
必要なファイル
必要なファイルは下記の通りです。
.
├── helmfile.yaml
└── values.yaml
Namespaceの作成
まずはデプロイ先となるNamespaceを作成します。
kubectl create ns postgres
helmfile.yamlの作成
helmfile.yaml
を作成します。
releases:
- name: postgresql
namespace: postgres
chart: bitnami/postgresql
values:
- ./values.yaml
ここでは、Bitnamiのチャートを使っています。

postgresql 12.1.14 · bitnami/bitnami
PostgreSQL (Postgres) is an open source object-relational database known for reliability and data integrity. ACID-compliant, it supports foreign keys, joins, vi...
values.yamlの作成
values.yaml
を作成します。
デフォルトの値は下記のファイルの通りになっています。
charts/values.yaml at main · bitnami/charts
Bitnami Helm Charts. Contribute to bitnami/charts development by creating an account on GitHub.
ここでは、my-db
というデータベースとmy-user
というユーザーを作成するようにしています。また、Adminユーザー(postgres)とmy-user
のパスワードは別のSecretに格納するようにしてみます。
auth:
username: "my-user"
database: "my-db"
existingSecret: "db-secret"
secretKeys:
adminPasswordKey: postgres-password
userPasswordKey: password
secret.yamlの作成
ユーザーのパスワードを格納したSecretを作成します。
kubectl -n postgres create secret generic db-secret --from-literal=postgres-password=hogehoge --from-literal=password=abc123xyz456
PostgreSQLの起動
Helmfileを使ってPostgreSQLを起動します。
必要であれば、リポジトリを追加します。
helm repo add bitnami https://charts.bitnami.com/bitnami
Helmfileでデプロイします。
helmfile apply
動作確認
Podが起動したら動作確認してみます。
まずは作成したSecretからmy-user
のパスワードを環境変数に格納します。
export POSTGRES_PASSWORD=$(kubectl get secret --namespace postgres db-secret -o jsonpath="{.data.password}" | base64 -d)
下記でパスワードを使ってmy-user
でPostgreSQLに接続してみます。
kubectl port-forward --namespace postgres svc/postgresql 5432:5432 & PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U my-user -d my-db -p 5432
my-db=> \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-------------------------
my-db | my-user | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/"my-user" +
| | | | | "my-user"=CTc/"my-user"
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres