本文共 5311 字,大约阅读时间需要 17 分钟。
本文为大家介绍一种容器化的数据服务 posgresql db on ACK,通过使用云盘自动挂载实现的块存储PVC来做到数据库的免运维恢复。借助阿里云Kubernetes服务与阿里云存储资源的深度整合,打造数据库实例的免运维异地(ECS)恢复的不死神话。
使用三次点击来创建一个postgresql的不死实例

4 (可选)选择你的Kubernetes集群. e.g. k8s-gpu
5 (可选)选择你的应用的命名空间。默认: default6 给你的数据库应用取个容易记住的名字。 e.g. postgresql-online
7 (可选) 点击 “参数” 来定制你的数据库实例
persistence.size: 20Gi
增加数据库云盘的容量persistence.storageClass: alicloud-disk-efficiency
调整数据卷所在云盘的性能。比如改为alicloud-disk-ssd
挂载ssd类型的
## Persist data to a persitent volumepersistence: enabled: true ## A manually managed Persistent Volume and Claim ## Requires persistence.enabled: true ## If defined, PVC must be created manually before volume will be bound # existingClaim: ## database data Persistent Volume Storage Class ## if defined, user has to define storageClassName: alicloud-disk to support dynamic provision of alicloud-disk ## If defined, storageClassName:## If set to "-", storageClassName: "", which disables dynamic provisioning ## If undefined (the default) or set to null, no storageClassName spec is ## set, choosing the default provisioner. (gp2 on AWS, standard on ## GKE, AWS & OpenStack) ## #storageClass: "alicloud-disk-ssd" storageClass: "alicloud-disk-efficiency" accessMode: ReadWriteOnce size: 20Gi subPath: "postgresql-db"
8 点击 “部署”, 完成数据发布
9 点击 “Kubernetes 控制台”,查看部署实例10 点击容器组,点击 postgresql-online-postgresql 查看数据库实例密码。容器实例启动在节点:cn-hangzhou.i-bp1fazljd8u2ylk2otdo


11 测试postgresql数据库的异地(ECS)恢复
ssh`# kubectl run my-pg-postgresql-client --rm --tty -i --image postgres \--env "PGPASSWORD=$PGPASSWORD" \--command -- psql -U postgres \-h postgresql-online-postgresql postgresIf you don't see a command prompt, try pressing enter.postgres=# create table t1 (id int);CREATE TABLEpostgres=# insert into t1 values(10);INSERT 0 1postgres=# select * from t1; id---- 10(1 row)
#kubectl get pod -o wide | grep postgresql-online-postgresqlpostgresql-online-postgresql-78884fbcf6-6mfzz 1/1 Running 0 36m 172.30.4.4 cn-hangzhou.i-bp1fazljd8u2ylk2otdo#kubectl cordon cn-hangzhou.i-bp1fazljd8u2ylk2otdo #kubectl delete pod postgresql-online-postgresql-78884fbcf6-6mfzzpod "postgresql-online-postgresql-78884fbcf6-6mfzz" deleted# kubectl get pod -o wide|grep postgresql-online-postgresql#postgresql-online-postgresql-78884fbcf6-87ttg 0/1 Running 0 15s 172.30.3.5 cn-hangzhou.i-bp1fazljd8u2ylk2otdp
# kubectl get pod -o wide|grep postgresql-online-postgresql#postgresql-online-postgresql-78884fbcf6-87ttg 0/1 Running 0 15s 172.30.3.5 cn-hangzhou.i-bp1fazljd8u2ylk2otdpkubectl run my-pg-postgresql-client --rm --tty -i --image postgres \> --env "PGPASSWORD=$PGPASSWORD" \> --command -- psql -U postgres \> -h postgresql-online-postgresql postgresIf you don't see a command prompt, try pressing enter.postgres=# select * from t1; id---- 10(1 row)
ssh <public ip of kubernetes master node >
helm install -n postgresql-online --set 'persistence.size=30Gi,persistence.storageClass=alicloud-disk-ssd' stable/postgresql
To get your user password run:
PGPASSWORD=$(kubectl get secret --namespace default my-pg-postgresql -o jsonpath="{.data.postgres-password}" | base64 --decode; echo)
To connect to your database run the following command (using the env variable from above):
kubectl run my-pg-postgresql-client --rm --tty -i --image postgres \ --env "PGPASSWORD=$PGPASSWORD" \ --command -- psql -U postgres \ -h postgresql-online-postgresql postgres
To connect to your database directly from outside the K8s cluster:
PGHOST=127.0.0.1 PGPORT=5432 # Execute the following commands to route the connection: export POD_NAME=$(kubectl get pods --namespace default -l "app=my-pg-postgresql" -o jsonpath="{.items[0].metadata.name}") kubectl port-forward $POD_NAME 5432:5432
#kubectl get pod -o wide | grep postgresql-online-postgresqlpostgresql-online-postgresql-78884fbcf6-6mfzz 1/1 Running 0 36m 172.30.4.4 cn-hangzhou.i-bp1fazljd8u2ylk2otdo#kubectl cordon cn-hangzhou.i-bp1fazljd8u2ylk2otdo #kubectl delete pod postgresql-online-postgresql-78884fbcf6-6mfzzpod "postgresql-online-postgresql-78884fbcf6-6mfzz" deleted# kubectl get pod -o wide|grep postgresql-online-postgresql#postgresql-online-postgresql-78884fbcf6-87ttg 0/1 Running 0 15s 172.30.3.5 cn-hangzhou.i-bp1fazljd8u2ylk2otdp
Test original postgresql db could be recovered on different node.
#kubectl get pod -o wide|grep postgresql-online-postgresql#postgresql-online-postgresql-78884fbcf6-87ttg 0/1 Running 0 15s 172.30.3.5 cn-hangzhou.i-bp1fazljd8u2ylk2otdpkubectl run my-pg-postgresql-client --rm --tty -i --image postgres \> --env "PGPASSWORD=$PGPASSWORD" \> --command -- psql -U postgres \> -h postgresql-online-postgresql postgresIf you don't see a command prompt, try pressing enter.postgres=# select * from t1; id---- 10(1 row)
转载地址:http://qheoa.baihongyu.com/