Kubernetes V1.11.1 Kubernetes V1.11.1 Kubernetes V1.11.1 Kubernetes V1.11.1 Kubernetes V1.11.1
The storage in the container is temporary, so when the Pod restarts, the internal data will be lost. In practice, we have some applications that are stateless, some applications that need to keep state data to ensure that the Pod can read the previous state data after restarting, and some applications that provide services as clusters. These three services are summarized as stateless service, stateful service and stateful cluster service, among which the latter two have the requirement of data storage and sharing, so it is necessary to adopt the storage scheme outside the container.
There are four important concepts in storage in Kubernetes: Volume, PersistentVolume PV, PersistentVolumeClaim PVC, and StorageClass. With these four concepts, you are at the heart of the storage system in Kubernetes. Let me draw a picture to illustrate the relationship between these four.
- Volumes is a basic storage abstraction that supports many types of storage, including local storage, NFS, FC, and many cloud storage. You can also write your own storage plug-ins to support specific storage systems. Volume can be used directly by Pod or PV. A normal Volume and Pod are static bindings. When a Pod is defined, pass the
volume
Property to define the type of storage throughvolumeMount
To define mount points within the container. - PersistentVolume. Unlike ordinary volumes, PV is a resource object in Kubernetes. Creating a PV is equivalent to creating a storage resource object, and the use of this resource is requested through PVC.
- PersistentVolumeClaim. PVC is the user’s request for storage resource PV. According to the conditions specified in THE PVC, Kubernetes dynamically searches for PV resources in the system and binds them. At present, PVC and PV matching can pass
StorageClassName
,matchLabels
ormatchExpressions
Three ways. - StorageClass.
Volumes
Docker provides Volumes. Volumes are folders on disk and have no lifecycle management. Volume in Kubernetes is an abstraction of storage and can provide multiple storage solutions for PODS. The Volume will eventually map to a folder or raw device accessible to the container in the Pod, but there are many ways to do this.
The type of Volumes
- cephfs
- configMap
- emptyDir
- hostPath
- local
- nfs
- persistentVolumeClaim
emptyDir
EmptyDir is created after the Pod is assigned to Node and remains for as long as the Pod is running. EmptyDir will be permanently deleted when Pod is removed from Node. The unexpected exit of the Container does not cause emptyDir to be deleted. EmptyDir is suitable for some scenarios where data is temporarily stored. By default, emptyDir is stored on any media supported by Node, whether disk, SSD, or network storage, or can be set to Memory.
apiVersion: v1
kind: Pod
metadata:
name: tomcat-ccb
namespace: default
labels:
app: tomcat
node: devops-103
spec:
containers:
- name: tomcat
image: docker.io/tomcat
volumeMounts:
- name: tomcat-storage
mountPath: /data/tomcat
- name: cache-storage
mountPath: /data/cache
ports:
- containerPort: 8080
protocol: TCP
env:
- name: GREETING
value: "Hello from devops-103"
volumes:
- name: tomcat-storage
hostPath:
path: /home/es
- name: cache-storage
emptyDir: {}
Copy the code
hostPath
HostPath is used to mount a Node file system into a Pod, as you can see in the previous example.
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /test-pd
name: test-volume
volumes:
- name: test-volume
hostPath:
# directory location on host
path: /data
# this field is optional
type: Directory
Copy the code
local
A local volume represents a mounted local storage device such as a disk, partition or directory.
The Local type is used as a static resource by PersistentVolume and does not support Dynamic Provisioning. Compared to hostPath, it is more suitable than the hostPath type because it is scheduled through PersistentVolume’s node affinity policy. There are also some problems with the local type. If the Node state is abnormal, the local storage will not be accessible, which will cause the Pod to run abnormally. Applications that use this type of storage must be able to withstand reduced availability, possible data loss, and so on.
apiVersion: v1
kind: PersistentVolume
metadata:
name: www
spec:
capacity:
storage: 100Mi
volumeMode: Filesystem
accessModes: ["ReadWriteOnce"]
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage
local:
path: /home/es
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- devops-102
- devops-103
Copy the code
For pv-based pods, Kubernetes schedules them to nodes with the corresponding PV, so the PV Node affinity nodeAffinity attribute is required.
PersistentVolume nodeAffinity is required when using local volumes. It enables the Kubernetes scheduler to correctly schedule Pods using local volumes to the correct node.
Persistent Volumes
Persistent Volumes provide an abstraction layer that hides concrete storage implementations from users.
- PersistentVolume PV: A storage provided by the cluster administrator and a plug-in to Volumes. Similar to Pod, but with a Pod independent lifecycle. The storage devices can be NFS or cloud service providers.
- PersistentVolumeClaim PVC: THE PVC is a storage request from the user, and the PVC consumes PV resources.
Life cycle:
-
supply
- Static supply
- Dynamic supply: Dynamic supply requests are based on StorageClass, and the cluster can generate dynamic supply based on PVC requests from users.
-
Binding Binding
-
use
-
Object protection in use: A protection mechanism is provided for a PV in use. If a PV in use is deleted, the deletion of the PV will be delayed until the PV is used by the user.
-
Reuse Reclaim Policy
- Retain Retain: Retain the site while Kubernetes waits for the user to manually process the data.
- Delete Delete: Kubernetes automatically deletes data
- Reuse: This strategy is no longer recommended and should be used Dynamic Provisioning instead.
-
Expansion. Scalability features are available for cloud storage types such as gcePersistentDisk and Azure Disk, which are in beta in version 1.11.
PersistenVolume is currently implemented as a Plugin and is currently available in 19 versions of V1.11.1, with a particular focus on HostPath.
HostPath (Single node testing only – local storage is not supported in any way and WILL NOT WORK in a multi-node cluster)
Some attributes of Persistent Volumes
- Capacity: In common cases, the PV has a fixed Capacity
- Volume Mode: alpha in version 1.9 allows you to set filesystem to use the filesystem (default) and raw to use raw devices.
- Access Modes
- Class: The name that can be set to StorageClass. PVS with Class attributes can only be bound to PVCS with the same Class name. A PV without CLASS can only be bound to a PVC without CLASS.
- Reclaim Policy
state
- Available: not used by any PVC
- Bound: Binds to the PVC
- Released: THE PVC was deleted and the resource was not used
- Failed: Automatic reclamation Failed
PersistentVolumeClaims
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: myclaim
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: 8Gi
storageClassName: slow
selector:
matchLabels:
release: "stable"
matchExpressions:
- {key: environment.operator: In.values: [dev]}
Copy the code
Some of the properties
- Access Modes
- Volume Modes
- Resources
- Selector: PVCS can select PV resources using label selectors. It can contain two fields
matchLabels
andmatchExpressions
. - StorageClassName Is similar to a label selector and determines PV resources by storagClassName.
Storage Class
StorageClass provides administrators with a way to describe storage types. In most cases, administrators need to manually create storage resources. Using the function of dynamic capacity supply, the ability to create PV dynamically can be realized. Dynamic Volume Provisioning relies on the StorageClass.
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
Copy the code
References:
- Kubernetes Storage
- Configure a Pod to Use a PersistentVolume for Storage
- Persistent Volumes
- Kubernetes Storage System Introduction (Volume, PV, Dynamic Provisioning)
- Kubernetes 1.4 features persistent volumes
- Kubernetes stateful Cluster service deployment and management