使用文件存储cfs -奇异果体育app竞彩官网下载
准备工作
用户做好以下准备,才能在容器中挂载cfs实例。
创建容器集群
- 创建一个容器集群,操作步骤参考创建集群。
- 下载命令行客户端kubectl,并连接集群,操作步骤参考
创建cfs实例和挂载点
- 创建cfs实例,操作步骤请参考
- 添加cfs挂载点,操作步骤请参考
注意: 创建的cfs实例和挂载点须和集群节点在同一vpc内。
- 获取cfs挂载地址,操作步骤请参考
本操作假设cfs挂载点地址为 cfs-test.baidubce.com
操作指南
静态pv/pvc方式挂载cfs
1.在集群中创建pv和pvc资源
使用kubectl,执行 kubectl create -f pv-cfs.yaml
完成pv的创建
对应的pv-cfs.yaml
文件如下所示:
apiversion: v1
kind: persistentvolume
metadata:
name: pv-cfs
spec:
capacity:
storage: 8gi
accessmodes:
- readwritemany
persistentvolumereclaimpolicy: retain
mountoptions:
- hard
- nfsvers=4.1
- nordirplus
nfs:
path: /
server: cfs-test.baidubce.com
注意: yaml中server字段对应的是cfs挂载点地址
注意: yaml中path字段对应的是cfs挂载目录,该目录需要在挂载前预先存在
创建pv后,输入kubectl get pv
可以看见一个available状态的pv,如下所示:
$ kubectl get pv
name capacity access modes reclaim policy status claim storageclass reason age
pv-cfs 8gi rwx retain available 3s
建立一个能够与该pv绑定的pvc
使用kubectl,执行 kubectl create -f pvc-cfs.yaml
完成pvc的创建
对应的pvc-cfs.yaml
文件如下所示:
kind: persistentvolumeclaim
apiversion: v1
metadata:
name: pvc-cfs
spec:
accessmodes:
- readwritemany
resources:
requests:
storage: 8gi
绑定前,pvc为pending状态
$ kubectl get pvc
name status volume capacity access modes storageclass age
pvc-cfs pending 2s 2s
绑定后,pv和pvc状态变为bound
$ kubectl get pv
name capacity access modes reclaim policy status claim storageclass reason age
pv-cfs 8gi rwx retain bound default/pvc-cfs 36s
$ kubectl get pvc
name status volume capacity access modes storageclass age
pvc-cfs bound pv-cfs 8gi rwx 1m
有关pv和pvc的更多设置和字段说明,见
2.在pod内挂载pvc
在pod spec内指定相应的pvc名称即可,使用kubectl,执行 kubectl create -f demo-cfs-pod.yaml
完成pod的创建
对应的demo-cfs-pod.yaml
文件如下所示:
apiversion: v1
kind: pod
metadata:
name: demo-cfs-pod
labels:
app: demo-cfs-pod
spec:
containers:
- name: nginx
image: nginx
volumemounts:
- name: cfs-pvc
mountpath: "/cfs-volume"
volumes:
- name: cfs-pvc
persistentvolumeclaim:
claimname: pvc-cfs
pod创建后,可以读写容器内的/cfs-volume
路径来访问相应的cfs存储上的内容。
由于创建pv和pvc时指定了accessmodes
为readwritemany
,该pvc可以被多节点上的pod挂载读写。
3.释放pv和pvc资源
完成存储资源的使用后,可以释放pvc和pv资源。在释放pvc和pv之前,需要先删除挂载了对应pvc的所有pod。
使用以下命令可以释放pvc
$ kubectl delete -f pvc-cfs.yaml
释放pvc后,原来与之绑定的pv状态会变为release,如下所示:
name capacity access modes reclaim policy status claim storageclass reason age
pv-cfs 8gi rwx retain released default/pvc-cfs 16m
输入以下指令释放pv资源
$ kubectl delete -f pv-cfs.yaml
动态pv/pvc方式挂载cfs
1.创建storageclass和provisioner
dynamic-cfs-template.yaml是一个yaml文件模板,包含了需要创建的集群资源信息。
dynamic-cfs-template.yaml文件内容如下:
kind: clusterrole
apiversion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
rules:
- apigroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apigroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apigroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apigroups: [""]
resources: ["events"]
verbs: ["create", "update", "patch"]
---
kind: clusterrolebinding
apiversion: rbac.authorization.k8s.io/v1
metadata:
name: run-nfs-client-provisioner
subjects:
- kind: serviceaccount
name: nfs-client-provisioner
namespace: kube-system
roleref:
kind: clusterrole
name: nfs-client-provisioner-runner
apigroup: rbac.authorization.k8s.io
---
kind: role
apiversion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
namespace: kube-system
rules:
- apigroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: rolebinding
apiversion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
namespace: kube-system
subjects:
- kind: serviceaccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: kube-system
roleref:
kind: role
name: leader-locking-nfs-client-provisioner
apigroup: rbac.authorization.k8s.io
---
apiversion: v1
kind: serviceaccount
metadata:
name: nfs-client-provisioner
namespace: kube-system
---
apiversion: v1
kind: persistentvolume
metadata:
name: pv-cfs
spec:
capacity:
storage: 5gi
accessmodes:
- readwritemany
persistentvolumereclaimpolicy: retain
mountoptions:
- hard
- nfsvers=4.1
- nordirplus
nfs:
path: {{nfs_path}}
server: {{nfs_server}}
---
kind: persistentvolumeclaim
apiversion: v1
metadata:
name: pvc-cfs
namespace: kube-system
spec:
accessmodes:
- readwritemany
resources:
requests:
storage: 5gi
---
kind: deployment
apiversion: apps/v1
metadata:
name: nfs-client-provisioner
namespace: kube-system
spec:
selector:
matchlabels:
app: nfs-client-provisioner
replicas: 1
strategy:
type: recreate
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceaccountname: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: registry.baidubce.com/cce-plugin-pro/nfs-client-provisioner:latest
imagepullpolicy: always
volumemounts:
- name: nfs-client-root
mountpath: /persistentvolumes
env:
- name: provisioner_name
value: {{provisioner_name}}
- name: nfs_server
value: {{nfs_server}}
- name: nfs_path
value: {{nfs_path}}
volumes:
- name: nfs-client-root
persistentvolumeclaim:
claimname: pvc-cfs
---
apiversion: storage.k8s.io/v1
kind: storageclass
metadata:
name: {{storage_class_name}}
provisioner: {{provisioner_name}}
parameters:
archiveondelete: "{{archive_on_delete}}"
sharepath: "{{share_path}}"
mountoptions:
- hard
- nfsvers=4.1
- nordirplus
dynamic-cfs-template.yaml模板文件中可自定义的选项如下:
nfs_server
: cfs挂载点地址。nfs_path
: cfs远程挂载目录,注意该目录在使用前需要预先存在,如果目录不存在会导致provisioner插件启动失败。share_path
: 不同pvc的cfs挂载目录是否隔离,true-不隔离,false-隔离。若指定隔离,则会在cfs挂载目录下为每个pvc创建一个子目录,对应pvc使用该子目录作为挂载目录;否则所有pvc共享挂载目录。archive_on_delete
: 删除pvc后是否保留对应数据,仅当pvc挂载目录隔离时生效,true-保留,false-不保留;pvc挂载目录共享时,删除pvc不会删除任何数据。设置为不保留则直接删除对应pvc的子目录,否则仅将原子目录名加上archive-
前缀后保留。storage_class_name
: 创建的storageclass名称。provisioner_name
: provisioner名称。
支持shell的系统中,可以直接使用下面的replace.sh脚本进行yaml模板中模板变量的替换操作。
#!/bin/sh
# user defined vars
nfs_server="cfs-test.baidubce.com"
nfs_path="/cce/shared"
share_path="true" # 不同pvc的挂载目录是否隔离,true-不隔离,false-隔离
archive_on_delete="false" # 删除pvc是否保留对应数据,仅当pvc挂载目录隔离时生效,true-保留,false-不保留
storage_class_name="sharedcfs" # storageclass名称
provisioner_name="baidubce/cfs-provisioner" # provisioner名称
yaml_file="./dynamic-cfs-template.yaml"
# replace template vars in yaml file
sed -i "s#{{share_path}}#$share_path#" $yaml_file
sed -i "s#{{archive_on_delete}}#$archive_on_delete#" $yaml_file
sed -i "s#{{storage_class_name}}#$storage_class_name#" $yaml_file
sed -i "s#{{provisioner_name}}#$provisioner_name#" $yaml_file
sed -i "s#{{nfs_server}}#$nfs_server#" $yaml_file
sed -i "s#{{nfs_path}}#$nfs_path#" $yaml_file
将脚本中前半段中的shell变量替换为期望值,将replace.sh脚本和dynamic-cfs-template.yaml文件放置在同一个目录下,执行sh replace.sh
即可。
或者采用其他方式,将模板yaml文件中的模板变量替换为期望值。
最后,使用kubectl工具,执行 kubectl create -f dynamic-cfs-template.yaml
完成storageclass和provisioner的创建。
$ kubectl create -f dynamic-cfs-template.yaml
clusterrole "nfs-client-provisioner-runner" created
clusterrolebinding "run-nfs-client-provisioner" created
role "leader-locking-nfs-client-provisioner" created
rolebinding "leader-locking-nfs-client-provisioner" created
serviceaccount "nfs-client-provisioner" created
persistentvolume "pv-cfs" created
persistentvolumeclaim "pvc-cfs" created
deployment "nfs-client-provisioner" created
storageclass "sharedcfs" created
$ kubectl get pod --namespace kube-system | grep provisioner
nfs-client-provisioner-c94494f6d-dlxmj 1/1 running 0 26s
如果相应的pod进入running状态,则动态绑定pv所需的资源已经建立成功。
2.创建pvc时动态生成pv并绑定
在pvc spec中指定上面创建的storageclass名称,则在创建pvc时,会自动调用相应storageclass绑定的的provisioner生成相应的pv进行绑定。
使用kubectl,执行 kubectl create -f dynamic-pvc-cfs.yaml
完成pvc的创建。
假设创建的storageclass名称为sharedcfs
,对应的 dynamic-pvc-cfs.yaml 文件如下所示
kind: persistentvolumeclaim
apiversion: v1
metadata:
name: dynamic-pvc-cfs
spec:
accessmodes:
- readwritemany
storageclassname: sharedcfs
resources:
requests:
storage: 5gi
创建pvc后,可以看见相应的pv自动创建,pvc状态变为bound
,即pvc已经与新创建的pv绑定。
$ kubectl create -f dynamic-pvc-cfs.yaml
persistentvolumeclaim "dynamic-pvc-cfs" created
$ kubectl get pvc
name status volume capacity access modes storageclass age
dynamic-pvc-cfs bound pvc-6dbf3265-bbe0-11e8-bc54-fa163e08135d 5gi rwx sharedcfs 4s
$ kubectl get pv
name capacity access modes reclaim policy status claim storageclass reason age
pv-cfs 5gi rwx retain bound kube-system/pvc-cfs 21m
pvc-6dbf3265-bbe0-11e8-bc54-fa163e08135d 5gi rwx delete bound default/dynamic-pvc-cfs sharedcfs 7s
3.在pod内挂载pvc
在pod spec内指定相应的pvc名称即可,使用kubectl,执行 kubectl create -f dynamic-cfs-pod.yaml
完成资源的创建。
对应的dynamic-cfs-pod.yaml
文件如下所示:
apiversion: v1
kind: pod
metadata:
name: test-pvc-pod
labels:
app: test-pvc-pod
spec:
containers:
- name: test-pvc-pod
image: nginx
volumemounts:
- name: cfs-pvc
mountpath: "/cfs-volume"
volumes:
- name: cfs-pvc
persistentvolumeclaim:
claimname: dynamic-pvc-cfs
pod创建后,可以读写容器内的/cfs-volume
路径来访问相应的cfs存储上的内容。
4.释放pvc时动态销毁绑定pv
删除pvc时,与之绑定的动态pv会被一同删除,其中的数据则根据用户定义的share_path
和archive_on_delete
选项进行相应的保留或删除处理。
$ kubectl get pvc
name status volume capacity access modes storageclass age
dynamic-pvc-cfs bound pvc-6dbf3265-bbe0-11e8-bc54-fa163e08135d 5gi rwx sharedcfs 9m
$ kubectl get pv
name capacity access modes reclaim policy status claim storageclass reason age
pv-cfs 5gi rwx retain bound kube-system/pvc-cfs 31m
pvc-6dbf3265-bbe0-11e8-bc54-fa163e08135d 5gi rwx delete bound default/dynamic-pvc-cfs sharedcfs 9m
$ kubectl delete -f dynamic-pvc-cfs.yaml
persistentvolumeclaim "dynamic-pvc-cfs" deleted
$ kubectl get pv
name capacity access modes reclaim policy status claim storageclass reason age
pv-cfs 5gi rwx retain bound kube-system/pvc-cfs 31m
$ kubectl get pvc
no resources found.