k8s之StorageClass(NFS)
实验环境
k8s环境参考: k8s-v1.20.10 二进制部署指导文档
NFS-Subdir-External-Provisioner 简介
NFS-Subdir-External-Provisioner
是一个自动配置卷程序,它使用现有的和已配置的 NFS 服务器来支持通过持久卷声明动态配置 Kubernetes 持久卷,PV命名规则如下
- PV以
${namespace}-${pvcName}-${pvName}
的命名格式提供(在NFS服务器上) - PV回收的时候以
archieved-${namespace}-${pvcName}-${pvName}
的命名格式(在NFS服务器上)
NFS-Subdir-External-Provisioner
此组件是对nfs-client-provisioner 的扩展
,nfs-client-provisioner 已经不提供更新,且 nfs-client-provisioner 的 Github 仓库已经迁移到 NFS-Subdir-External-Provisioner的仓库,GitHub地址
NFS服务器配置
所以节点必须安装nfs-utils
# 具体配置过程略,这里仅看下nfs配置
[root@k8s-node-1 k8s-nfs]# cat /etc/exports
/data/k8s-nfs/nfs-provisioner 192.168.0.0/24(rw,no_root_squash)
创建ServiceAccount
[root@k8s-master-1 storageClass]# cat serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get", "list", "watch"]
- 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
# replace with namespace where provisioner is deployed
namespace: default
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
# replace with namespace where provisioner is deployed
namespace: default
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
# replace with namespace where provisioner is deployed
namespace: default
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
roleRef:
kind: Role
name: leader-locking-nfs-client-provisioner
apiGroup: rbac.authorization.k8s.io
部署 NFS-Subdir-External-Provisioner
[root@k8s-master-1 storageClass]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-client-provisioner
labels:
app: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: nfs-client-provisioner
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
nodeName: k8s-master-1 #设置在master节点运行
tolerations: #设置容忍master节点污点
- key: node-role.kubernetes.io/master
operator: Equal
value: "true"
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: registry.cn-hangzhou.aliyuncs.com/jiayu-kubernetes/nfs-subdir-external-provisioner:v4.0.0
imagePullPolicy: IfNotPresent
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: k8s/nfs-subdir-external-provisioner
- name: NFS_SERVER
value: 192.168.0.11
- name: NFS_PATH
value: /data/k8s-nfs/nfs-provisioner
volumes:
- name: nfs-client-root
nfs:
server: 192.168.0.11 # NFS SERVER_IP
path: /data/k8s-nfs/nfs-provisioner
# 查看pods
[root@k8s-master-1 storageClass]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-67d8f9ffff-vfvgd 1/1 Running 0 2m56s
创建NFS StorageClass
[root@k8s-master-1 storageClass]# cat storage.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage
annotations:
storageclass.kubernetes.io/is-default-class: "false" # 是否设置为默认的storageclass
provisioner: k8s/nfs-subdir-external-provisioner # or choose another name, must match deployment's env PROVISIONER_NAME'
allowVolumeExpansion: true
parameters:
archiveOnDelete: "false" # 设置为"false"时删除PVC不会保留数据,"true"则保留数据
# 查看storageclass
[root@k8s-master-1 storageClass]# kubectl get storageclass
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
managed-nfs-storage k8s/nfs-subdir-external-provisioner Delete Immediate true 19m
创建PVC
[root@k8s-master-1 storageClass]# cat pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-claim
spec:
storageClassName: managed-nfs-storage
accessModes: ["ReadWriteMany","ReadOnlyMany"]
resources:
requests:
storage: 100Mi
# 查看pvc
[root@k8s-master-1 storageClass]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
nfs-claim Bound pvc-0258363b-6549-481e-9a6e-5dd4e1eca1ea 100Mi ROX,RWX managed-nfs-storage 3s
[root@k8s-master-1 storageClass]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-0258363b-6549-481e-9a6e-5dd4e1eca1ea 100Mi ROX,RWX Delete Bound default/nfs-claim managed-nfs-storage 12s
# k8s-node-1节点查看存储
[root@k8s-node-1 nfs-provisioner]# tree default-nfs-claim-pvc-0258363b-6549-481e-9a6e-5dd4e1eca1ea/
default-nfs-claim-pvc-0258363b-6549-481e-9a6e-5dd4e1eca1ea/
创建Pod并绑定PV
[root@k8s-master-1 storageClass]# cat busybox.yaml
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: test-pod
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- "/bin/sh"
args:
- "-c"
- "sleep 3600"
volumeMounts:
- name: nfs-pvc
mountPath: "/mnt/busybox"
restartPolicy: "Never"
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: nfs-claim
[root@k8s-master-1 storageClass]# kubectl exec -it test-pod -- ls /mnt/busybox/
[root@k8s-master-1 storageClass]# kubectl exec -it test-pod -- touch /mnt/busybox/test.txt
#k8s-node-1查看
[root@k8s-node-1 default-nfs-claim-pvc-bf5f8a3a-92ee-4ea4-baf1-156cdb87230e]# pwd
/data/k8s-nfs/nfs-provisioner/default-nfs-claim-pvc-bf5f8a3a-92ee-4ea4-baf1-156cdb87230e
[root@k8s-node-1 default-nfs-claim-pvc-bf5f8a3a-92ee-4ea4-baf1-156cdb87230e]# ls
test.txt