k8s環境でPostgreSQLのOperatorとしてCloudNativePGがよさそうだったので、LinodeのManaged Kubernetesサービス(LKE)上に導入してみた。
ドキュメントはしっかりしているようなので、こちらを参考にしてみた。
Installing Operator
最新のoperator manifestを適用する。
kubectl apply -f https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg/release-1.20/releases/cnpg-1.20.0.yaml
namespace/cnpg-system created
customresourcedefinition.apiextensions.k8s.io/backups.postgresql.cnpg.io created
customresourcedefinition.apiextensions.k8s.io/clusters.postgresql.cnpg.io created
customresourcedefinition.apiextensions.k8s.io/poolers.postgresql.cnpg.io created
customresourcedefinition.apiextensions.k8s.io/scheduledbackups.postgresql.cnpg.io created
serviceaccount/cnpg-manager created
clusterrole.rbac.authorization.k8s.io/cnpg-manager created
clusterrolebinding.rbac.authorization.k8s.io/cnpg-manager-rolebinding created
configmap/cnpg-default-monitoring created
service/cnpg-webhook-service created
deployment.apps/cnpg-controller-manager created
mutatingwebhookconfiguration.admissionregistration.k8s.io/cnpg-mutating-webhook-configuration created
validatingwebhookconfiguration.admissionregistration.k8s.io/cnpg-validating-webhook-configuration created
cnpg-systemというnamespaceが作成されるのでステータスを確認。
kubectl -n cnpg-system get all
こんな感じに表示される。
NAME READY STATUS RESTARTS AGE
pod/cnpg-controller-manager-6848c4b79b-gvp8n 1/1 Running 0 2m11s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/cnpg-webhook-service ClusterIP 10.128.129.153 <none> 443/TCP 2m12s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/cnpg-controller-manager 1/1 1 1 2m12s
NAME DESIRED CURRENT READY AGE
replicaset.apps/cnpg-controller-manager-6848c4b79b 1 1 1 2m11s
Deploy PostgreSQL Cluster
namespace:cnpgを作成
kubectl create ns cnpg
manifestを作成
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: cnpg1
namespace: cnpg
spec:
instances: 2
storage:
storageClass: linode-block-storage
size: 10Gi
monitoring:
enablePodMonitor: true
作成したmanifestを適用
kubectl apply -f cluster-cnpg.yml
ステータス確認
kubectl -n cnpg get all,pvc
NAME READY STATUS RESTARTS AGE
pod/cnpg1-1 1/1 Running 0 40m
pod/cnpg1-2 1/1 Running 0 39m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/cnpg1-r ClusterIP 10.128.157.111 <none> 5432/TCP 41m
service/cnpg1-ro ClusterIP 10.128.195.202 <none> 5432/TCP 41m
service/cnpg1-rw ClusterIP 10.128.182.242 <none> 5432/TCP 41m
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/cnpg1-1 Bound pvc-0160ca6fb944426f 10Gi RWO linode-block-storage 41m
persistentvolumeclaim/cnpg1-2 Bound pvc-59f42bd92fe04be5 10Gi RWO linode-block-storage 40m
CloudNativePG Pluginを使ってステータス確認
kubectl cnpg -n cnpg status cnpg1
Cluster Summary
Name: cnpg1
Namespace: cnpg
System ID: 7235528759757824019
PostgreSQL Image: ghcr.io/cloudnative-pg/postgresql:15.2
Primary instance: cnpg1-2
Status: Cluster in healthy state
Instances: 2
Ready instances: 2
Current Write LSN: 0/C000000 (Timeline: 2 - WAL File: 00000002000000000000000B)
Certificates Status
Certificate Name Expiration Date Days Left Until Expiration
---------------- --------------- --------------------------
cnpg1-ca 2023-08-19 07:01:17 +0000 UTC 88.10
cnpg1-replication 2023-08-19 07:01:17 +0000 UTC 88.10
cnpg1-server 2023-08-19 07:01:17 +0000 UTC 88.10
Continuous Backup status
Not configured
Streaming Replication status
Name Sent LSN Write LSN Flush LSN Replay LSN Write Lag Flush Lag Replay Lag State Sync State Sync Priority
---- -------- --------- --------- ---------- --------- --------- ---------- ----- ---------- -------------
cnpg1-1 0/C000000 0/C000000 0/C000000 0/C000000 00:00:00 00:00:00 00:00:00 streaming async 0
Unmanaged Replication Slot Status
No unmanaged replication slots found
Instances status
Name Database Size Current LSN Replication role Status QoS Manager Version Node
---- ------------- ----------- ---------------- ------ --- --------------- ----
cnpg1-2 43 MB 0/C000000 Primary OK BestEffort 1.20.0 lke109458-163366-6469b53b57e0
cnpg1-1 43 MB 0/C000000 Standby (async) OK BestEffort 1.20.0 lke109458-163366-6469b53bc03f
接続方法1
ポートフォワード設定
kubectl -n cnpg port-forward svc/cnpg1-rw 5432:5432
ローカルのpsqlで接続
PGPASSWORD=$(kubectl -n cnpg get secrets cnpg1-superuser -o go-template='{{.data.password | base64decode}}') psql -h localhost -U postgres
psql (15.3 (Homebrew)、サーバー 15.2 (Debian 15.2-1.pgdg110+1))
SSL接続(プロトコル: TLSv1.3、暗号化方式: TLS_AES_256_GCM_SHA384、圧縮: オフ)
"help"でヘルプを表示します。
postgres=# select version();
version
-----------------------------------------------------------------------------------------------------------------------------
PostgreSQL 15.2 (Debian 15.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
(1 行)
postgres=# select application_name, state, sync_state from pg_stat_replication;
application_name | state | sync_state
------------------+-----------+------------
cnpg1-1 | streaming | async
(1 行)
postgres=#
接続方法2
kubectl-cnpgプラグインを使うと、port-forwardの設定なしで接続出来る。
kubectl cnpg -n cnpg psql cnpg1
psql (15.2 (Debian 15.2-1.pgdg110+1))
Type "help" for help.
postgres=# select version();
version
-----------------------------------------------------------------------------------------------------------------------------
PostgreSQL 15.2 (Debian 15.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
(1 row)