HelmでPostgreSQLを動かしてみる

2023.02.09
2024.03.24
データベース
HelmHelmfilePostgreSQL

はじめに

HelmとHelmfileを使ってPostgreSQLをサクッと構築してみたいと思います。

必要なファイル

必要なファイルは下記の通りです。

1.
2├── helmfile.yaml
3└── values.yaml

Namespaceの作成

まずはデプロイ先となるNamespaceを作成します。

1kubectl create ns postgres

helmfile.yamlの作成

helmfile.yamlを作成します。

1releases:
2- name: postgresql
3  namespace: postgres
4  chart: bitnami/postgresql
5  values:
6  - ./values.yaml

ここでは、Bitnamiのチャートを使っています。

postgresql 15.1.4 · bitnami/bitnami

postgresql 15.1.4 · bitnami/bitnami

PostgreSQL (Postgres) is an open source object-relational database known for reliability and data integrity. ACID-compliant, it supports foreign keys, joins, views, triggers and stored procedures.

values.yamlの作成

values.yamlを作成します。

デフォルトの値は下記のファイルの通りになっています。

charts/bitnami/postgresql/values.yaml at main · bitnami/charts

charts/bitnami/postgresql/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に格納するようにしてみます。

1auth:
2  username: "my-user"
3  database: "my-db"
4
5  existingSecret: "db-secret"
6  secretKeys:
7    adminPasswordKey: postgres-password
8    userPasswordKey: password

secret.yamlの作成

ユーザーのパスワードを格納したSecretを作成します。

1kubectl -n postgres create secret generic db-secret --from-literal=postgres-password=hogehoge --from-literal=password=abc123xyz456

PostgreSQLの起動

Helmfileを使ってPostgreSQLを起動します。

必要であれば、リポジトリを追加します。

1helm repo add bitnami https://charts.bitnami.com/bitnami

Helmfileでデプロイします。

1helmfile apply

動作確認

Podが起動したら動作確認してみます。

まずは作成したSecretからmy-userのパスワードを環境変数に格納します。

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

下記でパスワードを使ってmy-userでPostgreSQLに接続してみます。

1kubectl 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
1my-db=> \l
2                                   List of databases
3   Name    |  Owner   | Encoding |   Collate   |    Ctype    |    Access privileges
4-----------+----------+----------+-------------+-------------+-------------------------
5 my-db     | my-user  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/"my-user"          +
6           |          |          |             |             | "my-user"=CTc/"my-user"
7 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
8 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres            +
9           |          |          |             |             | postgres=CTc/postgres
10 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres            +
11           |          |          |             |             | postgres=CTc/postgres

参考

Support

\ この記事が役に立ったと思ったら、サポートお願いします! /

buy me a coffee
Share

Profile

author

Masa

都内のIT企業で働くエンジニア
自分が学んだことをブログでわかりやすく発信していきながらスキルアップを目指していきます!

buy me a coffee