ทาง Google Cloud เพิ่งปล่อย Google Cloud Run ใน version beta ให้ใช้งาน
โดยให้เหล่านักพัฒนาสามารถ run serverless app ที่อยู่ใน Docker container ได้เลย ซึ่ง Google Cloud Run จัดการให้เองแบบอัตโนมัติ คือ
เมื่อมี request เข้ามาจะทำการ start ให้เอง
และเมื่อไม่มีการใช้งานจะทำการ stop ไปให้อีก
ชีวิตน่าจะง่าย สะดวกมากขึ้น

อีกทั้งการจัดการทั้งหมดจะอยู่บน Knative 
นั่นหมายความว่า
สามารถนำ serverless ที่ run อยู่บน Google Cloud Run
ไป deploy บน platform อื่น ๆ ที่ใช้ Kubernetes ได้อีกด้วย เช่น Google Kubernetes Engine (GKE)
ส่วนการ scale จะทำให้แบบอัตโนมัติ (Elastic scale) ตามจำนวน request นั่นเอง

มาลอง deploy ระบบงานง่าย ๆ กันดีกว่า

ทำ Hello app ซึ่งจะ Build เป็น Docker Image เพื่อ deploy กัน
ส่วนเรื่องภาษาก็เอาที่สบายใจ
ในหน้า Quick Start นั้นมีหลายภาษาเลย
ทั้ง Go, Node, Python, PHP, Ruby และอื่น ๆ

โดยใน blog นี้เลือกใช้ Go แค่ทำ Web Server ให้แสดงคำว่า Hello World พอนะ
สามารถดูเพิ่มเติ่มได้ใน Github:Up1

ขั้นตอนการพัฒนาและ deploy เป็นดังนี้

ขั้นตอนที่ 1 ทำการ build Docker Image และ run Docker container ในเครื่องเราก่อน

$docker image build -t somkiat/hello_cloud_run:1.0 .
$docker container run -d -p 8080:8080 somkiat/hello_cloud_run:1.0
$curl http://localhost:8080/
Hello Google Cloud Run!

ขั้นตอนที่ 2 ทำการ submit และสร้าง Docker image ไปไว้ที่ Google Container Registry

จากที่ลอง deploy ด้วย Docker image ที่อยู่ใน Docker Hub จะไม่ได้นะ !!

$gcloud builds submit --tag gcr.io/[PROJECT-ID]/helloworld

ขั้นตอนที่ 3 ทำการ deploy serverless บน Google Cloud Run

จากที่ลองสามารถ deploy ได้บาง region เท่านั้น
เพราะว่ายังอยู่ในสถานะ beta นั่นเอง
จากตัวอย่างผมทำการ deploy ไปยัง region us-central-1

$gcloud components install beta
$gcloud components update
$gcloud beta run deploy --image gcr.io/[PROJECT-ID]/helloworld --region us-central1
✓ Deploying new service... Done.
  ✓ Creating Revision...
  ✓ Routing traffic...
  ✓ Setting IAM Policy...
Done.
Service [helloworld] revision [helloworld-00001] has been deployed and is serving traffic at https://helloworld-6tqzl55hla-uc.a.run.app

จะได้ URL สำหรับ app ที่เรา deploy มาให้ใช้งานทันที

และสามารถเข้าไปดูใน Google Cloud Console ได้เลย

Google Cloud Run ลักษณะเหมือนกับพวก Serverless หรือ Function as a Service ต่าง ๆ

ไม่ว่าจะเป็น Google Cloud Functions, AWS Lambda และ Azure Functions
แต่ต่างกันมากเรื่องไม่ผูกมัดกับ platform
กับเรื่องของเทคโนโลยี และ ภาษาโปรแกรม
อีกทั้งยังสามารถใช้งาน Docker ได้อีก ยิ่งทำให้สะดวกขึ้นไปอีก
เพราะว่า สามารถทดสอบที่ local ก่อนได้เลย ไม่ต้องมีเรื่องเยอะ

นั่นหมายความว่าคู่แข่งของ Google Cloud Run จริง ๆ
คือ Azure Container Instances และ AWS Fargate นั่นเอง

มาลองใช้งานกันดูครับ

Reference Websites