【K8s存储】使用外部制备器创建NFS存储类说明
参考:
https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner
部署清单
RBAC清单
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
Deployment清单
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: k8s.gcr.io/sig-storage/nfs-subdir-external-provisioner:v4.0.2 ## 实际中可以使用阿里云海外构建所需镜像
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: k8s-sigs.io/nfs-subdir-external-provisioner ## 后面存储类的provisioner值要和这个保持一致
- name: NFS_SERVER
value: 10.3.243.101 ## 必须和volumes设置保持一致
- name: NFS_PATH
value: /ifs/kubernetes ## 必须和volumes设置保持一致
volumes:
- name: nfs-client-root
nfs:
server: 10.3.243.101
path: /ifs/kubernetes
StorageClass清单
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage
annotations:
storageclass.kubernetes.io/is-default-class: "false" ## 是否设置为默认的存储类
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner ## 必须匹配deployment中环境变量PROVISIONER_NAME的值
parameters:
archiveOnDelete: "false"
mountOptions:
- hard
- nfsvers=4
parameters说明:
- onDelete:如果存在且有delete值,则删除该目录,如果存在且有retain值,则保留该目录。默认将在共享上以此名称归档:archived-<volume.Name>
- archiveOnDelete:如果它存在并且值为false,则删除该目录。 如果 onDelete 存在,archiveOnDelete 将被忽略。默认将在共享上以此名称归档:archived-<volume.Name>
- pathPattern:指定用于通过 PVC 元数据(例如标签、注释、名称或命名空间)创建目录路径的模板。 要指定元数据,请使用 ${.PVC.
}。 示例:如果文件夹应命名为 - ,则使用 ${.PVC.namespace}-${.PVC.name} 作为 pathPattern。 默认不适用。
简单测试
特别注意:必须在集群所有节点安装nfs-common。
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-claim
spec:
storageClassName: managed-nfs-storage ## 设置上面创建的存储类
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Mi
---
kind: Pod
apiVersion: v1
metadata:
name: test-pod
spec:
containers:
- name: test-pod
image: gcr.io/google_containers/busybox:1.24
command:
- "/bin/sh"
args:
- "-c"
- "touch /mnt/SUCCESS && exit 0 || exit 1"
volumeMounts:
- name: nfs-pvc
mountPath: "/mnt"
restartPolicy: "Never"
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: test-claim ## 设置上面创建的PVC
局限性
- 不保证配置的存储。 您可以分配超过 NFS 共享的总大小。 共享也可能没有足够的存储空间来实际容纳请求。
- 未实施预配的存储限制。 无论供应的大小如何,应用程序都可以扩展以使用所有可用存储。
- 目前不支持任何形式的存储调整大小/扩展操作。 您最终将处于此错误状态: Ignoring the PVC: didn't find a plugin capable of expanding the volume; waiting for an external controller to process this PVC.