แสดงเนื้อหาแบบไดนามิกและโฮสต์ Microservice ด้วย Cloud Run

ไม่รองรับการผสานรวมกับCloud Run for Anthos

จับคู่ Cloud Run กับ Firebase Hosting เพื่อสร้างและแสดงเนื้อหาแบบไดนามิก หรือสร้าง REST API เป็น Microservice

การใช้ Cloud Run ช่วยให้คุณ ทําให้แอปพลิเคชันที่แพ็กเกจในอิมเมจคอนเทนเนอร์ใช้งานได้ จากนั้นคุณจะใช้ Firebase Hostingเพื่อส่งคำขอ HTTPS ให้ทริกเกอร์แอปที่ทำคอนเทนเนอร์ได้

ดูตัวอย่าง Use Case และตัวอย่างสำหรับ Cloud Run ที่ผสานรวมกับ Firebase Hosting ได้ที่ภาพรวมของ Serverless


คู่มือนี้แสดงวิธีทำสิ่งต่อไปนี้

  1. เขียนแอปพลิเคชัน Hello World แบบง่ายๆ
  2. สร้างคอนเทนเนอร์ของแอปและอัปโหลดไปยัง Artifact Registry
  3. ทำให้อิมเมจคอนเทนเนอร์ใช้งานได้กับ Cloud Run
  4. ส่งคำขอHostingไปยังแอปที่มีคอนเทนเนอร์โดยตรง

โปรดทราบว่าคุณสามารถปรับการตั้งค่าแคชได้ (ไม่บังคับ) เพื่อปรับปรุงประสิทธิภาพการแสดงเนื้อหาแบบไดนามิก

ก่อนเริ่มต้น

ก่อนใช้ Cloud Run คุณต้องทํางานเบื้องต้นบางอย่างให้เสร็จสมบูรณ์ ซึ่งรวมถึงการตั้งค่าบัญชี Cloud Billing การเปิดใช้ Cloud Run API และการติดตั้งเครื่องมือบรรทัดคําสั่ง gcloud

ตั้งค่าการเรียกเก็บเงินสำหรับโปรเจ็กต์

Cloud Run มีโควต้าการใช้งานฟรี แต่คุณยังคงต้องมีCloud Billing บัญชี ที่เชื่อมโยงกับโปรเจ็กต์ Firebase เพื่อใช้หรือลองใช้ Cloud Run

เปิดใช้ API และติดตั้ง SDK

  1. เปิดใช้ Cloud Run API ในคอนโซล Google APIs โดยทำดังนี้

    1. เปิดหน้า API ของ Cloud Run ในคอนโซล Google APIs

    2. เมื่อได้รับข้อความแจ้ง ให้เลือกโปรเจ็กต์ Firebase

    3. คลิกเปิดใช้ในหน้า API ของ Cloud Run

  2. ติดตั้งและเริ่มต้น Cloud SDK

  3. ตรวจสอบว่าได้กำหนดค่าเครื่องมือ gcloud สำหรับโปรเจ็กต์ที่ถูกต้องแล้ว

    gcloud config list

ขั้นตอนที่ 1: เขียนแอปพลิเคชันตัวอย่าง

โปรดทราบว่า Cloud Run รองรับ ภาษาอื่นๆ อีกมากมาย นอกเหนือจากภาษาที่แสดงในตัวอย่างต่อไปนี้

Go

  1. สร้างไดเรกทอรีใหม่ชื่อ helloworld-go จากนั้นเปลี่ยนไดเรกทอรีเป็น ไดเรกทอรีนั้น

    mkdir helloworld-go
    cd helloworld-go
  2. สร้างไฟล์ใหม่ชื่อ helloworld.go แล้วเพิ่มโค้ดต่อไปนี้

    package main
    
    import (
    	"fmt"
    	"log"
    	"net/http"
    	"os"
    )
    
    func handler(w http.ResponseWriter, r *http.Request) {
    	log.Print("helloworld: received a request")
    	target := os.Getenv("TARGET")
    	if target == "" {
    		target = "World"
    	}
    	fmt.Fprintf(w, "Hello %s!\n", target)
    }
    
    func main() {
    	log.Print("helloworld: starting server...")
    
    	http.HandleFunc("/", handler)
    
    	port := os.Getenv("PORT")
    	if port == "" {
    		port = "8080"
    	}
    
    	log.Printf("helloworld: listening on port %s", port)
    	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
    }
    

    โค้ดนี้จะสร้างเว็บเซิร์ฟเวอร์พื้นฐานที่รอฟังพอร์ตซึ่งกำหนดโดยตัวแปรสภาพแวดล้อม PORT

