การ Deploy แอปพลิเคชันแรก (Stateless)

Introduction to Kubernetes

Frank Heilmann

Platform Architect and Freelance Instructor

เพิ่มเติมเกี่ยวกับ "kubectl"

  • kubectl: คำสั่งหลักสำหรับโต้ตอบกับ Kubernetes objects
  • ตัวอย่าง object เช่น pod, service ฯลฯ

$$

  • รูปแบบการใช้งานทั่วไป:

    • kubectl create -f <Manifest.yml>: สร้าง object ใหม่ โดย -f ย่อมาจาก "filename"
    • kubectl apply -f <Manifest.yml>: สร้าง object ใหม่ & เปลี่ยนสถานะของ object
    • kubectl get <object>: ดูภาพรวม object ที่ deploy บน Kubernetes
    • kubectl describe <object>: ดูข้อมูลเชิงลึกของ object

    $$

  • ดูความช่วยเหลือเพิ่มเติมได้ผ่าน command line option --help

Introduction to Kubernetes

เพิ่มเติมเกี่ยวกับ Manifest

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 5
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.25.4
        ports:
        - containerPort: 80
  • จำไว้ว่า Manifest เป็นแบบ declarative
  • ใช้ YAML เป็นหลัก แต่รองรับ JSON ด้วย
  • มี 2 ส่วนสำคัญ:
    • metadata: ข้อมูลพื้นฐานของ object หรือ resource
    • spec: กำหนด specification หรือสถานะที่ต้องการของ object หรือ resource
  • แต่ละส่วนอาจซ้อนกันลึกได้ ขึ้นอยู่กับ resource ที่ deploy
Introduction to Kubernetes

แอปพลิเคชัน Stateless

  • แอปพลิเคชัน Stateless:

    • แนวคิดทั่วไป
    • ไม่ได้เฉพาะเจาะจงกับ Kubernetes
    • ไม่บันทึกสถานะภายใน หรือ context ของข้อมูลที่ประมวลผล
  • เมื่อหยุดทำงาน replica ใหม่ของแอป stateless จะถูกสร้างขึ้นและเริ่มทำงานต่อทันที

  • ตัวอย่าง:
    • Frontend ของฐานข้อมูลที่ query ไปยัง backend
    • แอปค้นหาที่ query full text index
    • แอป data stream ที่แปลงค่าอุณหภูมิจาก IoT sensor จาก °F เป็น °C
Introduction to Kubernetes

Kubernetes Deployments

  • "Stateless applications" สอดคล้องกับ "Kubernetes Deployments"
  • ตัวอย่าง Manifest ประกอบด้วย:
    • apiVersion และ kind
    • metadata และ spec
  • spec กำหนดจำนวน replicas, selector และ template
  • รายละเอียดเพิ่มเติมเกี่ยวกับ selector ในภายหลัง
  • template อธิบายรายละเอียดการสร้าง pod ใน Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: <deployment name>
  labels:
    app: <a label for the application>
spec:
  replicas: <number of initial replicas>
  selector:
    matchLabels:
      app: <matches the label above>
  template:
    metadata:
      labels:
        app: <label to be given to each pod>
    spec:
      containers:
      - name: <container name>
        image: <the image to be used>
        ports:
        - containerPort: <ports for networking>
Introduction to Kubernetes

การ Deploy ลงบน Kubernetes Cluster

  • ใช้ kubectl apply -f <manifest.yml> เพื่อสร้าง pod และ นำการเปลี่ยนแปลงไปใช้
  • Kubernetes Control Plane จะ schedule Deployment ลงบน Node
    • จากนั้น Pod จะถูกสร้างขึ้นบน Node
  • แต่ละ Pod ได้รับ identifier ที่ไม่ซ้ำกันแต่สุ่มไม่แน่นอน และถือว่า "เทียบเท่ากันทุก Pod"
Introduction to Kubernetes

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

Introduction to Kubernetes

Preparing Video For Download...