需要查看部署nfs服务请查看Linux 环境下 NFS 服务安装及配置使用
创建rbac文件
创建nfs-storage-class-rabc.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: ["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
执行创建
kubectl apply -f nfs-storage-class-rabc.yaml
创建storage-class和nfs-client-provisioner
为了方便的话,可以docker先把镜像下载下来。
docker pull registry.cn-hangzhou.aliyuncs.com/cm_ns01/nfs-subdir-external-provisioner:v4.0.2
创建nfs-storage.yaml文件
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-storage
annotations:
storageclass.kubernetes.io/is-default-class: "true"
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner
parameters:
archiveOnDelete: "true"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-client-provisioner
namespace: default
labels:
app: nfs-client-provisioner
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: registry.cn-hangzhou.aliyuncs.com/cm_ns01/nfs-subdir-external-provisioner:v4.0.2
imagePullPolicy: IfNotPresent
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: k8s-sigs.io/nfs-subdir-external-provisioner # 此处供应者名字供storageclass调用
- name: NFS_SERVER
value: k8s-master # 填入NFS的地址
- name: NFS_PATH
value: /nfs # 填入NFS挂载的目录
volumes:
- name: nfs-client-root
nfs:
server: k8s-master # 填入NFS的地址
path: /nfs # 填入NFS挂载的目录
执行创建
kubectl apply -f nfs-storage.yaml
创建pvc测试是否可以挂载绑定
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: flink-pvc
namespace: flink
spec:
storageClassName: nfs-storage
resources:
requests:
storage: 2Gi #设置 pvc 存储资源大小
accessModes:
- ReadWriteOnce
kubectl apply -f flink-pvc.yaml
kubectl get pvc -n flink
可以看到pvc的status已经是bound状态,就代表挂载成功。
也可以在本地磁盘路径下看看目录是否创建。