SoFunction
Updated on 2025-03-10

The use of volumes containers in kubernetes in cloud native technology

Volumes:

The files in the container exist for a short time. When a container crashes, the files will be lost, but the state after the container restarts is clean; the second problem solves the problem of sharing files between different containers in a Pod.

There are many types of volumes, please check the official documentation for details:roll

1. Volume characteristics:

When a Pod is created, the emptyDir volume is created, and this volume will remain during the Pod run.

Used for data sharing between different containers in Pod.

When a Pod is deleted from a node, the data in the emptyDir volume will also be permanently deleted and will not be saved.

The media used by emptyDir volumes to be stored on that node can be disk or SSD or network storage or memory;

Note that if the media is memory, it will be recorded in the container consumption.

1.2. Official example:

The configuration items are only last three lines, which is relatively simple.

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: /test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}

1.3. Let's make an example:

Step 1: We create a deploy type yaml file we are preparing to use. The general part above is not posted. It mainly depends on the part we want to use. Create two nginx containers and hang the emptyDir volumes under the two containers respectively.

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: /test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}

Step 2: Execute the yaml file to create deploy resources

[root@k8s-master01 ~]# kubectl create -f  
/dp-cm created
[root@k8s-master01 ~]# kubectl get  
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
dp-nginx   1/1     1            1           17m
[root@k8s-master01 ~]# kubectl get pod
NAME                       READY   STATUS    RESTARTS   AGE
dp-nginx-98db5f6df-6g24r   2/2     Running   0          2m40s

Step 3: Create a file in the /opt directory of the nginx container, and then check it in the Chengdu /tmp directory in the redis container to see if it is shared

# Create a file in nginx container[root@k8s-master01 ~]# kubectl exec -ti dp-nginx-98db5f6df-6g24r -c nginx -- bash
root@dp-nginx-98db5f6df-6g24r:/# cd /opt/
root@dp-nginx-98db5f6df-6g24r:/opt# touch 
root@dp-nginx-98db5f6df-6g24r:/opt# ls

# Go to the redis container to see if the file is shared[root@k8s-master01 ~]# kubectl exec -ti dp-nginx-98db5f6df-6g24r -c redis -- ls /tmp

Step 4: There is no step 4. From the third step, you can see the files created in the nginx container. You can see them in the redis container. All shared volumes are successful.

2. Volume characteristics:

The hostPath volume can mount files or directories on the host node file system to your Pod.

Note: This method is not recommended for official purposes, but when you need to use hostPath, its scope should be limited to the required files or directories and mounted in a read-only manner.

2.2. Official example:

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: /test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      # Directory location on the host      path: /data
      # This field is optional      type: Directory

The type value supported by the hostPath type:

Default is empty: means that no checks are performed before the hostPath volume is installed.

DirectoryOrCreate: If nothing exists on the given path, an empty directory will be created as needed, with permission set to 0755, with the same group and owner information as kubelet.

Directory: A directory that must exist on a given path.

FileOrCreate: If nothing exists on the given path, an empty file will be created there as needed, with permission set to 0644, with the same group and ownership as kubelet.

File: A file that must exist on a given path.

Socket: A UNIX socket that must exist on a given path.

CharDevice: A character device that must exist on a given path.

BlockDevice:BlockDevice

Note: FileOrCreate mode is not responsible for creating the parent directory of the file. If the parent directory of the file to be mounted does not exist, the Pod startup will fail.

2.3. Let's make an example:

Our example is the File type. You can also try other types, and the configuration method is the same

Step 1: From the following example, you can see that the time zone in the container is different from the time zone of our host machine.

[root@k8s-master01 ~]# kubectl exec -ti dp-nginx-98db5f6df-6g24r -c nginx -- cat /etc/timezone
Etc/UTC
[root@k8s-master01 ~]# cat /etc/timezone 
Asia/Shanghai

Step 2: Modify the yaml file and then update the deploy copy

    spec:
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
        - mountPath: /opt
          name: share-volume
        - mountPath: /etc/timezone
          name: timezone
      - image: redis
        name: redis
        volumeMounts:
        - mountPath: /tmp
          name: share-volume
      volumes:
      - name: share-volume
        emptyDir: {}
      - name: timezone
        hostPath:
          path: /etc/timezone  # Host path          type: File

# Update the copy, pay attention to upper and lowercase, the lowercase I just wrote the file has reported an error[root@k8s-master01 ~]# kubectl replace -f  
/dp-nginx replaced

Step 3: Check the container time zone, it is successful and has been modified to shanghai; you can see that the time zone of the host mounted host has been modified, and the time zone of the redis container that is not mounted is still the default

[root@k8s-master01 ~]# kubectl exec dp-nginx-7b456ccf45-8c8fn -c nginx -- cat /etc/timezone
Asia/Shanghai
[root@k8s-master01 ~]# kubectl exec dp-nginx-7b456ccf45-8c8fn -c redis -- cat /etc/timezone
Etc/UTC

The nfs volume can mount NFS (network file system) to your Pod. Unlike emptyDir, the Pod will be deleted while deleting it. The contents of the nfs volume will be saved when deleting the Pod, and the volume will be uninstalled.
However, it is not recommended to use it in production.

I didn't prepare for this example, so I'll talk about how to do it. If you are interested, you can do it yourself.

Step 1: Prepare an NFS server and create a shared directory /data/test
Step 2: Mount the shared directory of nfs into the pod

    spec:
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
        - mountPath: /opt
          name: nfs-volume
      volumes:
      - name: nfs-volume
        nfs:
          server: 192.168.10.10  #nfs server ip          path: /data/test

Step 3: After creating the Pod resource, create a file in the container /opt, check whether it is shared under the nfs server /data/test, and test it in turn. If resource sharing is successful.

For other types of volumes, you can check the official website, and write pv and pvc in the next article!

The above is the detailed content of the use of volumes in the cloud native container kubernetes. For more information about volumes in the cloud native container kubernetes, please follow my other related articles!