แอปของคุณเสร็จสมบูรณ์แล้วและพร้อมที่จะสร้างคอนเทนเนอร์และอัปโหลดไปยัง Artifact Registry

Node.js

  1. สร้างไดเรกทอรีใหม่ชื่อ helloworld-nodejs จากนั้นเปลี่ยนไดเรกทอรี เป็นไดเรกทอรีใหม่

    mkdir helloworld-nodejs
    cd helloworld-nodejs
  2. สร้างไฟล์ package.json ที่มีเนื้อหาต่อไปนี้

    {
      "name": "knative-serving-helloworld",
      "version": "1.0.0",
      "description": "Simple hello world sample in Node",
      "main": "index.js",
      "scripts": {
        "start": "node index.js"
      },
      "author": "",
      "license": "Apache-2.0",
      "dependencies": {
        "express": "^4.21.2"
      }
    }
    
  3. สร้างไฟล์ใหม่ชื่อ index.js แล้วเพิ่มโค้ดต่อไปนี้

    const express = require('express');
    const app = express();
    
    app.get('/', (req, res) => {
      console.log('Hello world received a request.');
    
      const target = process.env.TARGET || 'World';
      res.send(`Hello ${target}!\n`);
    });
    
    const port = process.env.PORT || 8080;
    app.listen(port, () => {
      console.log('Hello world listening on port', port);
    });
    

    โค้ดนี้จะสร้างเว็บเซิร์ฟเวอร์พื้นฐานที่รอฟังพอร์ตซึ่งกำหนดโดยตัวแปรสภาพแวดล้อม PORT

แอปของคุณเสร็จสมบูรณ์แล้วและพร้อมที่จะสร้างคอนเทนเนอร์และอัปโหลดไปยัง Artifact Registry

Python

  1. สร้างไดเรกทอรีใหม่ชื่อ helloworld-python จากนั้นเปลี่ยนไดเรกทอรี เป็นไดเรกทอรีใหม่

    mkdir helloworld-python
    cd helloworld-python
  2. สร้างไฟล์ใหม่ชื่อ app.py แล้วเพิ่มโค้ดต่อไปนี้

    import os
    
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
        target = os.environ.get('TARGET', 'World')
        return 'Hello {}!\n'.format(target)
    
    if __name__ == "__main__":
        app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))
    

    โค้ดนี้จะสร้างเว็บเซิร์ฟเวอร์พื้นฐานที่รอฟังพอร์ตซึ่งกำหนดโดยตัวแปรสภาพแวดล้อม PORT

แอปของคุณเสร็จสมบูรณ์แล้วและพร้อมที่จะสร้างคอนเทนเนอร์และอัปโหลดไปยัง Artifact Registry

Java

  1. ติดตั้ง JDK ของ Java SE 8 ขึ้นไป และ CURL

    โปรดทราบว่าเราต้องทำขั้นตอนนี้เพื่อสร้างโปรเจ็กต์เว็บใหม่ใน ขั้นตอนถัดไปเท่านั้น Dockerfile ซึ่งจะอธิบายในภายหลังจะโหลดการอ้างอิงทั้งหมดลงในคอนเทนเนอร์

  2. จากคอนโซล ให้สร้างโปรเจ็กต์เว็บว่างใหม่โดยใช้ cURL แล้วใช้คำสั่ง unzip ดังนี้

    curl https://start.spring.io/starter.zip \
        -d dependencies=web \
        -d name=helloworld \
        -d artifactId=helloworld \
        -o helloworld.zip
    unzip helloworld.zip

    ซึ่งจะสร้างโปรเจ็กต์ SpringBoot

  3. อัปเดตคลาส SpringBootApplication ใน src/main/java/com/example/helloworld/HelloworldApplication.java โดย เพิ่ม @RestController เพื่อจัดการการแมป / และเพิ่ม ฟิลด์ @Value เพื่อระบุตัวแปรสภาพแวดล้อม TARGET ด้วย

    package com.example.helloworld;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    public class HelloworldApplication {
    
      @Value("${TARGET:World}")
      String target;
    
      @RestController
      class HelloworldController {
        @GetMapping("/")
        String hello() {
          return "Hello " + target + "!";
        }
      }
    
      public static void main(String[] args) {
        SpringApplication.run(HelloworldApplication.class, args);
      }
    }
    

    โค้ดนี้จะสร้างเว็บเซิร์ฟเวอร์พื้นฐานที่รอฟังพอร์ตซึ่งกำหนดโดยตัวแปรสภาพแวดล้อม PORT

