共计 4496 个字符,预计需要花费 12 分钟才能阅读完成。
1、什么是StorageClass
StorageClass对象会定义下面两部分内容:
- 1: PV的属性.比如,存储类型,Volume的大小等.
- 2: 创建这种PV需要用到的存储插件
有了这两个信息之后,Kubernetes就能够根据用户提交的PVC,找到一个对应的StorageClass,之后Kubernetes就会调用该StorageClass声明的存储插件,进而创建出需要的PV。 但是其实使用起来是一件很简单的事情,你只需要根据自己的需求,编写YAML文件即可,然后使用kubectl create命令执行即可
2、流程
- 创建一个可用的NFS Serve
- 创建Service Account.这是用来管控NFS provisioner在k8s集群中运行的权限
- 创建StorageClass.负责建立PVC并调用NFS provisioner进行预定的工作,并让PV与PVC建立管理
- 创建NFS provisioner.有两个功能,一个是在NFS共享目录下创建挂载点(volume),另一个则是建了PV并将PV与NFS的挂载点建立关联
server | ip | comment |
---|---|---|
kubernetes-master | 172.20.5.10 | master |
kubernetes-node1 | 172.20.5.11 | node1 |
kubernetes-node2 | 172.20.5.12 | node2 |
nfs-server | 172.20.2.10 | NFS |
3、创建StorageClass
3.1、创建NFS共享服务
[root@nfs nfsdata]# yum install -y nfs-utils
[root@nfs nfsdata]# mkdir /nfsdata
[root@nfs nfsdata]# chmod 755 /nfsdata
[root@nfs nfsdata]# chown nfsnobody:nfsnobody /nfsdata/
[root@nfs nfsdata]# echo "/nfsdata *(rw,sync,all_squash)" > /etc/exports
[root@nfs nfsdata]# systemctl enable nfs
[root@nfs nfsdata]# systemctl start nfs
[root@nfs nfsdata]# exportfs -rv
exporting *:/nfsdata
3.2、创建account及相关权限
nfs-rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-provisioner
# replace with namespace where provisioner is deployed
namespace: nfs-provisioner #根据实际环境设定namespace,下面类同
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-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-provisioner
subjects:
- kind: ServiceAccount
name: nfs-provisioner
# replace with namespace where provisioner is deployed
namespace: nfs-provisioner
roleRef:
kind: ClusterRole
name: nfs-provisioner-runner
apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-provisioner
# replace with namespace where provisioner is deployed
namespace: nfs-provisioner
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-provisioner
subjects:
- kind: ServiceAccount
name: nfs-provisioner
# replace with namespace where provisioner is deployed
namespace: nfs-provisioner
roleRef:
kind: Role
name: leader-locking-nfs-provisioner
apiGroup: rbac.authorization.k8s.io
3.3、创建NFS资源的StorageClass
nfs-storageclass.yaml
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: nfs
provisioner: example.com/nfs #这里的名称要和provisioner配置文件中的环境变量PROVISIONER_NAME保持一致
3.4、创建NFS provisioner Deployment
nfs-deployment.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
name: nfs-provisioner
namespace: nfs-provisioner
spec:
replicas: 1
selector:
matchLabels:
app: nfs-provisioner
strategy:
type: Recreate
template:
metadata:
labels:
app: nfs-provisioner
spec:
serviceAccount: nfs-provisioner
containers:
- name: nfs-provisioner
image: registry.cn-hangzhou.aliyuncs.com/open-ali/nfs-client-provisioner
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: example.com/nfs #provisioner名称,请确保该名称与 nfs-StorageClass.yaml文件中的provisioner名称保持一致
- name: NFS_SERVER
value: 172.20.2.10 #NFS Server IP地址
- name: NFS_PATH
value: /nfsdata #NFS挂载卷
volumes:
- name: nfs-client-root
nfs:
server: 172.20.2.10 #NFS Server IP地址
path: /nfsdata #NFS挂载卷
[root@kubernetes-master nfs]# ll
total 20
-rw-r--r-- 1 root root 894 Dec 9 13:55 nfs-deployment.yaml
-rw-r--r-- 1 root root 65 Dec 9 14:50 nfs-namespace.yaml
-rw-r--r-- 1 root root 1449 Dec 9 15:15 nfs-rbac.yaml
-rw-r--r-- 1 root root 100 Dec 9 14:18 nfs-storageclass.yaml
4、创建Pod,检测部署
pvc-nfs.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-nfs
spec:
accessModes:
- ReadWriteOnce
storageClassName: nfs
resources:
requests:
storage: 1Gi
[root@kubernetes-master nfs]# kubectl apply -f pvc-nfs.yaml
persistentvolumeclaim/pvc-nfs created
[root@kubernetes-master nfs]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-nfs Bound pvc-7461b40b-f52e-4bf6-9d73-40432b77f108 1Gi RWO nfs 1s
创建测试pod,查看是否可以正常挂载
kind: Pod
apiVersion: v1
metadata:
name: test-pod
spec:
containers:
- name: test-pod
image: busybox:1.24
command:
- "/bin/sh"
args:
- "-c"
- "touch /mnt/SUCCESS && exit 0 || exit 1" #创建一个SUCCESS文件后退出
volumeMounts:
- name: pvc-nfs
mountPath: "/mnt"
restartPolicy: "Never"
volumes:
- name: pvc-nfs
persistentVolumeClaim:
claimName: pvc-nfs #与PVC名称保持一致
[root@nfs nfsdata]# ll default-pvc-nfs-pvc-7461b40b-f52e-4bf6-9d73-40432b77f108/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 9 15:29 SUCCESS #下面有一个 SUCCESS 的文件,证明我们上面的验证是成功
正文完