DEV Community

Amritanshu Pandey
Amritanshu Pandey

Posted on

Setup NFS Storage Class on Kubernetes

Create the following resources in order to setup an NFS storgae provisioner in a Kubernetes cluster. This allows creating Kubernetes volumes on an NFS server.

Since the code required for creating various K8S resources for NFS provisioner is split into
multiple files, I prefer to just clone this gist and using kubectl to create all resources
in one go

$ git clone https://gist.github.com/amritanshu-pandey/8ab00179c98720cbc28d8bb0c7064426
$ cd 8ab00179c98720cbc28d8bb0c7064426
$ kubectl apply -f *.yaml
Enter fullscreen mode Exit fullscreen mode
kind: Deployment
apiVersion: apps/v1
metadata:
name: nfs-client-provisioner
spec:
selector:
matchLabels:
app: nfs-client-provisioner
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: gcr.io/k8s-staging-sig-storage/nfs-subdir-external-provisioner:v4.0.0
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: <ENTER_PROVISIONER_NAME_HERE>
- name: NFS_SERVER
value: <NFS_HOST_HERE>
- name: NFS_PATH
value: <NFS_PATH_HERE>
volumes:
- name: nfs-client-root
nfs:
server: <NFS_HOST_HERE>
path: <NFS_PATH_HERE>
view raw deployment.yaml hosted with ❤ by GitHub
kind: ServiceAccount
apiVersion: v1
metadata:
name: nfs-client-provisioner
---
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
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
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
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
view raw rbac.yaml hosted with ❤ by GitHub
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage
annotations:
storageclass.kubernetes.io/is-default-class: "true"
provisioner: <ENTER_PROVISIONER_NAME_HERE>
parameters:
archiveOnDelete: "true"

Note: This article was originally posted at https://blog.amritanshu.in/posts/setup-nfs-storage-class-on-kubernetes/

Top comments (1)

Collapse
 
prasannjeet profile image
Prasannjeet Singh

Thank you for helping everyone! Would be nice to have just some explanations about what the above yaml's.