kubernetes中的常用存储类型

这里介绍hostPath

利用hostPath的挂载卷

假如容器希望使用主机目录下的/opt/etcd/ssl/下的认证文件,可以像如下这样定义:

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: k8s.gcr.io/test-webserver
    name: test-container
    env:
    - name: ETCD_KEY_FILE
      value: "/opt/etcd/ssl/server-key.pem"
    volumeMounts:
    - mountPath: /opt/etcd/ssl/  #容器中文件所在位置
      name: etcd-ssl             #卷名称,要和volumes中的一致
  volumes:
  - name: etcd-ssl
    hostPath:
      # directory location on host
      path: /opt/etcd/ssl        #主机文件所在的位置
      # this field is optional
      type: Directory
      readOnly: true

利用configmap的挂载卷

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web-nginx
  template:
    metadata:
      labels:
        app: web-nginx
    spec:
      containers:
        - name: web-nginx
          image: nginx:1.14.2
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 80
          volumeMounts:
          - name: web-nginx-config             #挂载卷名称,要和volumes中的一致
            mountPath: /etc/nginx/nginx.conf   #在容器内部的位置
            subPath: nginx.conf                #可用于指定所引用的卷内的子路径,而不是其根路径。以 subPath 卷挂载方式使用 ConfigMap 时,将无法接收 ConfigMap 的更新
      volumes:
        - name: web-nginx-config
          configMap:
            name: web-nginx-config
            items:
            - key: nginx.conf                  #挂载的是configmap key "nginx.conf"对应的值,该值是文件nginx.conf(path指定)的内容
              path: nginx.conf

emptyDir

emptyDir类型的volume在pod分配到node上时被创建,kubernetes会在node上自动分配 一个目录,因此无需指定宿主机node上对应的目录文件。这个目录的初始内容为空,当Pod从node上移除时,emptyDir中的数据会被永久删除。
emptyDir Volume主要用于某些应用程序无需永久保存的临时目录,多个容器的共享目录等。下面是一个pod挂载emptyDir的示例:

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - image: test-webserver
    name: test-container
    volumeMounts:
    - name: cache-volume
      mountPath: /cache
  volumes:
  - name: cache-volume
    emptyDir: {}

以上几种存储类型多用于测试,在生产环境中很少使用,更多的使用分布式存储、对上层人员屏蔽的存储方式。例如PV,PVC.
PV如果使用nfs-server的方式,需要先安装nfs-server(安装方法:https://support.huaweicloud.com/deepexidip-dma/deepexidip_57.html)

posted @ 2020-10-21 10:07  JaneySJ  阅读(803)  评论(0编辑  收藏  举报