Pod storage volume is a volume that can be shared directory access a plurality of containers, the volume Kubernetes concept, use and purpose of the docker volume relatively similar, but not both equivalent, firstly, the volume is defined Kubernetes on Pod, then a plurality of containers in the Pod hanging on to a specific file directory; Secondly, in the same volume and Pod kubenetes life cycle, but not with the life cycle of the container, when the container is terminated or restarted, the data volume of will not be lost, and finally Volume supports multiple data types, such as: GlusterFS, Ceph, etc. absorbed into the distributed file system
volume of use is relatively simple, in most cases, we first Volume in a Pod life, and then reference the Volume and mount it to a directory on the container in the container, for example, we define two in a Pod in container, a container run Nginx, a container run busybox, then we define a shared storage volume on the Pod, the contents inside the container should be able to see two, topology is as follows:
The following standard red to note that the name of the shared volume to be consistent
[root@master ~]# cat test.yaml apiVersion: v1 kind: Service metadata: name: serivce-mynginx namespace: default spec: type: NodePort selector: app: mynginx ports: - name: nginx port: 80 targetPort: 80 nodePort: 30080 --- apiVersion: apps/v1 kind: Deployment metadata: name: deploy namespace: default spec: replicas: 1 selector: matchLabels: app: mynginx template: metadata: labels: app: mynginx spec: containers: - name: mynginx image: lizhaoqwe/nginx:v1 volumeMounts: - mountPath: /usr/share/nginx/html/ name: share ports: - name: nginx containerPort: 80 - name: busybox image: busybox command: - "/bin/sh" - "-c" - "sleep 4444" volumeMounts: - mountPath: /data/ name: share volumes: - name: share emptyDir: {}
Create a Pod
[root@master ~]# kubectl create -f test.yaml
View Pod
[root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE deploy-5cd657dd46-sx287 2/2 Running 0 2m1s
View service
[root@master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6d10h serivce-mynginx NodePort 10.99.110.43 <none> 80:30080/TCP 2m27s
We busybox container into which create an index.html
[root@master ~]# kubectl exec -it deploy-5cd657dd46-sx287 -c busybox -- /bin/sh 容器内部: /data # cd /data /data # echo "fengzi" > index.html
Open a browser to test
Nginx seen to container if there is an index.html file
[root@master ~]# kubectl exec -it deploy-5cd657dd46-sx287 -c nginx -- /bin/sh 容器内部: # cd /usr/share/nginx/html # ls -ltr total 4 -rw-r--r-- 1 root root 7 Sep 9 17:06 index.html
ok, our documentation is written in the busybox in nginx to read!