Kubernetes Volumes in Rancher
Contents
What Are Kubernetes Volumes?
A Kubernetes volume is a way of storing data within a container that is running on a Kubernetes cluster. The data stored in a volume can persist even if the container is deleted or recreated. A Kubernetes volume is attached to a container and can be accessed by the container through the file system. The volume is a separate resource from the container and can be managed independently of the container, which makes it easy to add, change, or delete a volume without affecting the running container.
There are several types of Kubernetes volumes, including emptyDir, hostPath, configMap, secret, and PersistentVolumeClaim. Each type of volume is suited to different use cases, such as storing temporary data in an emptyDir volume or storing sensitive data in a secret volume.
In addition to the standard volume types, Kubernetes also supports custom volumes by using plugins. Custom volumes are third-party storage systems that are integrated with Kubernetes and can be used in the same way as standard volumes. This allows users to easily access storage from their preferred storage provider.
How Persistent Storage Works in Rancher
Rancher is an open-source software platform that enables users to manage and deploy containers on multiple hosts. It provides a centralized interface for deploying and managing applications running in containers, including Kubernetes clusters. Rancher improves cloud operations, by simplifying the process of managing containers, making it easier for organizations to deploy and manage applications at scale.
In Rancher, persistent storage is managed using persistent volume claims (PVCs) and persistent volumes (PVs).
A PVC is a request for a certain amount of storage made by a pod. Rancher uses this request to match the PVC to a PV, which is a physical piece of storage. When a PVC is created, Rancher automatically creates a PV and assigns it to the PVC. The PV is then mounted to the pod, providing it with access to the persistent storage.
Rancher supports multiple storage providers for persistent storage, including local storage, network attached storage (NAS), and cloud storage services. Organizations can choose the storage provider that best meets their needs and requirements.
Users can create PVCs, manage PVs, and monitor the utilization of persistent storage from a single interface. This makes it easier for organizations to manage persistent storage in Rancher and ensure that their applications have access to the data they need.
Persistent Volume Claims
Persistent Volume Claims (PVCs) are a feature in Kubernetes that allows containers to access persistent storage. They work by making a request for a certain amount of storage to the Kubernetes cluster. Rancher allows users to create an unlimited number of PVCs in a project.
A PVC is created by a user or an application and is essentially a request for a certain amount of storage that should persist even if the container is deleted or recreated. When a PVC is created, the Kubernetes cluster looks for a Persistent Volume (PV) that can satisfy the request. If a matching PV is found, the PVC is bound to the PV, and the PV is then mounted to the container.
The PVC and PV work together to provide the container with access to the persistent storage. The PVC defines the storage requirements, while the PV provides the actual storage. The PVC and PV are separate resources, which makes it easy to manage them independently of the container.
Set Up Existing Storage with a PV and PVC
A Persistent Volume (PV) is a physical piece of storage that is made available to the Kubernetes cluster. Without PVs, the data in your storage volumes is vulnerable to deletion, restarts, and other issues with ephemeral volumes.
To set up existing storage with a PV, you will need to define a YAML file that describes the PV. This file should include information such as the name of the PV, the capacity, and the access modes. After defining the PV in the YAML file, you can create the PV in your Kubernetes cluster by using the kubectl create command.
The final step is to mount the PV to the pod. This is done automatically by the Kubernetes cluster once the PVC is bound to the PV. The pod can now access the persistent storage provided by the PV.
Tutorial: Set Up Existing Persistent Storage for Workloads in Rancher
Here is an example of the steps involved in setting up existing storage for your Rancher workloads. You can follow this tutorial to see how to install Rancher on an Amazon EC2 instance.
Setting up persistent storage:
- Log in to your Rancher server.
- Go to the Infrastructure tab, and click on the Cluster that you want to set up persistent storage for.
- Click on the Storage tab, and then click on the Add Storage button.
- Select the type of storage you want to provision (e.g. NFS, AWS EBS, etc.). Fill in the required details for the storage type you selected.
- Click on the Create button to provision the storage.
Adding a PV that references the provisioned storage:
- Click on the Workloads tab in Rancher.
- Click on the Deploy button.
- Select the Kubernetes YAML option.
- Paste the following YAML code into the editor, replacing the placeholder values with the actual values for your environment:
apiVersion: v1
kind: PersistentVolume
metadata:
name: mypv
spec:
capacity:
storage: 10Gi
accessModes:
– ReadWriteOnce
nfs:
path: /path/to/nfs
server: nfsserver
- Click on the Deploy button to create the PV in your cluster.
Using StatefulSets and the storage class to deploy pods:
- Go to the Kubernetes YAML editor.
- Deploy a service called web using the following YAML configuration:
apiVersion: v1
kind: Service
metadata:
name: web
namespace: default
labels:
app: web
spec:
ports:
– port: 80
name: web
clusterIP: None
selector:
app: web
- Create a StatefulSet using the following YAML configuration, replacing the placeholder values with the actual values for your environment:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
namespace: default
spec:
serviceName: web
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
– name: web
image: nginx
ports:
– containerPort: 80
volumeMounts:
– name: mypvc
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
metadata:
name: mypvc
spec:
accessModes: [ “ReadWriteOnce” ]
resources:
requests:
storage: 1Gi
storageClassName: standard
- Click on the Deploy button to create the StatefulSet in your cluster.
This will deploy a StatefulSet with a single pod that is connected to the PV you created earlier. The PVC in the StatefulSet definition references the storage class, which maps to the PV. When the PVC is created, it will be automatically bound to the PV, providing the pod with access to the persistent storage.
With this setup, your pods will have access to persistent storage, which will persist even if the pods are deleted or recreated. This provides a flexible and scalable way to store data for your workloads in Rancher.
Conclusion
Using Kubernetes volumes in Rancher provides a robust and scalable solution for managing persistent storage in a Kubernetes cluster. By provisioning storage, creating persistent volumes, and utilizing StatefulSets and storage classes, Rancher makes it easy for administrators to manage storage for their workloads.
With the ability to easily manage storage for your workloads, Rancher provides a powerful tool for ensuring that your data is safe and accessible even as your environment evolves over time. Whether you are just starting out with Kubernetes or are an experienced administrator, Rancher provides a flexible and scalable solution for working with Kubernetes volumes.