使用本地硬盘存储卷

最近更新时间:2020-11-06 15:04:59

使用本地硬盘有两种形式:

指定源路径(HostPath),将容器所在宿主机的文件目录挂载到容器指定的挂载点中,如容器需要访问/etc/hosts则可以使用HostPath映射/etc/hosts等场景。 临时路径挂载(EmptyDir):用于临时存储,生命周期与容器实例相同。容器实例消亡时,EmptyDir会被删除,数据会永久丢失。

1、指定源路径(HostPath)挂载

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:latest
        imagePullPolicy: Always
        name: nginx
        volumeMounts:
        - mountPath: /data
          name: hostpath
      volumes:
      - hostPath:
          path: /var
        name: hostpath

2、临时路径(EmptyDir)挂载

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:latest
        imagePullPolicy: Always
        name: nginx
        volumeMounts:
        - mountPath: /data
          name: emptydir
      volumes:
      - emptyDir: {}
        name: emptydir

本地硬盘数据卷源路径为空时,系统分配临时目录在/data/kubelet/pods/pod_id/volumes/kubernetes.io~empty-dir.

使用临时的数据卷的生命周期与实例的生命周期保持一致。

金山云,开启您的云计算之旅

免费注册