Module 09 — Cloud Run, Functions & Event-Driven Services
🧠 ภาพจำก่อนอ่าน: 📩 Event → ⚡ Runtime → ✅ Verify
🎨 Visual Guide ของวิชานี้ — ภาพช่วยจำ ไม่แทนคำสั่งและเนื้อหาจริง
1. Cloud Run เหมาะกับอะไร
หัวข้อที่มีชื่อว่า “1. Cloud Run เหมาะกับอะไร”Cloud Run ให้รัน application workloads บน managed platform เช่น service สำหรับ requests และ jobs สำหรับ task ที่ทำจนจบ ส่วน worker-oriented capabilities ให้ตรวจ product mode ปัจจุบัน
สำหรับ ACE เน้น service/revision/traffic/identity/scaling และ event-driven workloads ไม่ต้องดูแล OS nodes แบบ VM แต่ยังต้องจัด image/code, config, access, data และ dependency
| โจทย์ | ตัวเลือกเริ่มต้น |
|---|---|
| Stateless HTTP API | Cloud Run service |
| Batch task ทำจบแล้ว exit | Cloud Run job |
| Function ตาม event | Cloud Run functions + trigger ที่เหมาะ |
| ต้อง Kubernetes APIs/custom cluster behavior | GKE |
| ต้อง Windows/OS control เต็ม | Compute Engine |
2. Container contract
หัวข้อที่มีชื่อว่า “2. Container contract”HTTP service container ต้อง listen บน address/port ตาม runtime contract โดยทั่วไปใช้ environment PORT และ bind 0.0.0.0 ไม่ใช่ localhost อย่างเดียว
Local filesystem ของ instance ไม่ใช่ฐานข้อมูลถาวรที่ share กับ instance อื่น เก็บ business data ใน external storage/database ที่เหมาะและออกแบบ app ให้ restart/scale ได้
Process เริ่มช้า, image ใหญ่, initialization หนัก และ dependency ช้า มีผลต่อ startup latency/cold start; minimum instances ช่วยบางด้านแต่มี cost และไม่ได้แก้ทุก latency cause
3. Service กับ revision
หัวข้อที่มีชื่อว่า “3. Service กับ revision”Service เป็นชื่อ workload/endpoint Revision เป็น snapshot ของ release configuration ที่เปลี่ยนแล้วสร้าง version ใหม่
แก้ image หรือ configuration ที่ทำให้สร้าง revision จะมี version ใหม่ ไม่ควรคิดว่าแก้ VM เดิมด้วย ssh แล้วทุก request เห็น code ใหม่เหมือนกัน
flowchart TB
U[Authenticated requests] --> S[Cloud Run service]
S --> R1[Revision 1 with 90 percent traffic]
S --> R2[Revision 2 with 10 percent traffic]
R1 --> D[Database and object storage]
R2 --> D
I[Runtime service account] --> D
4. Rollout, canary และ rollback
หัวข้อที่มีชื่อว่า “4. Rollout, canary และ rollback”ตาม Traffic migration สามารถ deploy revision โดยยังไม่ส่ง traffic แล้วปรับ traffic ไปตาม version
ตัวอย่างรูปแบบคำสั่ง:
gcloud run deploy SERVICE_NAME \ --image=IMAGE_URL \ --region=REGION_NAME \ --no-traffic
gcloud run revisions list \ --service=SERVICE_NAME --region=REGION_NAME
gcloud run services update-traffic SERVICE_NAME \ --region=REGION_NAME \ --to-revisions=OLD_REVISION=90,NEW_REVISION=10ตรวจ error rate/latency ก่อนขยาย traffic ถ้ารุ่นใหม่เสีย rollback traffic ไป revision เดิม แต่ถ้า DB schema migration ทำลาย backward compatibility การ rollback app อย่างเดียวอาจไม่พอ
Traffic percent ไม่รับประกันว่าผู้ใช้คนใดจะได้รับ version เดิมตลอดโดยไม่คิด session behavior เพิ่ม
5. IAM invocation กับ ingress
หัวข้อที่มีชื่อว่า “5. IAM invocation กับ ingress”Invoker permission คุมว่า identity ใดเรียก service ได้ Ingress คุมแหล่ง/network path ที่ platform ยอมรับ request
ตั้ง ingress แคบไม่ได้แปลว่าไม่ต้อง authenticate และมี Invoker ก็ไม่ได้ข้าม ingress restriction ได้เสมอ
สำหรับ service private caller ใช้ ID token ที่มี audience ตรง service ตาม invocation model ไม่เอา access token ของ gcloud ใช้แทนทุกกรณี
ตัวอย่าง caller ที่มีสิทธิ์:
curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" \ "SERVICE_URL"ใช้ใน interactive lab ที่ credentials/token audience ตรงเงื่อนไข ห้ามพิมพ์ token ลง log/repo สำหรับ production workload ใช้ library/platform identity ตามคู่มือ
6. Runtime SA vs deployment identity
หัวข้อที่มีชื่อว่า “6. Runtime SA vs deployment identity”คน deploy ใช้ permission จัดการ service และ attach runtime SA ส่วน app รันเป็น runtime SA ที่เลือก สิทธิ์อ่าน secret/bucket/database ต้องให้ identity ที่ app ใช้จริง
Cloud Build/source deployment มี build identity ของตนด้วย เมื่อ build ดึง artifact ไม่ได้ ให้ตรวจ build identity ไม่เพิ่ม runtime data permissions อย่างไม่มีเหตุผล
7. Concurrency และ scaling
หัวข้อที่มีชื่อว่า “7. Concurrency และ scaling”| Setting | ทำอะไร | Tradeoff |
|---|---|---|
| Concurrency | จำนวน requests ที่ instance รับพร้อมกันตาม config | สูงขึ้นอาจใช้ resource ร่วมและ latency เพิ่ม |
| Minimum instances | จำนวนที่คงไว้ตาม mode/config | ลดบาง startup delay แต่มี cost |
| Maximum instances | จำกัดการขยายตาม config/semantics | ช่วยจำกัด load ที่ downstream แต่ไม่ใช่ financial hard cap |
| CPU/memory | resource ของแต่ละ instance | ต้องตรง workload และ pricing mode |
Maximum instances ไม่ใช่กลไกที่รับประกัน database connection ไม่เกินเลขหนึ่งแน่นอนทุก deployment/revision condition จึงต้องออกแบบ pool/connection strategy ด้วย
ตัวอย่างเอง: 20 instances × pool max 20 = 400 possible app connections ถ้า DB รองรับ 150 ต้องลด pool จำกัด scaling หรือใช้ architecture ที่เหมาะ ไม่ตั้ง max instances 100 โดยไม่คำนวณ
8. Cloud Run functions
หัวข้อที่มีชื่อว่า “8. Cloud Run functions”ใช้เขียน function handler แล้ว deploy ใน platform model ที่รองรับ เหมาะ reaction ต่อ event/HTTP ที่ logic ไม่ต้องจัด app server เต็มรูปแบบ
ชื่อ Cloud Functions ในเนื้อหาเก่าอาจปรากฏต่อเนื่องกับ Cloud Run functions อ่าน generation/product/version ให้ตรง ไม่ถือว่า console label เก่าหมายถึงมี feature เหมือนกันทุกด้าน
Event trigger อาจใช้ Pub/Sub, Cloud Storage object events และ Eventarc ตาม support; identity ของ trigger/caller/runtime อาจเป็นคนละตัว
9. Pub/Sub และ Eventarc
หัวข้อที่มีชื่อว่า “9. Pub/Sub และ Eventarc”Pub/Sub เป็น messaging system Eventarc ใช้ route events จาก providers ไป event consumers ที่รองรับ ไม่ใช่ database หรือ load balancer
Event-driven flow ต้องคิดว่า events/messages อาจถูกส่งซ้ำตาม delivery/retry semantics การประมวลผลจึงควร idempotent
ตัวอย่าง payment event เดิมเข้าซ้ำ ถ้า handler หักเงินทุกครั้งโดยไม่มี idempotency key จะเกิด duplicate transaction แม้ infrastructure ทำงานตามที่ออกแบบ
10. Retry, acknowledgement และ dead letter
หัวข้อที่มีชื่อว่า “10. Retry, acknowledgement และ dead letter”| กลไก | จุดประสงค์ |
|---|---|
| Acknowledge/success response | บอกว่าประมวลผลตาม delivery contract สำเร็จ |
| Retry/backoff | รอลองใหม่เมื่อ error ชั่วคราว |
| Idempotency key | ลดผลซ้ำจาก event เดิม |
| Dead-letter handling | แยก messages ที่ประมวลผลไม่ได้ตาม policy/ข้อจำกัด |
ไม่ควรตอบ success ก่อนทำงานสำคัญเสร็จแล้วหวังว่าจะ retry ให้ และไม่ควร retry ทุก error ไม่มี limit เพราะ configuration/data error อาจไม่หายเอง
11. Runbook: deploy แล้วไม่ขึ้น
หัวข้อที่มีชื่อว่า “11. Runbook: deploy แล้วไม่ขึ้น”- ดู build/deployment status และ image URL
- ดู container startup logs และ
PORT/bind address - ตรวจ runtime SA, secret/config และ outbound connectivity
- ตรวจ memory/startup/request timeout settings ตาม workload
- ถ้า 401/403 ตรวจ caller identity, invoker และ ingress
- ถ้า 5xx ตรวจ app/dependency ไม่ถือว่า platform ต้องเสียเสมอ
- ตรวจ revision traffic ว่า request เข้ารุ่นที่คาดหรือไม่
- Rollback เมื่อมี release regression พร้อมวิเคราะห์ data compatibility
API เล็ก ทีมไม่มีคนดูแล Kubernetes: Cloud Run มักตรงภาระ operation ถ้า workload contract รองรับ
Deploy ใหม่แต่ user ยังเห็นรุ่นเดิม: ตรวจ traffic allocation/tagged URL ไม่สรุปว่า cache เสมอ
Event เดิมทำงานสองครั้ง: ออกแบบ idempotency และดู retry/delivery contract ไม่ปิด retry เพื่อกลบปัญหาโดยไม่มีเหตุผล
12. Cloud Run Functions และ application lifecycle ตาม course path
หัวข้อที่มีชื่อว่า “12. Cloud Run Functions และ application lifecycle ตาม course path”หน้า course แยก Cloud Run fundamentals ออกจาก Cloud Run functions จึงควรแยก mental model ตาม entry point:
| รูปแบบ | เริ่มทำงานเมื่อ | ประเด็นที่ต้องกำหนด |
|---|---|---|
| Cloud Run service | รับ HTTP request | container contract, ingress, invoker, revisions, traffic, concurrency |
| Cloud Run function | HTTP trigger หรือ event trigger | trigger source, event schema, retries, identity, timeout และ idempotency |
| Workflow | ประสานหลายขั้นตอน/บริการ | authentication ระหว่างขั้น, error path, retry policy และผลข้างเคียง |
บทฝึก Cloud Run เพิ่มหัวข้อ resource model, container lifecycle, autoscaling, service identity, least-privilege policy, secrets/environment variables, development/test ในเครื่อง, revision deployment และการเชื่อม Pub/Sub
หัวข้อเสริม Cloud Run functions:
- ใช้ trigger ให้ตรงชนิดงาน: HTTP สำหรับ caller ที่เรียก endpoint; event-driven สำหรับ event source ที่รองรับ
- เชื่อม Workflows เมื่อจำเป็นต้องจัดลำดับการเรียกหลาย service; อย่าซ่อน business transaction ที่มี side effect ไว้ใน retry แบบไม่ idempotent
- Serverless VPC Access ช่วยกำหนดทางออกไป private network ตาม configuration; ตรวจ DNS, routes, firewall และปลายทางด้วย
- กำหนด function/service identity แยกจาก deployer; จำกัด IAM และตรวจ authentication ที่ caller ต้องใช้
- Secret ใช้ secret-management integration และสิทธิ์อ่านที่แคบ แยก secret จากค่าทั่วไปใน environment
- เมื่อ function เรียก Firestore/BigQuery ให้ตรวจ API, IAM, location, schema, quotas และ error/retry behavior
การเชื่อม database ไม่ทำให้ function กลายเป็น transaction coordinator โดยอัตโนมัติ และ BigQuery Remote Functions เป็น integration ระหว่าง query กับ remote code ตาม contract ของมัน ไม่ใช่ทางแทน SQL UDF สำหรับทุกงาน
Labs ใน course page: deploy HTTP/event-driven functions, เชื่อม functions กับ project resources, ใช้ Firestore; Cloud Run fundamentals มี lab เรื่อง least privilege IAM bindings และ Pub/Sub integration
เอกสารประกอบ
หัวข้อที่มีชื่อว่า “เอกสารประกอบ”13. Traffic splitting: canary ที่ปลอดภัยและข้อจำกัด
หัวข้อที่มีชื่อว่า “13. Traffic splitting: canary ที่ปลอดภัยและข้อจำกัด”Traffic splitting เปลี่ยนสัดส่วน routing ระหว่าง revisions ที่ deploy แล้ว โดยไม่ต้อง deploy ซ้ำทุกครั้งที่ปรับเปอร์เซ็นต์ ใช้ทดสอบ revision ใหม่กับสัดส่วนเล็กก่อน เช่น 95/5 แล้วขยายเมื่อ metric ผ่าน ทั้งนี้เปอร์เซ็นต์เป็นการกระจายคำขอในภาพรวม ไม่ใช่การรับประกันจำนวน request เป๊ะใน sample ขนาดเล็ก
ลำดับทำ canary
หัวข้อที่มีชื่อว่า “ลำดับทำ canary”- Deploy revision ใหม่แบบไม่รับ traffic เพื่อให้ตรวจ config และ startup ก่อน
- ตรวจ revision พร้อมรับคำขอ, health/logs และทดสอบ URL/tagged revision ตามที่ตั้งไว้
- ส่ง traffic ส่วนน้อย เช่น 5% ให้ revision ใหม่ และคง revision เดิมไว้รับส่วนที่เหลือ
- เทียบ error rate, latency, saturation, downstream errors และ business metric กับ baseline
- เพิ่มสัดส่วนเป็นช่วงเมื่อผลผ่าน หากแย่ลงให้ส่ง traffic กลับ revision เดิมทันที
- เมื่อมั่นใจค่อยส่ง 100% ไป revision ใหม่ และตรวจอีกครั้งหลัง cutover
คำสั่งแนวทาง (เปลี่ยน placeholder ให้ตรง service/region/revision จริง):
gcloud run deploy SERVICE \ --image=IMAGE_URL --region=REGION --no-traffic
gcloud run revisions list \ --service=SERVICE --region=REGION
gcloud run services update-traffic SERVICE \ --region=REGION \ --to-revisions=OLD_REVISION=95,NEW_REVISION=5
# ย้อนกลับให้ revision เดิมรับทั้งหมดgcloud run services update-traffic SERVICE \ --region=REGION \ --to-revisions=OLD_REVISION=100ตรวจผลหลังเปลี่ยนด้วย gcloud run services describe SERVICE --region=REGION หรือหน้า Revisions/Manage Traffic ใน Console แล้วดูชื่อ revision และเปอร์เซ็นต์จริง อย่าสมมติว่า traffic เปลี่ยนพร้อมกันทุก request ทันที: คำขอที่กำลังทำงานอาจจบที่ revision เดิม และการกระจายใหม่มีช่วงเปลี่ยนผ่าน
จุดที่ทำให้ canary เข้าใจผิด
หัวข้อที่มีชื่อว่า “จุดที่ทำให้ canary เข้าใจผิด”- Session affinity: ถ้าเปิดไว้ พฤติกรรม affinity อาจมีผลเหนือสัดส่วนที่คาดสำหรับผู้ใช้เดิม จึงต้องอ่านข้อจำกัดก่อนใช้ split เป็น A/B test
- State/database: ทั้งสอง revision อาจทำงานพร้อมกัน จึงต้องรองรับ schema แบบ backward compatible และหลีกเลี่ยง migration ที่ทำให้รุ่นเก่าใช้ไม่ได้
- Metrics น้อย: 5% ของ traffic ที่น้อยอยู่แล้วอาจให้ sample ไม่พอ ต้องกำหนดเวลา/จำนวนคำขอขั้นต่ำและเกณฑ์ stop
- Revision พร้อม แต่ dependency เสีย: health ของ container อย่างเดียวไม่ยืนยันว่าฐานข้อมูล, Pub/Sub หรือ API ปลายทางทำงานปกติ
- Scale/cost: revision ที่รับสัดส่วนเล็กยังอาจมี instance ขั้นต่ำหรือ resource cost ตาม config
- Rollback: traffic rollback คืน routing เท่านั้น ไม่คืน side effects, schema หรือข้อมูลที่ revision ใหม่เขียนไปแล้ว
เอกสาร: Gradual rollout, traffic migration, rollback · Cloud Run revisions
↩️ กลับไป Activity หลัก
หัวข้อที่มีชื่อว่า “↩️ กลับไป Activity หลัก”หน้านี้เป็นเนื้อหาเสริมหรือ Companion Lab ใช้ประกอบบทเหล่านี้ได้: