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

推荐订阅源

Vercel News
Vercel News
SecWiki News
SecWiki News
WordPress大学
WordPress大学
小众软件
小众软件
博客园 - 司徒正美
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
Y
Y Combinator Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
K
Kaspersky official blog
T
The Exploit Database - CXSecurity.com
腾讯CDC
Scott Helme
Scott Helme
I
InfoQ
Cyberwarzone
Cyberwarzone
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Security Latest
Security Latest
The Register - Security
The Register - Security
Project Zero
Project Zero
F
Fortinet All Blogs
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
C
Cisco Blogs
L
LINUX DO - 热门话题
P
Privacy International News Feed
IT之家
IT之家
U
Unit 42
P
Privacy & Cybersecurity Law Blog
H
Help Net Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Palo Alto Networks Blog
F
Full Disclosure
宝玉的分享
宝玉的分享
Simon Willison's Weblog
Simon Willison's Weblog
L
Lohrmann on Cybersecurity
Google DeepMind News
Google DeepMind News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
H
Hacker News: Front Page
Know Your Adversary
Know Your Adversary
PCI Perspectives
PCI Perspectives
Hugging Face - Blog
Hugging Face - Blog
AWS News Blog
AWS News Blog
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Recent Announcements
Recent Announcements
Forbes - Security
Forbes - Security
Cisco Talos Blog
Cisco Talos Blog

陈少文的网站

巨变与机遇的未来十年 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) 使用 Velero 备份 Kubernetes 集群 Kubernetes Cheat Sheet 开发 Tips(18) 如何构建一个 Java 工程 开发 Tips(17) KubeSpray 安装 Kubernetes 报错 ip in ansible_all_ipv4_addresses 基于 Kubernetes 和 Jenkins 搭建自动化测试系统 在 Kubernetes 上动态创建 Jenkins Slave 使用 Jenkins 进行服务拨测 开发 Tips(16) Kubernetes 签发 Ingress 证书及日常故障运维 Kubernetes 中 Deployment 的基本操作 Kubernetes 中的证书 如何使用 KubeBuilder 开发一个 Operator Kubernetes 1.6.0 安装问题汇总 镜像管理工具 -- Harbor 开发 Tips(15) Docker 如何拉取镜像 开发 Tips(14) 使用 Helm 安装 harbor 开发 Tips(13) 使用 S2I 构建云原生应用 开发 Tips(12) 开发 Tips(11) 代码质量分析工具 SonarQube 使用 Kubeadm 安装 Kubernetes 集群 一起来学 Go --(4)常用函数 Kubernetes 中的 Ceph Kubernetes 之 Volumes Kubernetes 之 Labels、Selectors 开发 Tips(10) 开源正在重构商业模式 Kubernetes 之网络 Kubernetes 之 API 使用 Helm 和 Operator 快速部署 Prometheus Kubernetes 复杂有状态应用管理框架 -- Operator Kubernetes 的包管理器 -- Helm 一起来学 Go --(3)Go Modules 如何一步一步地优化博客方案 kubectl 实用指南 Kubernetes 中的基本概念 搭建远程 Kubernetes 开发环境 大公司和小公司的 ToB 思路 开发 Tips(9) Go 入门指南 一起来学 Go --(2)数据与逻辑结构 如何预防 Web 富文本中的 XSS 攻击 django-xss-cleaner 云工作时代 一起来学 Go --(1)背景与特点 SaaS 开发团队的不同阶段 你不知道的 Git 使用技巧 输出既服务 微服务设计 继续奔跑 开发 Tips(8) 从账户安全到二次验证 Django 性能之数据库查询优化 Django 性能之分库分表 敏捷开发之研发流程 打造一致性的团队 开发 Tips(7) Pytest 进阶学习之 Mock PaaS 部署之 buildpack Go 开发配置 领域输出才是 PaaS 的核心竞争力 Pytest 入门学习 开发 Tips(6) 如何使用 Jenkins、Docker、GitLab 搭建 Django 自动化部署流程 开发 Tips(5)
在 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