Kubernetes core concept Volume storage data Volume details
In docker, there is the concept of data volume. When the container is deleted, the data will be deleted. To persist the data, you need to mount the directory on the host to docker. In k8s, the data volume is persistent through Pod. If Pod is deleted, the data volume will also be deleted. The data volume in k8s is an extension of the docker data volume. K8s is suitable for various storage systems, including local storage emptydir and hostpath, Network storage NFS,GlusterFS,PV/PVC, etc. the following describes in detail how to implement k8s storage.
I Local storage
1,EmptyDir
① Edit EmptyDir profile
vim emptydir.yaml
<span style="color:#333333"><span style="color:#ffffff !important"><code class="language-bash">apiVersion: v1 kind: Pod <span style="color:slategray">#Type is pod</span> metadata: labels: name: redis role: master <span style="color:slategray">#Define primary redis</span> name: redis-master spec: containers: - name: master image: redis:latest env: <span style="color:slategray">#Define environment variables</span> - name: MASTER value: <span style="color:#669900">"true"</span> ports: <span style="color:slategray">#In container port</span> - containerPort: 6379 volumeMounts: <span style="color:slategray">#Mounting point in container</span> - mountPath: /data name: redis-data <span style="color:slategray">#Must have a name</span> volumes: - name: redis-data <span style="color:slategray">#Corresponding to the above name</span> emptyDir: <span style="color:#999999">{</span><span style="color:#999999">}</span> <span style="color:slategray">#Host machine mount point </span></code></span></span>
② Create Pod
kubectl create -f emptydir.yaml
At this point, Emptydir has been created successfully. The access path on the host machine is /var/lib/kubelet/pods/<Pod uid>/volumes/kubernetes Io~empty dir/redis-data. If you create and delete files in this directory, it will affect the /data directory in the container. If you delete Pod, all the files will be deleted, even the files created on the host machine. If you delete the container on the host machine, k8s will automatically create another container. At this time, the files still exist.
2.HostDir
Specify a directory on the host machine and mount it to the pod container. In fact, the writing method is different from the above. Here, only different parts are intercepted. When the pod is deleted, the local file is still retained
<span style="color:#333333"><span style="color:#ffffff !important"><code class="language-bash"><span style="color:#999999">..</span>. volumes: - name: redis-data <span style="color:slategray">#Corresponding to the above name</span> hostPath: path: /data <span style="color:slategray">#Host machine mount point </span></code></span></span>
II Network data volume (NFS)
1.NFS
① Edit the profile of a Pod using NFS
vim nfs.yaml
<span style="color:#333333"><span style="color:#ffffff !important"><code class="language-bash">apiVersion: v1 kind: Pod metadata: name: nfs-web spec: containers: - name: web image: nginx imagePullPolicy: Never <span style="color:slategray">#If there is already an image, you do not need to pull the image</span> ports: - name: web containerPort: 80 hostPort: 80 <span style="color:slategray">#Map port 80 of the container to port 80 of the host</span> volumeMounts: - name <span style="color:#0077aa">:</span> nfs <span style="color:slategray">#The specified name must be consistent with the following</span> mountPath: <span style="color:#669900">"/usr/share/nginx/html"</span> <span style="color:slategray">#Mounting point in container</span> volumes: - name: nfs <span style="color:slategray">#The specified name must be consistent with the above</span> nfs: <span style="color:slategray">#nfs storage</span> server: 192.168.66.50 <span style="color:slategray">#nfs server ip or domain name</span> path: <span style="color:#669900">"/test"</span> <span style="color:slategray">#Directory shared by nfs server </span></code></span></span>
② Create Pod
kubectl create -f nfs.yaml
On the node side, you can use the mount command to query the mounting status
Because I map the code directory, create index HTML file, the file will also take effect in the container. When the Pod is deleted, the file will not be affected, and data persistence is realized.
III Persistent Volume(PV) and Persistent Volume Claim(PVC)
In fact, these two data volumes also belong to network data volumes. The reason why they are taken out alone is that I think they are much cooler than the previous data volumes. They have the meaning of big data, cloud platform. When users want to use data storage, do they need to know what type of data storage it is? The answer is No. users only want safe and reliable data storage, and it is very simple to implement. Administrators establish a storage platform, Users can consume according to their own needs. Here is the PV/PVC architecture.
1.Persistent Volume(PV)
① Edit PV profile
vim persistent-volume.yaml
<span style="color:#333333"><span style="color:#ffffff !important"><code class="language-bash">apiVersion: v1 kind: PersistentVolume metadata: name: nfs-pv labels: type: nfs <span style="color:slategray">#Specified type is NFS</span> spec: capacity: <span style="color:slategray">#The specified access space is 15g</span> storage: 15Gi accessModes: <span style="color:slategray">#The specified access mode can be mounted on multiple nodes, and the access permission is read-write execution</span> - ReadWriteMany persistentVolumeReclaimPolicy: Recycle <span style="color:slategray">#Specify that the recycling mode is automatic recycling. When the space is released, K8S automatically cleans it up, and then it can continue to be bound for use</span> nfs: server: 192.168.66.50 path: /test</code></span></span>
② Create PV
kubectl create -f persistent-volume.yaml
Status has become available
2.Persistent Volume Claim(PVC)
① Edit PVC profile
vim test-pvc.yaml
<span style="color:#333333"><span style="color:#ffffff !important"><code class="language-bash">apiVersion: v1 kind: PersistentVolumeClaim metadata: name: test-pvc spec: accessModes: - ReadWriteMany resources: <span style="color:slategray">#Specify requested resources to store 3G</span> requests: storage: 3Gi</code></span></span>
If there are currently two PV S, one 10G and one 2G, and the requested resource is 3G, 10GPV will be used directly
② Create PVC
kubectl create -f test-pvc.yaml
Because I created a 3G recyclable PV before, this volume is automatically selected. After PVC selects PV, no matter how much space the PV has, it will directly occupy all the virtual space. The actual use will be completed by Pod
3. create Pod to use platform space
vim pv-pod.yaml
<span style="color:#333333"><span style="color:#ffffff !important"><code class="language-bash">apiVersion: v1 kind: Pod metadata: name: redis111 labels: app: redis111 spec: containers: - name: redis image: redis imagePullPolicy: Never volumeMounts: - mountPath: <span style="color:#669900">"/data"</span> name: data ports: - containerPort: 6379 volumes: - name: data persistentVolumeClaim: <span style="color:slategray">#Specify PVC to be used</span> claimName: test-pvc <span style="color:slategray">#The name must be correct </span></code></span></span>
Currently, the available space of the Pod is 3G. If it exceeds 3G, you need to create another storage to meet the demand, because it is a network data volume. If you need to expand the space, you can directly delete the Pod and create another one.