kubernetes-sigs / nfs-subdir-external-provisioner
创建 StatefulSet 应用时要求必须有且有一个默认 StorageClass,参考官网创建 StorageClass 对象但是里面的provisioner
属性配置总是出现问题。
选择nfs
作为网络存储系统搭建 StatefulSet ,但是nfs
没有内部制备器,所以需要使用第三方存储供应商提供自己的外部制备器。
目标:创建nfs-client
的StorageClass
,以实现动态供给。
下载地址:https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner
在deploy
目录下修改配置,或者自己创建所需的三个文件,class.yaml
创建存储类:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-client # 存储类名字
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
archiveOnDelete: "false" # pvc删除后,pv不删除(nfs共享目录不删除),设置true会删除
deployment.yaml
部署nfs-client-provisioner
这里除了nfs
配置信息还改了镜像地址,因为默认的镜像下载地址在国外。
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:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: dyrnq/nfs-subdir-external-provisioner:v4.0.2 # dockerhub 地址:https://hub.docker.com/r/dyrnq/nfs-subdir-external-provisioner/tags
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: k8s-sigs.io/nfs-subdir-external-provisioner
- name: NFS_SERVER
value: 192.168.150.104 # nfs服务器IP,改成自己的
- name: NFS_PATH
value: /root/data/nfs # nfs服务器共享目录
volumes:
- name: nfs-client-root
nfs:
server: 192.168.150.104 # nfs服务器IP,改成自己的
path: /root/data/nfs # nfs服务器共享目录
rbac.yaml
授权(连接k8s-api)
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存储(动态存储):
kubectl apply -f class.yaml
kubectl apply -f deployment.yaml
kubectl apply -f rbac.yaml
将存储类设置为默认存储类:
kubectl patch storageclass nfs-client -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
创建成功kubectl get storageclass
:
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs-client (default) k8s-sigs.io/nfs-subdir-external-provisioner Delete Immediate false 90s
kubectl apply -f pvc.yaml
创建pvc
测试
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
spec:
storageClassName: "nfs-client" #存储类的名字,只需要加这一个参数
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
可以看到pv
已经被自动创建kubectl get pv
:
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-cf4ced81-07ad-4082-acba-3a26858d7acd 1Gi RWX Delete Bound default/nfs-pvc nfs-client 33s