Kubernetes volume data volumes

Volume is the shared directory accessible to a plurality of containers in kubernetes Pod

Kubernetes offers numerous volume types, including: emptyDir, hostPath, NFS, GlusterFS, configMap, Cephfs

. 1, emptyDir
emptyDir type vloume in Pod assigned to be created, Kubernetes automatically assigns a directory on the node when the node, it is not necessary to specify the corresponding on the host node directory file, the directory initialization is empty, when the Pod from the node on being removed when the data in emptyDir will be permanently deleted

1. Edit the emptyDir.yamlfile

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: web-demo-empty
spec:
  replicas: 2
  selector:
    matchLabels:
      app: app-demo-empty
  template:
    metadata:
      labels:
        app: app-demo-empty
    spec:
      containers:
      - name: tomcat-demo
        image: tomcat
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
        volumeMounts:
          # 将/mydata-data目录挂载到共享仓库
          - mountPath: /mydata-data
            name: datavol

      - name: nginx-demo
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
          # 将/mydata-data目录挂载到共享仓库
          - mountPath: /mydata-data
            name: datavol

      # emptyDir是一个共享仓库(中央仓库)
      volumes:
        - name: datavol
          emptyDir: {}

2. Performkubectl create

$ kubectl create -f  emptyDir.yaml
deployment.extensions/web-demo-empty created

3. The first into tomcat-demothe container to create a file

$  kubectl exec -it web-demo-empty-9b5644c45-7bpn5 -c tomcat-demo bash
$ touch /mydata-data/data.txt
$ ls /mydata-data/
data.txt

4. Go to the nginx-demovessel to see if there is data.txta file

$ kubectl exec -it web-demo-empty-9b5644c45-7bpn5 -c nginx-demo bash
$ ls /mydata-data/
data.txt

After tests proved empty emptyDir is a shared directory that allows multiple containers Pod is shared between the directory

2, hostPath
hostPath type Pod vloume is mounted in the directory or a file on the host, the host so that the container can be used for storage of the file system, but in the kubernetes, Pod are based scheduleto a different node in the dynamic scheduling node assembly on, when a Pod is up and on the current node by node hostPathto store the file locally, the next time a scheduled start on another node, you can not use the files previously stored on the node.

1. Edit the hostPath.yamlfile

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: web-demo-hostpath
spec:
  replicas: 2
  selector:
    matchLabels:
      app: app-demo-hostpath
  template:
    metadata:
      labels:
        app: app-demo-hostpath
    spec:
      containers:

      containers:
      - name: nginx-demo
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
          - mountPath: /mydata-data
            name: datavol

      volumes:
        - name: datavol
          hostPath:
            path: "/data"

2. Performkubectl create

$ kubectl create -f hostPath.yaml
deployment.extensions/web-demo-hostpath created

3. The need to create at each node node /datadirectory, and create a test file

$ mkdir /data 
$ touch /data/test.txt

4. Check whether the container into the /mydata-datamount to the host

$ kubectl exec -it web-demo-hostpath-7866c644c4-7f8fk bash
$ ls /mydata-data/
test.txt

Guess you like

Origin blog.51cto.com/12643266/2463636