แอปของคุณเสร็จสมบูรณ์แล้วและพร้อมที่จะสร้างคอนเทนเนอร์และอัปโหลดไปยัง Artifact Registry

ขั้นตอนที่ 2: สร้างคอนเทนเนอร์ของแอปและอัปโหลดไปยัง Artifact Registry

  1. สร้างคอนเทนเนอร์แอปตัวอย่างโดยสร้างไฟล์ใหม่ชื่อ Dockerfile ในไดเรกทอรีเดียวกับไฟล์ต้นฉบับ คัดลอกเนื้อหาต่อไปนี้ลงใน ไฟล์

    Go

    # Use the official Golang image to create a build artifact.
    # This is based on Debian and sets the GOPATH to /go.
    FROM golang:latest AS builder
    
    ARG TARGETOS
    ARG TARGETARCH
    
    # Create and change to the app directory.
    WORKDIR /app
    
    # Copy local code to the container image.
    COPY . ./
    
    # Install dependencies and tidy up the go.mod and go.sum files.
    RUN go mod tidy
    
    # Build the binary.
    # -mod=readonly ensures immutable go.mod and go.sum in container builds.
    RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -mod=readonly -v -o server
    
    # Use the official Alpine image for a lean production container.
    # https://hub.docker.com/_/alpine
    # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
    FROM alpine:3
    RUN apk add --no-cache ca-certificates
    
    # Copy the binary to the production image from the builder stage.
    COPY --from=builder /app/server /server
    
    # Run the web service on container startup.
    CMD ["/server"]
    

    Node.js

    # Use the official lightweight Node.js 12 image.
    # https://hub.docker.com/_/node
    FROM node:12-slim
    
    # Create and change to the app directory.
    WORKDIR /usr/src/app
    
    # Copy application dependency manifests to the container image.
    # A wildcard is used to ensure both package.json AND package-lock.json are copied.
    # Copying this separately prevents re-running npm install on every code change.
    COPY package*.json ./
    
    # Install production dependencies.
    RUN npm install --only=production
    
    # Copy local code to the container image.
    COPY . ./
    
    # Run the web service on container startup.
    CMD [ "npm", "start" ]
    

    Python

    # Use the official lightweight Python image.
    # https://hub.docker.com/_/python
    FROM python:3.7-slim
    
    # Allow statements and log messages to immediately appear in the Knative logs
    ENV PYTHONUNBUFFERED True
    
    # Copy local code to the container image.
    ENV APP_HOME /app
    WORKDIR $APP_HOME
    COPY . ./
    
    # Install production dependencies.
    RUN pip install Flask gunicorn
    
    # Run the web service on container startup. Here we use the gunicorn
    # webserver, with one worker process and 8 threads.
    # For environments with multiple CPU cores, increase the number of workers
    # to be equal to the cores available.
    CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 app:app
    

    Java

    # Use the official maven/Java 8 image to create a build artifact: https://hub.docker.com/_/maven
    FROM maven:3.5-jdk-8-alpine AS builder
    
    # Copy local code to the container image.
    WORKDIR /app
    COPY pom.xml .
    COPY src ./src
    
    # Build a release artifact.
    RUN mvn package -DskipTests
    
    # Use the Official OpenJDK image for a lean production stage of our multi-stage build.
    # https://hub.docker.com/_/openjdk
    # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
    FROM openjdk:8-jre-alpine
    
    # Copy the jar to the production image from the builder stage.
    COPY --from=builder /app/target/helloworld-*.jar /helloworld.jar
    
    # Run the web service on container startup.
    CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/helloworld.jar"]
    

  2. สร้างอิมเมจคอนเทนเนอร์โดยใช้ Cloud Build โดยเรียกใช้คำสั่งต่อไปนี้จากไดเรกทอรีที่มี Dockerfile

    gcloud builds submit --tag gcr.io/PROJECT_ID/helloworld

    เมื่อดำเนินการสำเร็จ คุณจะเห็นข้อความ SUCCESS ที่มีชื่อรูปภาพ
    (gcr.io/PROJECT_ID/helloworld)

