ry's Tech blog

Cloud Native技術などについて書いていきます。

kubedb ~ Continuous Archiving with WAL-G and Restore ~

はじめに

今回第三回ということで、前回までの記事をまだお読みで無い方は、下記を読んでいただけたらと思います!

Kubedbのデプロイに関して

Snapshotについて

Continuous Archiving with WAL-G

PostgreSQLではデータ書き込みに先立ってWALファイルに変更内容を書き出して行きます。 このWALファイルをMinIOに送って行きます。

Cluster作成時にこちらの設定をして行きます。

前回同様、MinIOを使うためのSecretを作成して行きます。

# echo -n 'minioadmin' > AWS_ACCESS_KEY_ID
# echo -n 'minioadmin' > AWS_SECRET_ACCESS_KEY
# kubectl create secret generic s3-secret \
    --from-file=./AWS_ACCESS_KEY_ID \
    --from-file=./AWS_SECRET_ACCESS_KEY
secret "s3-secret" created

manifestを作成して行きます。 WALファイルを保存する先をarchiverで記述して行きます。

# vi pg-wal.yaml

apiVersion: kubedb.com/v1alpha1
kind: Postgres
metadata:
  name: wal-postgres
  namespace: postgres
spec:
  version: "11.1-v2"
  replicas: 2
  updateStrategy:
    type: RollingUpdate
  storage:
    storageClassName: "openebs-hostpath"
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 1Gi
  archiver:
    storage:
      storageSecretName: s3-secret
      s3:
        bucket: waltest
        endpoint: <URL of MinIO>

それでは、適用して行きます。

# kubectl apply -f pg-wal.yaml
postgres.kubedb.com/wal-postgres created

確認して行きます。

# kubectl get pods
NAME                 READY   STATUS    RESTARTS   AGE
pod/wal-postgres-0   1/1     Running   0          13m
pod/wal-postgres-1   1/1     Running   0          13m

では、MinIOを見て行きます。

minio-wal

minio-wal2

中にはたくさんのfileが入っています。

minio-wal3

Restore

それでは、Restore検証をしていきます。

データの書き込み

直接アクセスして書き込んでいきます。

# kubectl exec -it wal-postgres-0 -- psql -U postgres
psql (11.1)
Type "help" for help.
postgres=#

postgres=# create database waltest;

postgres=# \c waltest

waltest=# create table waltest (a text);

waltest=# insert into waltest values ('before restore');

waltest=# select * from waltest;
       a
----------------
 before restore
(1 row)

Initialize

では、さっそくInitializeしていきます。

# vi pg-fromwal.yaml

apiVersion: kubedb.com/v1alpha1
kind: Postgres
metadata:
  name: restored-postgres-wal
  namespace: postgres
spec:
  version: "11.1-v2"
  replicas: 2
  databaseSecret:
    secretName: wal-postgres-minio-auth
  storage:
    storageClassName: "openebs-hostpath"
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 1Gi
  init:
    postgresWAL:
      storageSecretName: s3-secret
      s3:
        bucket: waltest
        prefix: "kubedb/demo/wal-postgres-minio/archive"
        endpoint: <URL of MinIO>

applyしていきます。

# kubectl apply -f pg-fromwal.yaml
postgres.kubedb.com/restored-postgres-wal created

では、作成されたかを確認していきます。

# kubectl get pods -n postgres
NAME                      READY   STATUS    RESTARTS   AGE
restored-postgres-wal-0   1/1     Running   0          3m3s
restored-postgres-wal-1   1/1     Running   1          2m57s
wal-postgres-0            1/1     Running   0          20m
wal-postgres-1            1/1     Running   0          20m

# kubectl get svc
NAME                             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubedb                           ClusterIP   None             <none>        <none>     23m
restored-postgres-wal            ClusterIP   10.100.200.118   <none>        5432/TCP   3m18s
restored-postgres-wal-replicas   ClusterIP   10.100.200.49    <none>        5432/TCP   3m18s
wal-postgres                     ClusterIP   10.100.200.12    <none>        5432/TCP   23m
wal-postgres-replicas            ClusterIP   10.100.200.199   <none>        5432/TCP   23m

それでは、確認していきます。

# kubectl exec -it restored-postgres-wal-0 -- psql -U postgres
psql (11.1)
Type "help" for help.
postgres=#

postgres=# \c waltest
You are now connected to database "waltest" as user "postgres".

waltest=# select * from waltest;
       a
----------------
 before restore
(1 row)

ちゃんと書き込んだ内容が反映されたPostgreSQLが作成されたことを確認することができました。

終わりに

  • Kubedbのインストール
  • Kubedbにおいて、Snapshotを作成し、そのSnapshotを元に新しいPostgreSQLを作成する。
  • PostgreSQLのWALファイルをバックアップし、そのWALを元に新しいPostgreSQLを作成する。

の3つを見てきました。

何かしらのお役に立てたら嬉しいです。