Storage volume usage in kubernetes

Table of contents

1. Why use storage volumes?

2. emptyDir storage volume

1. Concept

2. Create Pod emptyDir

3. Verify emptyDir storage volume

 3. hostPath storage volume

1. Concept

2. Create Pod hostPath

 3. Verify hostPath storage volume

3. nfs shared storage volume

1. Concept

2. Install nfs and configure nfs service

 3.Create Pod

4. Verify nfs storage volume


1. Why use storage volumes?

The life cycle of files on the container disk is short-lived, which causes some problems when running important applications in the container. First, when a container crashes, the kubelet will restart it, but the files in the container will be lost - the container is restarted in a clean state (the original state of the image). Secondly, when multiple containers are running simultaneously in a Pod, files usually need to be shared between these containers. The Volume abstraction in Kubernetes solves these problems very well. Containers in the Pod share the Volume through the Pause container.

2. emptyDir storage volume

1. Concept

When a Pod is assigned to a node, the emptyDir volume is first created and exists as long as the Pod is running on that node. As the name of the volume states, it is initially empty. Containers in a Pod can read and write the same files in the emptyDir volume, although the volume can be mounted to the same or different paths in each container. When a Pod is removed from a node for any reason, the data in emptyDir is permanently deleted.

2. Create Pod emptyDir

vim pod-emptydir.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-emptydir
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
  - name: myapp
    image: nginx:1.14
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    volumeMounts:                              #定义容器挂载详细信息
    - name: html                               #挂载存储卷的名称,如果跟下面volume字段name值相同,则表示使用volume的这个存储卷
      mountPath: /usr/share/nginx/html/        #挂载到容器中的目录路径
  - name: busybox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: html
      mountPath: /data/                        #挂载到容器中的目录路径
    command: ["/bin/sh","-c","while true;do echo $(date) >> /data/index.html;sleep 2;done"]  #执行死循环,向挂载的目录下文件写入数据
  volumes:         #定义存储卷
  - name: html     #定义存储卷名称
    emptyDir: {}   #定义存储卷类型

3. Verify emptyDir storage volume

kubectl apply -f pod-emptydir.yaml
kubectl get pods -o wide

在上面定义了2个容器,其中一个容器是输入日期到index.html中,然后验证访问nginx的html是否可以获取日期。以验证两个容器之间挂载的emptyDir实现共享。
curl 10.244.2.58

 3. hostPath storage volume

1. Concept

hostPath allows mounting the file system on the Node into the Pod. If the Pod needs to use files on the Node, hostPath can be used. Pods running on the same node and using the same path in their hostPath volume can see the same files.

  • The hostPath volume mounts files or directories in the node's file system to the cluster.
  • hostPath can achieve persistent storage, but it will also cause data loss when the node node fails.

2. Create Pod hostPath

vim pod-hostpath.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-hostpath
spec:
  containers:
  - name: myapp
    image: nginx:1.14
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: false
  volumes:                       #volumes字段定义了paues容器关联的宿主机或分布式文件系统存储卷
  - name: html                   #定义存储卷名称              
    hostPath:                    #定义宿主机存储路径
      path: /data/pod/volume1    #挂载宿主机目录的路径
      type: DirectoryOrCreate    #定义类型,如果宿主机没有此目录则自动创建

 3. Verify hostPath storage volume

kubectl apply -f pod-hostpath.yaml 
kubectl get pods -owide

#在node01节点
echo 'node01.com' > /data/pod/volume1/index.html

curl 10.244.2.60

3. nfs shared storage volume

1. Concept

NFS is the abbreviation of Network File System, which is the network file system. In Kubernetes, NFS can be mounted to a Pod through simple configuration, and the data in NFS can be permanently saved. At the same time, NFS supports simultaneous write operations.

emptyDir can provide file sharing between different containers, but cannot store it; hostPath can provide file sharing and storage for different containers, but is subject to node restrictions and cannot be shared across nodes; in this case, network storage (NAS) is required, that is, both It is convenient to store containers and can be accessed from any cluster node. This article uses NFS as an example for testing.

2. Install nfs and configure nfs service

#配置nfs服务
mkdir /data/volumes -p
chmod 777 /data/volumes
echo '<h1> nfs <h1>' > /data/volumes

vim /etc/exports
/data/volumes 192.168.88.0/24(rw,no_root_squash,sync)

systemctl start rpcbind
systemctl start nfs

showmount -e

 3.Create Pod

vim pod-nfs.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-nfs
spec:
  containers:
  - name: myapp
    image: nginx:1.14
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
  volumes:
  - name: html
    nfs:                          #定义nfs存储卷信息
      path: /data/volumes         #挂载nfs服务器的共享目录
      server: 192.168.88.60       #nfs服务器的ip地址

4. Verify nfs storage volume

kubectl apply -f pod-nfs.yaml

curl 10.244.2.61

 

Guess you like

Origin blog.csdn.net/q1y2y3/article/details/132231832