Extend Kubernetes with NFS and Dynamic provisioning

NFS-Server

pic by sysadmins.co.za
apt install nfs-kernel-server nfs-common
vim /etc/default/nfs-common
# find NEED_STATD= and replace it with:
NEED_STATD=yes
systemctl status nfs-serversystemctl restart nfs-server
mkdir -p /opt/nfs/k8s_test# not recommended but will do it for the test
chmod -R 777 /opt/nfs
# to open it for every host:
/opt/nfs/k8s_test *(rw,sync,no_subtree_check,no_root_squash,insecure)
# to just open it for a specific cidr:
/opt/nfs/k8s_test 192.168.150.0/255.255.255.0(rw,sync,no_subtree_check,no_root_squash,insecure)
# Enable and validate the configuration
$ exportfs -rv
exporting 192.168.150.0/255.255.255.0:/opt/nfs/k8s_test

Kubernetes-Setup

pic by softwareengineeringdaily.com
---
kind: ServiceAccount
apiVersion: v1
metadata:
name: nfs-pod-provisioner-sa
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1 # auth API
metadata:
name: nfs-provisioner-clusterRole
rules:
- apiGroups: [""] # rules on persistentvolumes
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: nfs-provisioner-rolebinding
subjects:
- kind: ServiceAccount
name: nfs-pod-provisioner-sa # defined on top of file
namespace: kube-system
roleRef:
kind: ClusterRole
name: nfs-provisioner-clusterRole # name defined in clusterRole
apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-pod-provisioner-otherRoles
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-pod-provisioner-otherRoles
subjects:
- kind: ServiceAccount
name: nfs-pod-provisioner-sa
namespace: kube-system
roleRef:
kind: Role
name: nfs-pod-provisioner-otherRoles
apiGroup: rbac.authorization.k8s.io
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
# NOTE: The PVC needs to reference to this
name: nfs-storageclass
provisioner: nfs-test
parameters:
archiveOnDelete: "false"
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: nfs-pod-provisioner
spec:
replicas: 1
selector:
matchLabels:
app: nfs-pod-provisioner
strategy:
type: Recreate
template:
metadata:
labels:
app: nfs-pod-provisioner
spec:
serviceAccountName: nfs-pod-provisioner-sa
containers:
- name: nfs-pod-provisioner
image: quay.io/external_storage/nfs-client-provisioner:latest
volumeMounts:
- name: nfs-provisioner-v
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: nfs-test
- name: NFS_SERVER # do not change
value: <NFS>
- name: NFS_PATH # do not change
value: /opt/nfs/k8s_test
volumes:
- name: nfs-provisioner-v # same as volumemouts name
nfs:
server: <NFS>
path: /opt/nfs/k8s_test
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc-test
spec:
storageClassName: nfs-storageclass
accessModes:
- ReadWriteMany
resources:
requests:
storage: 50Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nfs-nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
volumes:
- name: nfs-test
persistentVolumeClaim:
claimName: nfs-pvc-test
containers:
- image: nginx
name: nginx
volumeMounts:
- name: nfs-test
mountPath: mydata2

--

--

Working as a IT-Operations engineer at NeXenio, a spin-off by Hasso-Plattner-Institute for products around a digitial workspace.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store