การ Deploy, Scale และ Monitor Storage ใน Kubernetes

Introduction to Kubernetes

Frank Heilmann

Platform Architect and Freelance Instructor

Persistent Volumes และ Persistent Volume Claims

Persistent Volumes และ Persistent Volume Claims

  • ออบเจ็กต์หลักสำหรับ Storage: Persistent Volumes (PV) ดูแลแยกจาก Pod
  • PV เชื่อมกับ Pod ผ่าน Persistent Volume Claims (PVC)
  • PV ที่เชื่อมแล้วช่วยให้ข้อมูลคงอยู่แม้ Pod จะหยุด, ถูกยกเลิก หรือรีสตาร์ท
  • PV ช่วยแยก Storage และ Compute ออกจากกัน
Introduction to Kubernetes

Storage Classes

Storage Classes

  • PV: จัดสรรได้สองแบบ
    • กำหนดเองโดย Kubernetes admin
    • จัดสรรอัตโนมัติโดยผู้ใช้ทั่วไป
  • การจัดสรรอัตโนมัติทำผ่าน Storage Classes (SC) โดยไม่ต้องมีการแทรกแซงจากมนุษย์
  • Storage Classes (SC):
    • กำหนดโดย Kubernetes admin
    • มีหลายประเภท (ค่า Latency ต่างกัน เช่น SSD vs HDD, กลยุทธ์การ Backup ต่างกัน)
  • หากไม่แน่ใจ ใช้ Storage Classes เลย ;-)
Introduction to Kubernetes

สรุปภาพรวม

  • ออบเจ็กต์ที่ทำให้ Storage ทำงานมีเพียงสามตัว:

    • PersistentVolume
    • PersistentVolumeClaim
    • StorageClass
  • Pod ที่ต้องการเก็บข้อมูลถาวรจะใช้ PersistentVolumeClaim

  • PVC นี้จะสั่งให้ Kubernetes สร้าง PersistentVolume ให้กับ Pod
  • PersistentVolume จะถูกผูกกับ Pod ที่ร้องขอ
  • ใช้ StorageClass ที่ระบุชื่อ ซึ่งกำหนดรายละเอียดเช่น Latency และ กลยุทธ์การ Backup ของ PV
  • PersistentVolume นี้จะยังคงอยู่ (พร้อมข้อมูลที่บันทึกไว้) แม้ Pod จะถูกยุติ
Introduction to Kubernetes

ตัวอย่าง Manifest

Pod กับ PersistentVolume

apiVersion: v1
kind: Pod
...

spec: containers: ... volumeMounts: - name: pv-mydata mountPath: /mydata
volumes: - name: pv-mydata persistentVolumeClaim: claimName: datacamp-pvc

PersistentVolumeClaim กับ StorageClass

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: datacamp-pvc

spec: storageClassName: "standard" accessModes: - ReadWriteOnce
resources: requests: storage: 5Gi
Introduction to Kubernetes

คำสั่ง "kubectl" สำหรับ Storage

  • kubectl มีชุดคำสั่งครบสำหรับสร้างและ Monitor Kubernetes Storage
  • ตัวอย่าง:
    • kubectl get sc แสดง Storage Classes ทั้งหมดที่มี
    • kubectl get pvc แสดง Persistent Volume Claims ที่ Deploy แล้ว
    • kubectl get pv แสดง Persistent Volumes ที่ Deploy แล้ว
    • เช่นเดิม kubectl apply -f <manifest> ใช้ Deploy ทรัพยากร Storage ที่ประกาศไว้ใน Manifest
Introduction to Kubernetes

มาฝึกกันเถอะ!

Introduction to Kubernetes

Preparing Video For Download...