In Docker there is the concept of data volume, when the container is deleted, the data will be deleted together, want to persist the use of data, you need to mount the directory on the host to Docker, in K8s, the data volume is persisted through the pod, if the pod is deleted, the data volume will be deleted together, K8s data volume is the expansion of the Docker data volume, k8s suitable for a variety of storage systems, including local storage Emptydir,hostpath, network storage NFS,GLUSTERFS,PV/PVC, and so on, the following details on how to implement k8s storage.
One. Local Storage
1,emptydir
① editing the Emptydir configuration file
Vim Emptydir.yaml
apiversion: v1kind: pod #类型是Podmetadata: labels: name: redis role: master #定义为主redis name: redis-masterspec: containers: - name: master image: redis:latest env: #定义环境变量 - name: MASTER value: "true" ports: #容器内端口 - containerPort: 6379 volumeMounts: #容器内挂载点 -&Nbsp;mountpath: /data name: redis-data #必须有名称 volumes: - name: redis-data #跟上面的名称对应 emptydir: {} #宿主机挂载点
② Creating Pods
Kubectl create-f Emptydir.yaml
At this point Emptydir has been created successfully, the access path on the host is/var/lib/kubelet/pods/<pod Uid>/volumes/kubernetes.io~empty-dir/redis-data, If you create a delete file in this directory, you will have an effect on the/data directory in the container, and if you delete the pod, the file will all be deleted, even if the file is created on the host, and if the container is deleted on the host, k8s will automatically create a container when the file still exists.
2.HostDir
Specify a directory on the host, attached to the pod container, in fact, the above is not the same as the wording, here only to intercept the different parts, when the pod is deleted, the local still keep the file
Volumes:-Name:redis-data #跟上面的名称对应 hostpath:path:/data #宿主机挂载点
Two. Network data volumes (NFS)
1.NFS
① editing a configuration file for a pod that uses NFS
Vim Nfs.yaml
apiversion: v1kind: podmetadata: name: nfs-webspec: containers: - name: web image: nginx imagePullPolicy: Never #如果已经有镜像, You don't need to pull the mirror again ports: - name : web containerport: 80 hostport: 80 # Map the container's port 80 to the 80 port of the host volumeMounts: - name : nfs #指定名称必须与下面一致 mountPath: "/usr/share/nginx/html" #容器内Mount Point volumes: - name: nfs #指定名称必须与上面一致 nfs: #nfs存储 server: 192.168.66.50 #nfs服务器ip或是域名 path: "/test" #nfs服务器共享的目录
② Creating Pods
Kubectl create-f Nfs.yaml
Mount command can be used to query mount on node side
Because I am mapping the code directory, after creating the index.html file in the/test directory, this file will also take effect in the container, when the pod is deleted, the file is not affected, and data persistence is implemented.
Three. Persistent Volume (PV) and persistent Volume Claim (PVC)
In fact, these two data volumes also belong to the network data volume, single carry out because I think this is more cool than the previous data volume, there is a big data, cloud platform meaning, when users want to use data storage when he also need to know what type of data storage, the answer is not required, users only want to secure and reliable data storage, And the implementation is very simple, the administrator set up a storage platform, users according to their own needs to spend on it, the following to implement the PV/PVC architecture.
1.Persistent Volume (PV)
① editing the PV configuration file
Vim Persistent-volume.yaml
APIVERSION:V1KIND:PERSISTENTVOLUMEMETADATA:NAME:NFS-PV Labels:type:nfs #指定类型是NFSspec: Capacity: #指定访问空间是15G storage:15gi accessmodes: #指定访问模式是能在多节点上挂载, and access rights are read-write execution-Readwritemany Persistentvolumerec Laimpolicy:recycle #指定回收模式是自动回收, when the space is freed, K8s automatically cleans up and can then continue to bind using nfs:server:192.168.66.50 path:/test
② Creating PV
Kubectl create-f Persistent-volume.yaml
Status has become available
2.Persistent Volume Claim (PVC)
① Editing a PVC configuration file
Vim Test-pvc.yaml
Apiversion:v1kind:persistentvolumeclaimmetadata:name:test-pvcspec:accessmodes:-ReadWriteMany resources: #指定请求的资源, storage 3G Requests:storage:3gi
If there are currently two PV, a 10G, a 2G, the request resource is 3G, then the direct use of 10GPV
② Creating a PVC
Kubectl create-f Test-pvc.yaml
Because I have previously created a 3G recyclable PV, so the automatic selection of this volume, after the PVC selection of PV, regardless of how much PV space will directly occupy all virtual space, the actual use by the pod to complete
3. Create pods to use platform space
Vim Pv-pod.yaml
apiVersion:v1kind:Podmetadata:name:redis111 labels:app:redis111spec:containers:-Name:redis Image:redi S imagepullpolicy:never volumemounts:-Mountpath: "/data" Name:data ports:-containerport:6379 V Olumes:-name:data persistentvolumeclaim: #指定使用的PVC CLAIMNAME:TEST-PVC #名字一定要正确
The current pod free space is 3G, if more than 3G, you need to create storage to meet the demand, because it is a network data volume, if you need to expand space, directly delete the pod and then build one.
Kubernetes core concept of volume storage data volume detailed