Containers open the journey of data services (1): How Kubernetes solves the pain of self-built PostgreSQL operation and maintenance

Abstract: Through Alibaba Cloud Kubernetes Container Service, start your journey of data service db on ACK, by using the block storage PVC implemented by the automatic mounting of cloud disks, the database can be restored without operation and maintenance.

 

Containers open the journey of data services (1): How Kubernetes solves the pain of self-built PostgreSQL operation and maintenance

 

Overview

 

This article introduces a containerized data service posgresql db on ACK, which uses the block storage PVC implemented by the automatic mounting of cloud disks to achieve free operation and maintenance recovery of the database. With the help of the deep integration of Alibaba Cloud Kubernetes service and Alibaba Cloud storage resources, the immortal myth of the operation-free off-site (ECS) recovery of database instances is created.

 

prerequisites

 

1. You have created a Kubernetes cluster through Alibaba Cloud Container Service. For detailed steps, see Creating a Kubernetes Cluster

2. The storage plug-in of Alibaba Cloud Container Service (by default, the storage plug-in has been automatically deployed in Alibaba Cloud Kubernetes Container Service)

 

Create a posgresql instance from the container service console

 

Use three clicks to create an undead instance of postgresql

1. Log  in to https://cs.console.aliyun.com/

2. Click "App Directory"

3. Select "postgresql"

4 (Optional) Select your Kubernetes cluster. eg k8s-gpu

5 (Optional) Select your app's namespace. Default: default

6 Give your database application an easy-to-remember name. eg postgresql-online

 

7 (Optional) Click on "Parameters" to customize your database instance

 

1. Modify persistence.size: 20Gi to increase the capacity of the database cloud disk. 2. Modify persistence.storageClass: alicloud-disk-efficiency to adjust the performance of the cloud disk where the data volume is located. For example, change to alicloud-disk-ssd to mount the ssd type

 

## 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 Click "Deploy" to complete the data release

9 Click "Kubernetes Console" to view the deployment instance

 

10 Click the container group and click postgresql-online-postgresql to view the database instance password. The container instance is started on the node: cn-hangzhou.i-bp1fazljd8u2ylk2otdo

 

11 Test offsite (ECS) recovery of postgresql database

 

1. Add test data

 

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)

 

1. Drive out the database instance from the ECS node i-bp1fazljd8u2ylk2otdo to 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

 

1. Data verification, instance migration, automatic recovery of database instances, no data loss.

 

# 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 control: use the command line to complete the creation of postgresql instance, migration verification

 

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)

 

Original link

To read more good articles, please scan the following QR code: 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325257015&siteId=291194637