docker storage
When the container is deleted, the data is also deleted. For the data that needs to be applied for persistence, the storage must be configured, which is usually based on storage on the node (and the node storage can be NFS, ceph, iscsi, etc.)
Pod storage of K8s
Since Pod is a collection based on containers, storage sharing is realized based on the pause container
K8s storage volume The
role of storage volume is that due to the structure of the container, data will be deleted as the container is deleted, so Pod needs to use storage volume configuration for data sharing and persistence.
1. Use configuration structure
- Docker: The storage usage of docker is directly relative to the container itself
- Configure resources, based on the node directory or NFS, ceph must be configured on the node
- Mount use: docker run -v /root/xxxx:/root/xxxx (Docker storage is divided into 2 types, you can see the previous article for details)
- Pod: Pod storage is defined based on Pod resources, because Pod is not a single individual
- The storage needs to be defined first, that is, the storage resources are prepared, the volumes field (Pod layer)
- Container mount storage resources (bind storage resources), volumeMounts field (containers layer)
2. Storage volume classification
- Shared storage volume: for temporary data use or based on use within a Pod
- Persistent shared storage volume: data can be persistent shared storage, and will not be deleted with the container life cycle
Shared storage volume
- emptyDir: similar to docker running directly, and Pod is the content container that needs to share data, and it will disappear with the Pod life cycle
- Can provide high performance based on memory implementation
Persistent shared storage volume
- hostPath: Node-based directory storage. The node can be NFS storage or ceph storage, but the controller's copy strategy cannot be implemented. Data can only be persisted and shared on the node (suitable for daemonset controllers, only responsible for collecting data on their respective nodes)
- nfs/cinder/ceph...: based on network storage to realize true persistent shared storage
Example (NFS)
Prerequisites The
node node needs to install the nfs client
apiVersion: apps/v1
kind: Deployment
metadata:
name: centos-nfs
namespace: default
labels:
app: centos
version: "7.6"
spec:
replicas: 1
selector:
matchLabels:
app: centos
version: "7.6"
template:
metadata:
labels:
app: centos
version: "7.6"
spec:
containers:
- name: centos-nfs
image: centos:7.6.1810
imagePullPolicy: IfNotPresent
command: [ /usr/sbin/init ]
volumeMounts: #挂载存储
- mountPath: /mnt #容器内挂载目录
name: nfs #定义存储的name名称
volumes: #定义存储
- name: nfs #名称
nfs: #存储类型
path: /nfs_share #nfs定义的目录
server: 192.168.12.10 #服务器地址
readOnly: true #开启只读,volumeMounts也可以设置针对容器,这里开启就是针对Pod
- Reference: Books-kubernetes advanced combat-Ma Yongliang