摘要: 通过阿里云Kubernetes容器服务,开启你的数据服务之旅 (一)云上运维自建数据库之痛,使用容器服务自动恢复数据库postgresql实例 概述 本文为大家介绍一种容器化的数据服务 posgresql db on ACK,通过使用云盘自动挂载实现的块存储PVC来做到数据库的免运维恢复。
容器开启数据服务之旅系列(一):Kubernetes如何解自建PostgreSQL运维之痛
概述
本文为大家介绍一种容器化的数据服务 posgresql db on ACK,通过使用云盘自动挂载实现的块存储PVC来做到数据库的免运维恢复。借助阿里云Kubernetes服务与阿里云存储资源的深度整合,打造数据库实例的免运维异地(ECS)恢复的不死神话。
先决条件
1. 你已经通过阿里云容器服务创建了一个Kubernetes集群,详细步骤参见创建Kubernetes集群
2. 阿里云容器服务的存储插件(默认情况存储插件已经自动部署在阿里云Kubernetes容器服务)
从容器服务控制台创建一个posgresql实例
使用三次点击来创建一个postgresql的不死实例
1. 登录 https://cs.console.aliyun.com/
2. 点击 “应用目录”
3. 选择 "postgresql"

4.(可选)选择你的Kubernetes集群. e.g. k8s-gpu
5.(可选)选择你的应用的命名空间。默认: default
6. 给你的数据库应用取个容易记住的名字。 e.g. postgresql-online

7.(可选) 点击 “参数” 来定制你的数据库实例
1.修改 persistence.size: 20Gi 增加数据库云盘的容量
2.修改 persistence.storageClass: alicloud-disk-efficiency 调整数据卷所在云盘的性能。比如改为alicloud-disk-ssd 挂载ssd类型的

## Persist data to a persitent volume
persistence:
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: <storageclass>
## 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)恢复
1.添加测试数据
ssh <public ip="" of="" kubernetes="" master="" node="">`
# kubectl run my-pg-postgresql-client --rm --tty -i --image postgres \
--env "PGPASSWORD=$PGPASSWORD" \
--command -- psql -U postgres \
-h postgresql-online-postgresql postgres
If you don't see a command prompt, try pressing enter.
postgres=# create table t1 (id int);
CREATE TABLE
postgres=# insert into t1 values(10);
INSERT 0 1
postgres=# select * from t1;
id
----
10
(1 row)
驱逐数据库实例从ECS节点 i-bp1fazljd8u2ylk2otdo 到 i-
bp1fazljd8u2ylk2otdp
#kubectl get pod -o wide | grep postgresql-online-postgresql
postgresql-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-6mfzz
pod "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-bp1fazljd8u2ylk2otdp
kubectl run my-pg-postgresql-client --rm --tty -i --image postgres \
> --env "PGPASSWORD=$PGPASSWORD" \
> --command -- psql -U postgres \
> -h postgresql-online-postgresql postgres
If you don't see a command prompt, try pressing enter.
postgres=# select * from t1;
id
----
10
(1 row)
CLI控: 使用命令行完成postgresql实例的创建,迁移验证
login k8s master and deploy an postgresql db in seconds.
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-postgresql
postgresql-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-6mfzz
pod "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-bp1fazljd8u2ylk2otdp
kubectl run my-pg-postgresql-client --rm --tty -i --image postgres \
> --env "PGPASSWORD=$PGPASSWORD" \
> --command -- psql -U postgres \
> -h postgresql-online-postgresql postgres
If you don't see a command prompt, try pressing enter.
postgres=# select * from t1;
id
----
10
(1 row)
阅读更多干货好文,请关注扫描以下二维码:
</public></public></storageclass>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。