By Kan Junbao
If you deploy an application in Alibaba Cloud Container Service and use disk data volumes to save data, you may have data migration, data rollback, and disk type change requirements. This solution provides a disk Data Management solution in clusters, running Flexvolume and Disk-Controller.
It is suitable for the following scenarios:
The general principle of the solution is to use disk snapshot technology to implement data rollback, migration, and configuration change and maintain data consistency.
Create a StatfulSet application and attach a disk. The template is listed below:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: "mysql"
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
command: ["sh", "-c"]
args: ["sleep 10000"]
volumeMounts:
- name: ssd
mountPath: /data
volumeClaimTemplates:
- metadata:
name: ssd
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "alicloud-disk-ssd"
resources:
requests:
storage: 20Gi
Create the preceding application and write the validation data:
# kubectl get pod
mysql-0 1/1 Running 0 6m40s
mysql-1 1/1 Running 0 72s
mysql-2 1/1 Running 0 65s
# kubectl get pvc
ssd-mysql-0 Bound d-2ze48olpbvnlpofw9wpi 20Gi RWO alicloud-disk-ssd 7m3s
ssd-mysql-1 Bound d-2ze6sw4nc3n9ovquss0s 20Gi RWO alicloud-disk-ssd 95s
ssd-mysql-2 Bound d-2zeddogm2wsjovoch45m 20Gi RWO alicloud-disk-ssd 88s
# kubectl exec -ti mysql-0 touch /data/mysql0
# kubectl exec -ti mysql-1 touch /data/mysql1
# kubectl exec -ti mysql-2 touch /data/mysql2
# kubectl exec -ti mysql-0 ls /data
lost+found mysql0
# kubectl exec -ti mysql-1 ls /data
lost+found mysql1
# kubectl exec -ti mysql-2 ls /data
lost+found mysql2
In the preceding MySQL instance, three pods use three SSD disks. We use rotation to change the three disks to the ESSD type, but the data is consistent. The rotation process includes:
Find the disk IDs from the PV information corresponding to the three pods and go to the ECS console to create the disk snapshots.
Assume the three disk snapshot IDs are:
The old PVC template is listed below:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: ssd-mysql-0
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: alicloud-disk-ssd
Create a new PVC:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
annotations:
provider.disksnapshot.aliyuncs.com/disk-snapshot-id: s-2zeh66llz43m8g65y4um
name: essd-mysql-0
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: alicloud-disk-essd
Changes include:
1) Change the name to "essd-mysql-0"
2) Add annotations and the value is pod-0,pvc-0 the corresponding disk snapshot ID
3) storageClassName to alicloud-disk-essd
Create a PVC:
# kubectl apply -f pvc-0.yaml
essd-mysql-0 Bound d-2ze1nxm8hrcdmktxuocn 20Gi RWO alicloud-disk-essd 18m
Similarly, create the pvc-1:essd-mysql-1,pvc-2:essd-mysql-2:
essd-mysql-1 Bound d-2zeh66llz43m8tzeminq 20Gi RWO alicloud-disk-essd 2m27s
essd-mysql-2 Bound d-2ze383b6z44xjm79qoj2 20Gi RWO alicloud-disk-essd 2m25s
Delete the old statefulset and create a new statefulset. The template is listed below:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: "mysql"
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
command: ["sh", "-c"]
args: ["sleep 10000"]
volumeMounts:
- name: essd
mountPath: /data
volumeClaimTemplates:
- metadata:
name: essd
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "alicloud-disk-ssd"
resources:
requests:
storage: 20Gi
Compared with the old template, two places need to be modified:
1) The name in the volumeClaimTemplates is changed to ESSD
2) The name in volumeMounts is changed to ESSD
# kubectl get pod
mysql-0 1/1 Running 0 58s
mysql-1 1/1 Running 0 28s
mysql-2 1/1 Running 0 23s
# kubectl describe pod mysql-0 |grep ClaimName
ClaimName: essd-mysql-0
# kubectl describe pod mysql-1 |grep ClaimName
ClaimName: essd-mysql-1
# kubectl describe pod mysql-2 |grep ClaimName
ClaimName: essd-mysql-2
# kubectl exec mysql-0 ls /data
lost+found mysql0
# kubectl exec mysql-1 ls /data
lost+found mysql1
# kubectl exec mysql-2 ls /data
lost+found mysql2
In summary, the cloud disk of the mysql 3 replica has been changed to the ESSD type using snapshot. Other scenario documents will be available later.
ALB Ingress Handles Cloud-Native Application Traffic Management Easily
Serverless Application Programming Model in Cloud-Native Scenarios
166 posts | 30 followers
FollowAlibaba Clouder - February 10, 2021
Alibaba Developer - August 18, 2020
Alibaba Developer - June 17, 2020
Alibaba Container Service - November 7, 2024
Alibaba Developer - February 1, 2021
Marketplace - November 23, 2018
166 posts | 30 followers
FollowProvides a control plane to allow users to manage Kubernetes clusters that run based on different infrastructure resources
Learn MoreAlibaba Cloud Container Service for Kubernetes is a fully managed cloud container management service that supports native Kubernetes and integrates with other Alibaba Cloud products.
Learn MoreA secure image hosting platform providing containerized image lifecycle management
Learn MoreElastic and secure virtual cloud servers to cater all your cloud hosting needs.
Learn MoreMore Posts by Alibaba Container Service