iT邦幫忙

2021 iThome 鐵人賽

DAY 28
0
DevOps

從煉獄走到天堂的AWS DevOps 工具及應用開發大進擊系列 第 28

只有留存下來的人事物才是你所擁有

  • 分享至 

  • twitterImage
  •  

留存

使用者在瀏覽網頁時,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


上一篇
利用世界第八大奇蹟,讓你的財富橫向擴展
下一篇
D槽空間還夠嗎?擴充一下可以下載更多東西
系列文
從煉獄走到天堂的AWS DevOps 工具及應用開發大進擊30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
juck30808
iT邦研究生 1 級 ‧ 2021-10-12 18:32:15

恭喜即將完賽!!!

我要留言

立即登入留言