“This is the 8th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
【 K8S series 】 K8S learning 21, k8S in the volume
In previous articles, we shared pod, RC, RS, DaemonSet, CJ, Service and other resources
In docker, this technology is called data volume. If you are interested in this technology, you can check the article
For a POD, it has its own CPU, RAM, network interface and other resources that can be shared by the pod container, but each container has its own independent file system, so this problem will occur when we need to use the file system in daily use
For example
Pod1 is currently running, generating some data, and for some reason the pod has been restarted (deleting the POD1 and restarting a Pod2), which is not possible if the new pod2 expects to continue using the data generated by the previous POD1
Therefore, rolls are useful
What is a roll?
** It is not a class of resource in K8S, it is defined as part of pod, ** it is not a separate object in K8S, it is a part of POD, we can use the keyword volume when writing the POD manifest
We know that a POD can define multiple containers, pod volume can be shared by multiple containers, pod volume, not every container is used, these can be configured according to their own needs
Look at the picture and speak:
A POD contains 3 containers and 2 volumes, which can be configured according to your needs
Multiple types of volumes
There are many types of volumes. Different volumes are used for different needs, including the following types
-
emptyDIr
-
gitRepo
-
hostPath
-
nfs
-
Cloud service provider specific storage types and so on
-
A special type of volume
Special volumes refer to configMap, Secret, and downloadApi volumes
This section describes the application scenarios and application modes of the first four types
EmptyDir, gitRepo
emptyDir
EmptyDir is an empty directory from which the volume starts. It is possible to write data to multiple containers in pod. It is also possible to write temporary large amounts of data
But there is one thing we should note:
A volume of type emptyDir will be generated when pod is started and will disappear when POD is deleted
A small case
We can simulate a small case in pod where multiple containers share a mount
Create a mirror to write data to the volume, and create a mirror to read data from the volume
echo_uuid.sh
#~/bin/bash
mkdir /var/testuuid
while :
do
STR=`cat /proc/sys/kernel/random/uuid`
echo ${STR} > /var/testuuid/info.txt
echo uuid - ${STR}
sleep 5
done
Copy the code
The main function is to input a uUID to the file /var/testuuuid/info.txt every 5 seconds
Dockerfile
FROM ubuntu:latest
ADD echo_uuid.sh /bin/echo_uuid.sh
ENTRYPOINT /bin/echo_uuid.sh
Copy the code
Create an image echo_uuid
The DockerHub account here can be changed to the reader’s own account. Remember to log in to the Docker account in the environment
docker build -t xiaomotong888/echo_uuid .
docker push xiaomotong888/echo_uuid
Copy the code
query_uuid.sh
#~/bin/bash
while :
do
echo queryuuid:
cat /var/testuuid/info.txt
sleep 5
done
Copy the code
The main function is to read the contents of /var/testuuuid/info.txt every 5 seconds
Dockerfile
FROM ubuntu:latest
ADD query_uuid.sh /bin/query_uuid.sh
ENTRYPOINT /bin/query_uuid.sh
Copy the code
Create an image query_uuid
docker build -t xiaomotong888/query_uuid .
docker push xiaomotong888/query_uuid
Copy the code
Write the yamL file: emptydir_test.yaml
apiVersion: v1
kind: Pod
metadata:
name: testuuiddemo
spec:
containers:
- image: xiaomotong888/echo_uuid
name: echo-uuid
volumeMounts:
- name: uuid
mountPath: /var/testuuid
- image: xiaomotong888/query_uuid
name: query-uuid
volumeMounts:
- name: uuid
mountPath: /var/testuuid
readOnly: true
volumes:
- name: uuid
emptyDir: {}
Copy the code
Xiaomotong888 /echo_uuid; xiaomotong888/query_uuid; The mount path is /var/testuuid.
Another thing to note here is that the Query-UUID container is read-only for the above mount path
Kubectl create -f emptydir_test. yaml kubectl create -f emptydir_test. yaml
View logs for the echo- uUID container if there are multiple containers in pod
kubectl logs -f podName containerName
Copy the code
View the query- UUID container logs
The above example makes it clear that multiple containers within a POD can share mounted content
gitRepo
The gitRepo volume is similar to emptyDir, except that the gitRepo volume clones the Git repository and populates the data by checking out the specific version when pod is enabled
To put it simply:
In the example above, we output data on the specified volume ourselves, and gitRepo is to go to the git repository and pull the latest version of the latest master branch at pod startup, and put it in our mounted directory, as shown below:
- The user will first create a Pod with a gitRepo volume
- K8s creates an empty directory and immediately pulls the latest master release from the Git repository, showing that gitRepo is no different from emptyDir
- After pulling the Git version, the container is started and the volume is already mounted on the mount path
GitRepo volume is pulled from git repository when pod is started. If pod is running and changes the contents of git repository, pod container will not synchronize data
You must be a new pod to pull the latest git version
To create a POD and configure the gitRepo repository, we can briefly say:
apiVersion: v1
kind: Pod
metadata:
name: testuuiddemo
spec:
containers:
- image: xiaomotong888/echo_uuid
name: echo-uuid
volumeMounts:
- name: uuid
mountPath: /var/testuuid
volumes:
- name: uuid
gitRepo:
repository: https://xxxxxxx.git
revision: master
directory: .
Copy the code
The first half of the list definition is the same. The main difference is the following volumes
Both of these are relatively simple, basic volumes, and we will share the volumes involved in persistent storage in the next article
Today is here, learning, if there is a deviation, please correct
Welcome to like, follow and favorites
Friends, your support and encouragement, I insist on sharing, improve the quality of the power
All right, that’s it for this time
Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.
I am Nezha, welcome to like, see you next time ~