惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
U
Unit 42
N
Netflix TechBlog - Medium
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
小众软件
小众软件
V
Visual Studio Blog
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
博客园 - 叶小钗
GbyAI
GbyAI
爱范儿
爱范儿
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
D
DataBreaches.Net
博客园_首页
D
Docker
A
About on SuperTechFans
G
Google Developers Blog
I
InfoQ
T
The Blog of Author Tim Ferriss
V
V2EX
博客园 - Franky

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19)
在 Kubernetes 中使用 emptyDir、hostPath、localVolume
微信公众号 · 2019-08-24 · via 陈少文的网站

之前通过 Kubernetes 之 Volumes ,对 Volumes 有了一定的了解。本篇主要侧重实践,学习如何使用 emptydir、hostpath、localvolume 三种本地存储方案。

1. PV 的基本属性

1.1 PV 的生命周期

PV 的状态:

  • Available:可用,还未被任何 PVC 绑定
  • Bound:已经被 PVC 绑定
  • Released:PVC 被删除,但是资源还未被重新声明
  • Failed:自动回收失败

1.2 PV 的回收策略

PersistentVolumeReclaimPolicy,即 PV 的回收策略。

当 Pod 不需要 PV 时,如何处理:

  • Retain:保留数据,需要管理员手工清理数据
  • Recycle:资源回收,清除 PV 中的数据
  • Delete:直接删除 PV

目前只有 NFS 和 HostPath 类型卷支持回收策略,AWS EBS、GCE PD、Azure Disk 和 Cinder 支持 Delete 策略。

1.3 PV 的访问模式

PV 的访问模式(accessModes):

  • ReadWriteOnce:PV 以 read-write 挂载到一个 Pod
  • ReadWriteMany:PV 以 read-write 方式挂载到多个 Pod
  • ReadOnlyMany:PV 以 read-only 方式挂载到多个 Pod

2. emptyDir

使用 emptyDir 时,Kubernetes 在 Node 上自动分配一个目录给 Pod。此目录中,初始内容为空,当 Pod 从 Node 上移除时,emptyDir 中的数据也被移除。

主要用于无需永久保存的临时目录,多个容器的共享目录等场景。

创建文件 emptydir.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
apiVersion: v1
kind: Pod
metadata:
  name: emptydir
spec:
  containers:
    - image: busybox
      name: app
      volumeMounts:
        - mountPath: /logs
          name: shared-dir
      args:
        - /bin/sh
        - -c
        - echo emptydir >> /logs/app.log; sleep 60000
    - image: busybox
      name: log-collector
      volumeMounts:
        - mountPath: /app_logs
          name: shared-dir
      args:
        - /bin/sh
        - -c
        - cat /app_logs/app.log; sleep 60000
  volumes:
    - name: shared-dir
      emptyDir: {}

执行命令:

1
2
3
kubectl apply -f emptydir.yaml
kubectl exec emptydir -c log-collector cat /app_logs/app.log
emptydir

可以看到两个容器之间,实现了文件共享的功能。

3. hostPath

hostPath 类型是映射 Node 文件系统中的文件或者目录到 Pod 。支持的类型有文件、目录、File、Socket、CharDevice 和 BlockDevice。

创建文件 hostpath.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: v1
kind: Pod
metadata:
  name: pod-hostpath
spec:
  nodeSelector:
    kubernetes.io/hostname: i-6fns0nua
  containers:
    - image: busybox
      name: app
      volumeMounts:
        - mountPath: /logs
          name: shared-dir
      args:
        - /bin/sh
        - -c
        - echo hostpath >> /logs/app.log; sleep 60000
  volumes:
    - name: shared-dir
      hostPath:
        path: /data/logs
  1. 登陆主机 i-6fns0nua ,创建目录
  1. 创建 hostpath
1
kubectl apply -f pod.yaml
  1. 登陆主机 i-6fns0nua ,查看共享文件
1
2
cat /data/logs/app.log
hostpath

4. local volume

local volume 适合的场景:

  • 数据缓存,应用可以就近访问数据,快速处理。
  • 分布式存储系统,如分布式数据库,分布式文件系统。

4.1 创建静态存储

创建文件 sc.yaml

1
2
3
4
5
6
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

pv.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local1
spec:
  capacity:
    storage: 30Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local
  local:
    path: /data/local1
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - i-6fns0nua

4.2 Pod 使用

创建文件 pvc.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc1
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 30Gi

Kubernetes 会自动对 PV 和 PVC 进行匹配。下面是在 Pod 中通过 PVC 使用存储。

pod.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
kind: Pod
apiVersion: v1
metadata:
  name: pod1
spec:
  containers:
    - name: nginx1
      image: nginx
      volumeMounts:
        - mountPath: "/var/www/html"
          name: pod1
  volumes:
    - name: pod1
      persistentVolumeClaim:
        claimName: pvc1