Kubernetes Storage allows containerized applications to access storage resources seamlessly, without being aware of the containers consuming the data. One of the ways Kubernetes allows applications to access storage is the standard Network File Service (NFS) protocol.
In this post, we offer two quick tutorials that will show how to mount NFS file shares directly from a container in a Kubernetes pod, and set up a Kubernetes Persistent Volume accessed via NFS. In addition, we’ll show how NetApp Cloud Volumes ONTAP can help with setting up enterprise-grade NFS Persistent Volumes.
In this article:
Kubernetes Volumes are abstracted storage units that allow nodes within a cluster to write, read and share data between them. Kubernetes offers many storage plugins that provide access to storage services and platforms. One of these is the NFS plugin.
Network File System (NFS) is a standard protocol that lets you mount a storage device as a local drive. Kubernetes allows you to mount a Volume as a local drive on a container. The NFS integration is very useful for migrating legacy workloads to Kubernetes, because very often legacy code accesses data via NFS.
There are two ways to access data via NFS in Kubernetes:
Here are a few reasons you should consider using NFS in Kubernetes:
One important caveat is that for an NFS Volume to work, you must set up a server that exposes storage via NFS. Kubernetes will not manage your existing NFS Volumes for you.
Here is how to add an NFS Volume to your pod definition, so that containers can mount a share from an existing NFS server:
1. Pod definition
In your pod YAML file, include the following directive under the container definition (substitute bold text with your own data):
volumeMounts: - name: your-nfs-volume mountPath: /var/your-destination
volumes: - name: your-nfs-volume nfs: server: nfs-server.yourdomain.com path:/path/to/shared-folder
Create the pod, and ensure it is deployed correctly, using:
$ kubectl create -f your-pod-definition.yaml $ kubectl get pods
Check that the relevant container has mounted the NFS share correctly:
$ kubectl exec -it your-pod-definition sh / # / # mount \| grep nfs-server.yourdomain.com
The flipside of the Kubernetes NFS integration is that you can set up an NFS Persistent Volume which is managed within Kubernetes.
Below we summarize an official Kubernetes example that shows how to set up a complete application that saves data to an NFS Persistent Volume. See the full details and download the code from the official Kubernetes examples report.
This step will look a bit different depending on which underlying storage you want to use for your NFS service. On Azure, use this command.
1. Define the NFS service
$ kubectl create -f examples/staging/volumes/nfs/provisioner/nfs-server-gce-pv.yaml
Run these commands to create the NFS server from the service definition and expose it as a service. Finally, check that pods have deployed correctly
$ kubectl create -f examples/staging/volumes/nfs/nfs-server-rc.yaml $ kubectl create -f examples/staging/volumes/nfs/nfs-server-service.yaml $ kubectl get pods -l role=nfs-server.
Find the cluster IP of your server using this command:
$ kubectl describe services nfs-server
4. Create the Persistent Volume
Use these commands to set up the persistent volume that uses the NFS service.
$ kubectl create -f examples/staging/volumes/nfs/nfs-pv.yaml $ kubectl create -f examples/staging/volumes/nfs/nfs-pvc.yaml
NetApp Cloud Volumes ONTAP, the leading enterprise-grade storage management solution, delivers secure, proven storage management services on AWS, Azure and Google Cloud. Cloud Volumes ONTAP supports up to a capacity of 368TB, and supports various use cases such as file services, databases, DevOps or any other enterprise workload.
In particular, Cloud Volumes ONTAP can help you setup Kubernetes storage based on NFS, with high-performance data access, built-in backup, high availability, and more. Learn how to provision Kubernetes Persistent Volumes for NFS with Cloud Volumes ONTAP and NetApp Trident.