This is the 8th day of my participation in the More text Challenge. For details, see more text Challenge

Data in Docker

Save the Docker startup container, as long as the container is not deleted, its internal files will not disappear

[root@k8s-node1 ~]# docker run -it -d victor2019/test:v1
e64c2bc31ac04174c1c78f9223e01a2cdec371104c58b74bdfc0a439913bf2d3
Copy the code

Then write something into the container, because my test image is nginx, so write to the main page to test

[root@k8s-node1 ~]# docker exec e64c bash -c “echo hello > /usr/share/nginx/html/index.html”

Check the IP address in the docker0 network segment of the container

[root @ k8s rac-node1 ~] # docker inspect e64c | grep 172 "Gateway" : "172.17.0.1", "IPAddress" : "172.17.0.2", "Gateway" : "172.17.0.1", "IPAddress" : "172.17.0.2",Copy the code

Verify what you’ve written

[root@k8s-node1 ~]# curl 172.17.0.2
hello
Copy the code

Then stop the container and start it again

[root@k8s-node1 ~] [root@k8s-node1 ~] [root@k8s-node1 ~]# curl 172.17.0.2  helloCopy the code

The files in the container are always there.

So, if you start a docker container directly and specify –restart=always, you can guarantee that the container will continue to provide services.

Data in K8s is saved

However, k8S has a different mechanism. Each POD has its own full life cycle. Even if it is not manually deleted, if the POD crashes and restarts because of one of the containers, not only will the IP of the POD change, but all the contents of the containers will disappear

Then, is there a way to make THE POD of K8S the same as the container in Docker? Unless it is manually deleted, the things saved in it will always be there even if we restart it. The volume we study in this section is designed to solve this problem.

It should be noted that docker also has the concept of volume. However, volume in Docker is the persistence of the contents of the container on the host disk, even if the container is deleted. It is similar to PVC, which we will learn in the next section, but it is two things with k8s volume

The volume in K8s is a public folder that can be used by all containers in pod. Each container can mount the public folder to a directory within the pod (different containers can have different directories), so that pods can exchange data with each other. The life cycle of the volume is the same as that of the POD, so the contents of the volume and the mount directory remain unchanged no matter how many times the POD is restarted, but the volume disappears if the POD is terminated.

Note that the restart is caused by a container crash, not by manually removing the POD and then restarting because of the restart policy

K8s supports many types of volume, and dozens of media are available in the official website. We’ve already seen configMap and Secret in the last section, but we’ll take emptyDir and hostPath, which are the two most common.

The actual operation

All of the yamL files below are hosted on my Github repository

When a POD is allocated to a node, an emptyDir volume is created and its lifetime is the same as that of a POD. As the name suggests, emptyDir is initially empty and all containers in the POD can read and write from it. If pod is deleted on node, the data in emptyDir will be permanently deleted.

A container crash doesn’t remove the POD from the node, it just restarts, so emptyDir is safe for container crashes

EmptyDir is usually used to declare some temporary space to store some temporary files, especially when multiple containers work together. Or some checkpoint when the container is recovering from a crash.

Two configuration fields in pod. Spec. Volumes. EmptyDir statement

Medium String The default value is an empty string. It can also be Memory for Memory. SizeLimit String defaults to nil for no specified size. Create a Deployment using the yamL file test-volume-emptydir

apiVersion: extensions/v1beta1 kind: Deployment metadata: name: test-volume-emptydir spec: replicas: 1 template: metadata: labels: app: test-volume-emptydir spec: containers: - name: mynginx image: mynginx:v2 ports: - containerPort: 80 volumeMounts: - mountPath: /usr/share/nginx/html name: test-emptydir - name: myalpine image: alpine command: ["ping","8.8.8.8"] volumeMounts: -mountpath: /emptydir name: test-emptydir volumes: -name: test-emptydir emptydir: {} volumeMounts: -mountpath: /emptydir volumes: -name: test-emptydir emptydir: {}Copy the code

EmptyDir: {} indicates that the default configuration is used, that is, the size of the disk is not declared.

After successful creation, enter the MyAlpine container and write files to the directory bound to emptyDir, which will be displayed when you access another mynginx container

[root@k8s-master volume]# kubectl apply -f test-volume-emptydir.yaml deployment.extensions/test-volume-emptydir created [root@k8s-master volume]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES Test-volume-emptydir -6bf8d675fc-ds2jx 2/2 Running 0 15m 10.244.1.117k8s -node1 <none> <none> [root@k8s-master volume]# kubectl exec test-volume-emptydir-6bf8d675fc-ds2jx -c myalpine -it -- /bin/sh -c "echo hello emptydir > /emptydir/index.html" [root@k8s-master volume]# curl 10.244.1.117 hello emptydirCopy the code

Note Because emptyDir introduces a POD container can share the same volume.