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:
- What are Kubernetes Volumes and how they work with NFS
- Advantages of using NFS with Kubernetes
- Quick tutorial #1: Mounting an NFS share on a container
- Quick tutorial #2: Creating an NFS Persistent Volume
- Kubernetes NFS with Cloud Volumes ONTAP
Kubernetes Volumes and NFS
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:
- Ephemeral NFS Volume - this lets you connect to existing NFS storage you already possess.
- Persistent Volume with NFS - this lets you set up a managed resource within the cluster which is accessed via NFS. To understand the basics of persistency in Kubernetes, see our post on Kubernetes Persistent Volumes.
Advantages of Using NFS with Kubernetes
Here are a few reasons you should consider using NFS in Kubernetes:
- Use existing storage - you can mount existing data volumes you are currently using, on-premises or in the cloud, with a standard interface.
- Persistency - a regular Kubernetes Volume is ephemeral, meaning it is torn down when its parent pod shuts down. However, an NFS Volume, which you define within pod definitions, gives you persistency without having to define a Persistent Volume. Any data saved via NFS will be stored in the connected storage device, even after the pod shuts down. You also have the option of defining a Kubernetes Persistent Volume that exposes its data via an NFS interface.
- Share data - because of its persistent nature, NFS Volumes can be used to share data between containers, whether in the same pod or different pods.
- Simultaneous mounting - NFS Volumes can be mounted by multiple nodes at the same time, and multiple nodes can write to the same NFS Volume at the same time.
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.
Quick Tutorial #1: Mounting an Ephemeral NFS Share on a Container
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):
- name: your-nfs-volume
Define the volume as follows (substitute bold text with your own data):
- name: your-nfs-volume
2. Deploy the pod
Create the pod, and ensure it is deployed correctly, using:
$ kubectl create -f your-pod-definition.yaml
$ kubectl get pods
3. Verify NFS share is working
Check that the relevant container has mounted the NFS share correctly:
$ kubectl exec -it your-pod-definition sh
/ # mount | grep nfs-server.yourdomain.com
Quick Tutorial #2: Setting up an NFS Persistent Volume
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.
1. Define the NFS service
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.
$ kubectl create -f examples/staging/volumes/nfs/provisioner/nfs-server-gce-pv.yaml
2. Create an NFS server and service
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.
3. Create the Persistent Volume Claim
Find the cluster IP of your server using this command:
$ kubectl describe services nfs-server
Now, edit the NFS Persistent Volume and replace the IP with the correct one. You need to hard-code the IP for now because there are no service names yet.
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
That’s it! In the full example, you can run a fake backend and web server that use the NFS Persistent Volume to share data between them.
Kubernetes NFS with Cloud Volumes ONTAP
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, with a strong set of features including high availability, data protection, storage efficiencies, cloud automation, and more.
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.
Want to learn more about Kubernetes Storage?
Have a look at these articles:
- An Introduction to Kubernetes
- Kubernetes Persistent Storage: Why, Where and How
- AWS Kubernetes Cluster: Quick Setup with EC2 and EKS
- AWS ECS vs Kubernetes: An Unfair Comparison?
- Understanding Kubernetes Persistent Volume Provisioning
- Managing Stateful Applications in Kubernetes
- Dynamic Kubernetes Persistent Volume Provisioning with NetApp Trident and Cloud Volumes ONTAP
- Data Protection for Persistent Data Storage in Kubernetes Workloads
- Storage Efficiency for Improving Persistent Volume Storage Costs
- Kubernetes Persistent Volumes Cloning with NetApp Trident and Cloud Volumes ONTAP
- How to Set Up MySQL Kubernetes Deployments with Cloud Volumes ONTAP
Want to get started? Try out Cloud Volumes ONTAP today with a 30-day free trial.