作者:阚俊宝
一、CSI部署拓扑
共分为 CSI Provisioner 和 CSI Plugin 两部分:
Provisioner:目前只支持云盘,提供云盘数据卷动态创建的功能;
Plugin:云盘、NAS、OSS均需实现plugin;云盘实现了attach、mount的功能;NAS、OSS实现mount功能;
OSS Plugin 是通过 fuse 驱动实现的文件系统挂载,在为了解决升级问题,通过 systemd 方案 说明
二、环境准备
需要有 Kubernetes 集群,可以通过阿里云ACK 服务快速创建:使用文档
阿里云 CSI 插件推荐使用 Node Attach 方式参考,需要在 kubelet 中配置下面参数:
--enable-controller-attach-detach=false
下载部署模板:
https://github.com/AliyunContainerService/csi-plugin/blob/master/deploy/all/csi-plugin.yaml
https://github.com/AliyunContainerService/csi-plugin/blob/master/deploy/all/csi-provisioner.yaml
三、插件组成
1. 依赖部分:
RBAC:
参考模板:https://github.com/AliyunContainerService/csi-plugin/blob/master/deploy/rbac.yaml
这里我们使用阿里云ACK中默认提供的serviceaccount:admin,可以满足我们的需求;
CSIDriver:
定义插件执行Node Attach方式,不需要部署attacher;分别为NAS,OSS,EBS创建CSIDriver;
2. Plugin 插件:
默认提供云盘、NAS、OSS 三种存储的插件,每个插件由 plugin 容器 和 driver-registrar 容器组成;
模板:https://github.com/AliyunContainerService/csi-plugin/blob/master/deploy/all/csi-plugin.yaml
通过配置- operator: "Exists",逃过所有 node taint,保证在所有节点都部署;
通过配置 priorityClassName: system-node-critical,保证插件在所有节点优先部署;
tolerations:
- operator: "Exists"
priorityClassName: system-node-critical
网络、Pid、securityContext 都配置为高权模式,以便插件具有挂载权限;
hostNetwork: true
hostPID: true
csi-diskplugin,配置以下环境变量:
# 通过配置AK环境,为plugin提供挂载ak权限;也可用通过STS方式配置;
# 文档:https://help.aliyun.com/document_detail/86485.html
# 配置MAX_VOLUMES_PERNODE,声明单个节点最多过载的云盘数量;ecs限制最大值为16,这里取值应该小于等于16;
- name: ACCESS_KEY_ID
value: ""
- name: ACCESS_KEY_SECRET
value: ""
- name: MAX_VOLUMES_PERNODE
value: "15"
3. Provisioner 插件:
默认提供以下 5 个 StorageClass:
alicloud-disk-available:会通过efficiency、ssd、common的顺序依次尝试创建云盘;
alicloud-disk-essd:创建essd类型云盘;
alicloud-disk-common:创建普通云盘;
alicloud-disk-ssd:创建ssd类型云盘;
alicloud-disk-efficiency:创建高效云盘;
通过配置- operator: "Exists",逃过所有 node taint,保证在所有节点都部署;
通过配置 priorityClassName: system-node-critical,保证插件在所有节点优先部署;
通过配置 nodeSelector,限制 Provisioner 只在 master 上执行,对于 managed K8S 场景可以去掉这个限制;
tolerations:
- operator: "Exists"
nodeSelector:
node-role.kubernetes.io/master: ""
priorityClassName: system-node-critical
四、部署插件
下载部署模板:
https://github.com/AliyunContainerService/csi-plugin/blob/master/deploy/all/csi-plugin.yaml
https://github.com/AliyunContainerService/csi-plugin/blob/master/deploy/all/csi-provisioner.yaml
执行部署命令:
# kubectl create -f csi-plugin.yaml
# kubectl create -f csi-provisioner.yaml
查看服务:
# kubectl get pod -nkube-system | grep csi
csi-plugin-2csjm 6/6 Running 0 75s
csi-plugin-52cs7 6/6 Running 0 75s
csi-plugin-g7kwr 6/6 Running 0 75s
csi-plugin-lqv2p 6/6 Running 0 75s
csi-plugin-r5d82 6/6 Running 0 75s
csi-plugin-vgkzp 6/6 Running 0 75s
csi-provisioner-0 2/2 Running 0 65s
# kubectl get sc
NAME PROVISIONER AGE
alicloud-disk-available diskplugin.csi.alibabacloud.com 2m8s
alicloud-disk-common diskplugin.csi.alibabacloud.com 2m8s
alicloud-disk-efficiency diskplugin.csi.alibabacloud.com 2m8s
alicloud-disk-essd diskplugin.csi.alibabacloud.com 2m8s
alicloud-disk-ssd diskplugin.csi.alibabacloud.com 2m8s
# kubectl get CSIDriver
NAME CREATED AT
diskplugin.csi.alibabacloud.com 2019-07-11T08:36:09Z
nasplugin.csi.alibabacloud.com 2019-07-11T08:36:09Z
ossplugin.csi.alibabacloud.com 2019-07-11T08:36:09Z
五、插件测试
部署下面模板,通过指定 storageclass 为 licloud-disk-ssd 创建一个 ssd 类型云盘;
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: disk-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 25Gi
storageClassName: alicloud-disk-ssd
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-disk
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
volumeMounts:
- name: disk-pvc
mountPath: "/data"
volumes:
- name: disk-pvc
persistentVolumeClaim:
claimName: disk-pvc
查看部署:
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
disk-pvc Bound pvc-008eee21-a3db-11e9-8dec-00163e0a6ecc 25Gi RWO alicloud-disk-ssd 14s
# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-008eee21-a3db-11e9-8dec-00163e0a6ecc 25Gi RWO Retain Bound default/disk-pvc alicloud-disk-ssd 23s
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-disk-6d5659d745-pdjjl 1/1 Running 0 33s
# 云盘已经挂载成功
# kubectl exec nginx-disk-6d5659d745-pdjjl ls data
lost+found
# kubectl exec nginx-disk-6d5659d745-pdjjl mount | grep /data
/dev/vdb on /data type ext4 (rw,relatime,data=ordered)