Kubernetes集群中云盘在线扩容

云盘在线扩容功能已经发布,本文介绍如何在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,正在使用中。点击磁盘扩容:

Kubernetes集群中云盘在线扩容

选择云盘在线扩容,并调整期望大小:

Kubernetes集群中云盘在线扩容

点击确认,并到控制台查看当前云盘大小:

Kubernetes集群中云盘在线扩容

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

用以上方法对其他云盘进行扩容

上一篇:使用阿里云Flexvolume插件实现云盘数据卷动态扩容


下一篇:K8S中手动扩容云盘数据卷