只有留存下来的人事物才是你所拥有

留存

使用者在浏览网页时,Web Server会产生Log,当网页挂掉时,势必需要去查看Log作分析,导致网站发生挂掉的源头,所以网站的Log需要保存完善。
反观,Kubernetes里的Pod,因为Pod的生命周期,当Pod突然发生一些事情挂掉而release时,原先在Pod上产生的新资料时,就会跟着一起消失,所以需要切割或挂载Storage空间给予Pod做存放资料,当Pod挂掉而release时,资料还可以被保存下来。

实际演练会使用Persistent Volume(PV) & Persistent Volume Claim(PVC)使用手动去建立这两个元件,则称之为Static,先来看以下图片,来了解PV & PVC的关系:
https://ithelp.ithome.com.tw/upload/images/20211012/20140172LlSkPUmpLQ.png
PV可以想像成一颗USB硬碟,有储存空间,而去分配实际想要的硬体空间。所以PV拥有一个独立的储存空间
PVC可以想像成USB驱动程序,给予硬碟和系统的沟通桥梁,并已对装置进行访问,以及驱动程序会占据一点点的硬碟空间(系统资源)。所以PVC可以对PV进行存取模式的设定,相对的PVC也需要储存空间

以下会进行实际演练

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mypv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Delete
  storageClassName: mypv
  hostPath:
    path: "/mnt/mypv"

storage: 设定储存空间
accessModes: 访问模式,有以下三种模式:

  1. ReadWriteOnce: 可读可写,但只能mount到一个Node
  2. ReadOnlyMany: 只读不写,可以mount到多个Node
  3. ReadWriteMany: 可读可写,可以mount到多个Node

persistentVolumeReclaimPolicy: PV删除後的回收策略,有以下三种方式:

  1. Retain: PV删除後,需要手动删除mount到Node上的资料
  2. Delete: PV删除後,会自动删除mount到Node上的资料
  3. Recycle: 类似Delete,但Kubernetes官方已弃用
    storageClassName: 给PVC做连结
    hostPath: 将资料夹mount在Node上,若没有会自动建立

PVC只需要设定访问模式以及初始的储存空间

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mypvc
spec:
  resources:
    requests:
      storage: 1Gi
  accessModes:
    - ReadWriteMany
  storageClassName: mypv

上面图片所呈现的关系,需要了解,在volume做参数设定,就不会搞混

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-deploy
spec:
  replicas: 2
  selector:
    matchLabels:
      name: itdemo
  template:
    metadata:
      labels:
        name: itdemo
    spec:
      containers:
      - name: itdemo
        image: johnson860312/awswebdb
        resources:
          limits:
            memory: "256Mi"
            cpu: "128m"
        ports:
          - containerPort: 80
        volumeMounts:
          - mountPath: "/var/www/html"
            name: deploy-mount
      volumes:
        - name: deploy-mount
          persistentVolumeClaim:
            claimName: mypvc

volumeMounts:

  • mountPath: mount到Pod的路径,若Container在指定的资料夹下存在资料的话,此方式会把现有的资料给覆盖,而变成空的资料夹,若有不影响现有的资料而mount的话,需要加subPath: html。
  • name: 需要与底下volumes的名字是一样,这样才可以给PVC建立连结
    volumes:
  • persistentVolumeClaim: volume与PVC建立连结

测试方法

使用port-forward access到pod
https://ithelp.ithome.com.tw/upload/images/20211012/20140172raBUOe0k7F.png

结果

PV与PVC建立连结
https://ithelp.ithome.com.tw/upload/images/20211012/2014017295vcuj572f.png

PVC与Pod成功建立连结。Status: Bound
https://ithelp.ithome.com.tw/upload/images/20211012/201401728n47LyVy29.png
https://ithelp.ithome.com.tw/upload/images/20211012/20140172rMm17FU9KJ.png

原先在"/var/www/html"底下有资料但这里volumeMount的方式会直接覆盖过去
https://ithelp.ithome.com.tw/upload/images/20211012/20140172S14mcldT1h.png

Node上的资料夹成功创建
https://ithelp.ithome.com.tw/upload/images/20211012/20140172fOpWk1A4v1.png

在Pod建立一个测试web,验证volume是共享
https://ithelp.ithome.com.tw/upload/images/20211012/20140172rWZoEnQ8xH.png
https://ithelp.ithome.com.tw/upload/images/20211012/20140172v2qG3voUuo.png
https://ithelp.ithome.com.tw/upload/images/20211012/20140172lJGmXUf2Xm.png

在Node上建立测试web,验证储存空间是共享
https://ithelp.ithome.com.tw/upload/images/20211012/20140172ZQLchYBRtZ.png
https://ithelp.ithome.com.tw/upload/images/20211012/2014017278lqpeWVME.png

结论

明天会介绍和实作在EKS上建立EBS


<<:  Day 27:如何写出一篇还不错的技术文章?

>>:  802.1X 是网路访问控制 (NAC) 的 IEEE 标准

【Day 10】穿过 IE 的巴巴 - Hook IE 窃取明文密码

环境 Windows 10 21H1 Visual Studio 2019 IE 11.0.1904...

【Day16】音乐生成,我是要生成什麽?

音乐的解 大约在去年的时候,接触了 GAN 之後再加上专题的需要,想说来就来试试看生成音乐吧,但很快...

33岁转职者的前端笔记-DAY 3 什麽是 iframe 及使用心得

iframe 是 写网页常见的语法之一 在进公司前不知道有这语法 但通常一个网页内容 左侧或上方选单...

在 Debian 10.10 上轻松安装呒虾米

花了一点时间,把 Debian 10.10 下载与安装,我选择 Gnome 桌面环境,原因是在 Fe...

Day.24 Binary Search Tree II

今天来实作二元树~ 首先来定义一下资料结构 type Node struct { Left *Nod...