ตอนนี้ระบบจัดเก็บอิมเมจคอนเทนเนอร์ไว้ใน Artifact Registry และสามารถนำกลับมาใช้ใหม่ได้หากต้องการ

โปรดทราบว่าคุณสามารถใช้ Docker เวอร์ชันที่ติดตั้งในเครื่องแทน Cloud Build เพื่อสร้างคอนเทนเนอร์ในเครื่องได้

ขั้นตอนที่ 3: นำอิมเมจคอนเทนเนอร์ไปใช้งานใน Cloud Run

  1. ติดตั้งใช้งานโดยใช้คำสั่งต่อไปนี้

    gcloud run deploy --image gcr.io/PROJECT_ID/helloworld

  2. เมื่อได้รับข้อความแจ้ง ให้ทำดังนี้

  3. รอสักครู่เพื่อให้การติดตั้งใช้งานเสร็จสมบูรณ์ เมื่อสำเร็จแล้ว บรรทัดคำสั่ง จะแสดง URL ของบริการ เช่น https://helloworld-RANDOM_HASH-us-central1.a.run.app

  4. ไปที่คอนเทนเนอร์ที่ติดตั้งใช้งานโดยเปิด URL ของบริการในเว็บเบราว์เซอร์

ขั้นตอนถัดไปจะแนะนำวิธีเข้าถึงแอปที่อยู่ในคอนเทนเนอร์นี้จากFirebase Hosting URL เพื่อให้แอปสร้างเนื้อหาแบบไดนามิกสำหรับเว็บไซต์ที่โฮสต์ใน Firebase ได้

ขั้นตอนที่ 4: ส่งคำขอโฮสติ้งโดยตรงไปยังแอปที่สร้างโดยใช้คอนเทนเนอร์

กฎการเขียนใหม่ช่วยให้คุณส่งคำขอ ที่ตรงกับรูปแบบที่เฉพาะเจาะจงไปยังปลายทางเดียวได้

ตัวอย่างต่อไปนี้แสดงวิธีเปลี่ยนเส้นทางคำขอทั้งหมดจากหน้าเว็บ /helloworldในเว็บไซต์ Hosting เพื่อทริกเกอร์การเริ่มต้นและการเรียกใช้ อินสแตนซ์คอนเทนเนอร์ helloworld

  1. โปรดตรวจสอบดังนี้

    ดูวิธีการติดตั้ง CLI และการเริ่มต้นใช้งานHostingแบบละเอียดได้ที่คู่มือการเริ่มต้นใช้งานสำหรับ Hosting

  2. เปิดไฟล์ firebase.json

  3. เพิ่มการกำหนดค่า rewrite ต่อไปนี้ในส่วน hosting

    "hosting": {
      // ...
    
      // Add the "rewrites" attribute within "hosting"
      "rewrites": [ {
        "source": "/helloworld",
        "run": {
          "serviceId": "helloworld",  // "service name" (from when you deployed the container image)
          "region": "us-central1",    // optional (if omitted, default is us-central1)
          "pinTag": true              // optional (see note below)
        }
      } ]
    }
  4. ติดตั้งใช้งานการกำหนดค่าโฮสติ้งกับเว็บไซต์โดยเรียกใช้คำสั่งต่อไปนี้จากรูทของไดเรกทอรีโปรเจ็กต์

    firebase deploy --only hosting

ตอนนี้คุณเข้าถึงคอนเทนเนอร์ได้ผ่าน URL ต่อไปนี้

ไปที่Hostingหน้าการกำหนดค่าเพื่อดูรายละเอียดเพิ่มเติมเกี่ยวกับกฎการเขียนใหม่ นอกจากนี้ คุณยังดูข้อมูลเกี่ยวกับลำดับความสำคัญของคำตอบสำหรับHostingการกำหนดค่าต่างๆ ได้ด้วย

ทดสอบในเครื่อง

ในระหว่างการพัฒนา คุณสามารถเรียกใช้และทดสอบอิมเมจคอนเทนเนอร์ภายในได้ ดูวิธีการโดยละเอียดได้ที่เอกสารประกอบของ Cloud Run

ขั้นตอนถัดไป