k8s- Storage -volume

One background

 

The life cycle of the files on the container disk is short , This causes some problems when running important applications in containers . First , When the container crashes when ,kubelet Will restart it , But the files in the container will be lost —— The container is in a clean condition ( Mirror the original state ) Restart . secondly , stay Pod When multiple containers are running simultaneously in , These containers usually need to share files .Kubernetes Medium Volume Abstraction is a good solution to these problems .

 

Kubernetes The volume in has a definite lifetime —— And the one that encapsulates it Pod identical . the f With , The life ratio of volume Pod All containers in are long , When this When a container is restarted, the data is still saved . Of course , When Pod When it no longer exists , The volume will no longer exist . Maybe more importantly ,Kubernetes Supports multiple types of volumes ,Pod You can use any number of volumes at the same time

 

Two Common mount volumes

2.1 emptyDir

Application scenarios : Pod Data sharing between containers in

 

 

apiVersion: v1
kind: Pod
metadata:
  name: my-pod2
spec:
  containers:
  - name: write
    image: centos
    command: ["bash","-c","for i in {1..100};do echo
$i >> /data/hello;sleep 1;done"]
    volumeMounts:
      - name: data
        mountPath: /data
  - name: read
    image: centos
    command: ["bash","-c","tail -f /data/hello"]
    volumeMounts:
      - name: data
        mountPath: /data
  volumes:
  - name: data
    emptyDir: {}

2 Then create

 

kubectl create -f emptydir.yaml

 

3 see

kubectl logs my-pod2 -c write
kubectl logs my-pod2 -c read #  It's constantly outputting content 

2.2 hostpath

hostPath The purpose of :

Running requires access to Docker The inner container ; Use /var/lib/docker Of hostPath

Run in a container cAdvisor; Use /dev/cgroups Of hostPath

allow pod Specify the given hostPath Should it be in pod Before running , Should we create , And in what form it should exist

example

 

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: wangyanglinux/myapp:v1
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      path: /data
      type: Directory

Then login pod, Go to test-pd Create a file under the directory , Then go to the host's data Directory to see if the file is generated

kubectl exec -it test-pd /bin/sh # Sign in test-pod