雲盤線上擴容功能已經釋出,本文介紹如何在Kubernetes叢集中在不斷服的情況下實作雲盤擴容。下面以StatefulSet多執行個體應用為例,介紹應用持續運作中實作挂載的雲盤擴容。
注意:擴容雲盤前,請務必先給雲盤打快照,以免操作中出現資料丢失;
注意:本方案雖然實作了線上擴容雲盤,但在擴容檔案系統的時候如果應用正在讀寫資料盤,存在導緻資料不一緻的可能;
更新的擴容雲盤方案請參考:
雲盤線上擴容、離線擴容雲盤方案 ;建立多執行個體statefulset應用
kubectl create -f sts.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- name: disk-ssd
mountPath: /data
volumeClaimTemplates:
- metadata:
name: disk-ssd
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "alicloud-disk-ssd"
resources:
requests:
storage: 20Gi
運作2個pod,分别挂載一個雲盤:
# kubectl get pod
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 1m
web-1 1/1 Running 0 1m
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
disk-ssd-web-0 Bound d-2zecm3r1qylc9dq057ul 20Gi RWO alicloud-disk-ssd 2m
disk-ssd-web-1 Bound d-2zeav09gzb5smie2iekw 20Gi RWO alicloud-disk-ssd 1m
# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
d-2zeav09gzb5smie2iekw 20Gi RWO Delete Bound default/disk-ssd-web-1 alicloud-disk-ssd 1m
d-2zecm3r1qylc9dq057ul 20Gi RWO Delete Bound default/disk-ssd-web-0 alicloud-disk-ssd 1m
目前2個盤大小都是20G:
# kubectl exec web-0 df | grep data
/dev/vdc 20511312 45080 20449848 1% /data
# kubectl exec web-1 df | grep data
/dev/vdd 20511312 45080 20449848 1% /data
建立測試資料:
# kubectl exec web-1 touch /data/test1
# kubectl exec web-0 touch /data/test0
# kubectl exec web-0 ls /data
lost+found test0
# kubectl exec web-1 ls /data
lost+found test1
擴容雲盤
上面應用共用到2塊雲盤:
d-2zecm3r1qylc9dq057ul:pod web-0使用;
d-2zeav09gzb5smie2iekw:pod web-1使用;
1. 狀态資訊:
以d-2zeav09gzb5smie2iekw為例;
登陸pod web-1所在節點:
# mount | grep d-2zeav09gzb5smie2iekw
/dev/vdd on /var/lib/kubelet/plugins/kubernetes.io/flexvolume/alicloud/disk/mounts/d-2zeav09gzb5smie2iekw type ext4 (rw,relatime,data=ordered)
/dev/vdd on /var/lib/kubelet/pods/e8d03d50-ad24-11e9-b1a1-00163e03e854/volumes/alicloud~disk/d-2zeav09gzb5smie2iekw type ext4 (rw,relatime,data=ordered)
可見:
雲盤挂載挂載裝置為/dev/vdd;
使用的檔案系統為ext4;
容器的挂載目錄為:/var/lib/kubelet/plugins/kubernetes.io/flexvolume/alicloud/disk/mounts/d-2zeav09gzb5smie2iekw
雲盤目前狀态:大小為20G;
# df -h | grep vdd
/dev/vdd 20G 45M 20G 1% /var/lib/kubelet/plugins/kubernetes.io/flexvolume/alicloud/disk/mounts/d-2zeav09gzb5smie2iekw
2. 控制台擴容雲盤
登陸阿裡雲ECS控制台,找到相應Region、可用區,并找到雲盤:d-2zeav09gzb5smie2iekw
控制台的狀态如下:雲盤大小為20G,正在使用中。點選磁盤擴容:
選擇雲盤線上擴容,并調整期望大小:
點選确認,并到控制台檢視目前雲盤大小:
3. 檔案系統擴容:
在挂載節點上檢視雲盤,可以看到/dev/vdd已經擴容。
# lsblk -l /dev/vdd
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vdd 253:48 0 30G 0 disk /var/lib/kubelet/pods/e8d03d50-ad24-11e9-b1a1-00163e03e854/volumes/alicloud~disk/d-2zeav09gzb5smie2iekw
但此時檔案系統依然是20G:
# df | grep vdd
/dev/vdd 20511312 45080 20449848 1% /var/lib/kubelet/plugins/kubernetes.io/flexvolume/alicloud/disk/mounts/d-2zeav09gzb5smie2iekw
執行檔案系統擴容(線上擴容需要當時沒有檔案讀寫,如果存在應用讀寫,需要先把應用停掉):
# resize2fs /dev/vdd
3. 擴容驗證:
下面指令驗證雲盤已經擴容成功;
# kubectl exec -ti web-1 df | grep data
/dev/vdd 30832548 45036 30771128 1% /data
下面指令驗證資料持久性:
# kubectl exec -ti web-1 ls data
lost+found test1