介绍:
如果您在阿里云容器服务部署了应用,并且使用云盘数据卷保存数据,您可以有数据迁移、数据回滚、磁盘类型变配等需求,本方案为您提供了Flexvolume、Disk-Controller所运行的集群中,云盘数据管理方案;
适合以下场景:
磁盘轮转:您之前使用了essd云盘,想轮转为ssd的盘;或者反之;
磁盘轮转:您之前使用了加密云盘,想轮转为非加密盘;或者反之;
跨可用区迁移:您之前使用了可用区A的盘,想在可用区B使用;
跨Region迁移:您之前在杭州Region使用的云盘,想在北京Region使用;
数据回滚:您的磁盘定期打了快照,想使用某个版本的快照恢复数据;
方案总体原则是:通过云盘快照技术实现数据的回滚、迁移、变配操作,并保持数据的一致性。
示例应用:
创建一个StatfulSet应用并挂载一块云盘,模板如下:
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
创建上面应用,并写入验证数据:
# 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
磁盘类型转换场景:
上述MySQL实例中3个pod使用了3个ssd云盘,我们通过轮转的方式将3个云盘变成essd类型,但数据保持一致。轮转过程包括:
将多个云盘分别打成快照;
通过快照创建新的pvc、pv;
pvc、pv名字要符合新的StatefulSet命名规则;
步骤1:创建快照;
从3个pod所对应的pv信息中找到云盘ID,并到到ECS控制台分别创建云盘快照;
假设三个云盘快照ID分别为:
pod-0 -> ssd-mysql-0 -> d-2ze48olpbvnlpofw9wpi -> s-2zeh66llz43m8g65y4um
pod-1 -> ssd-mysql-1 -> d-2ze6sw4nc3n9ovquss0s -> s-2zegk0d8ne75tz0g15zd
pod-2 -> ssd-mysql-2 -> d-2zeddogm2wsjovoch45m -> s-2zec3gco02of30dt6lgb
步骤2:创建新pvc;
老的pvc模板如下:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: ssd-mysql-0
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: alicloud-disk-ssd
创建新的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
改动包括:
1)名字改成“essd-mysql-0”;
2)添加annotations,并且value为pod-0,pvc-0对应的云盘快照ID;
3)storageClassName改成 alicloud-disk-essd;
创建pvc:
# kubectl apply -f pvc-0.yaml
essd-mysql-0 Bound d-2ze1nxm8hrcdmktxuocn 20Gi RWO alicloud-disk-essd 18m
同理,创建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
步骤3:更新应用:
删除老的statefulset,并创建新的statefulset,模板如下:
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
相对于老的模板,需要修改两个地方:
1)volumeClaimTemplates里面的name改成 essd;
2)volumeMounts里面的name改成 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
综上:mysql 3副本的云盘已经通过快照的方式变成了essd类型;
其他场景文档后续提供。