Kubernetes那点事儿——存储之存储卷
前言
一、K8s数据卷
常用的数据卷:
- 本地(hostPath,emptyDir)
 - 网络(NFS,Ceph,GlusterFS)
 - 公有云(AWS EBS)
 - K8s资源(Configmap,Secret)
 
其中像网络存储(NFS,Ceph,GlusterFS)在K8s中主要使用PV/PVC来实现,不仅提高了安全性和还解决了组织分工的问题,我们在后续章节详细研究PV/PVC。
 K8s资源存储:
 ConfigMap:一般用于应用程序配置文件存储,,支持变量和文件。
 Secret:与ConfigMap类似,区别在于Secret主要存储敏感数据,所有的数据要经过base64编码,一般用于凭证存储。
一、临时存储卷emptyDir
emptyDir卷:是一个临时存储卷,与Pod生命周期绑定一起,如果Pod删除了卷也会被删除。
 主要应用场景:Pod中容器之间数据共享。
# cat emptyDir.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod-empty
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: {}
 
二、节点存储卷hostPath
hostPath卷:挂载Node文件系统(Pod所在节点)上文件或者目录到Pod中的容器。
 主要应用场景:Pod中容器需要访问宿主机文件。
# hostPath示例.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod-hostpath
spec:
  containers:
  - name: busybox
    image: busybox:latest
    args:
    - /bin/sh
    - -c
    - sleep 36000
    volumeMounts:
    - name: data
      mountPath: /data
  volumes:
  - name: data
    hostPath:
      path: /tmp/hostPath # 主机必须存在此目录
      type: Directory
 
三、网络存储NFS
NFS卷:提供对NFS挂载支持,可以自动将NFS共享路径挂载到Pod中。
# cat nfs_volum.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx-nfs
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx-nfs
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - name: wwwroot
          mountPath: /usr/share/nginx/html
        ports:
        - containerPort: 80
      volumes:
      - name: wwwroot
        nfs:
          server: 10.7.7.222
          path: /ifs/nfsdir










