本機疑難排解

本教學課程說明如何使用 Stackdriver 工具進行探索,並使用本機開發工作流程進行調查,排解 Knative serving 服務中斷的問題。

這份逐步「個案研究」是疑難排解指南的輔助說明,使用範例專案,在部署時會產生執行階段錯誤,您可透過疑難排解找出並修正問題。

目標

  • 編寫、建構服務,並部署至 Knative serving
  • 使用 Cloud Logging 找出錯誤
  • 從 Container Registry 擷取容器映像檔,以便進行根本原因分析
  • 修正「正式環境」服務,然後改善服務,減少日後發生問題

費用

在本文件中,您會使用下列 Google Cloud計費元件:

您可以使用 Pricing Calculator,根據預測用量估算費用。

初次使用 Google Cloud 的使用者可能符合免費試用期資格。

事前準備

正在組合程式碼

逐步建構新的 Knative serving greeter 服務。提醒您,這項服務會刻意建立執行階段錯誤,以便進行疑難排解練習。

  1. 建立新專案:

    Node.js

    定義服務套件、初始依附元件和一些常見作業,建立 Node.js 專案。

    1. 建立 hello-service 目錄:

      mkdir hello-service
      cd hello-service
      
    2. 生成 package.json 檔案:

      npm init --yes
      npm install express@4
      
    3. 在編輯器中開啟新的 package.json 檔案,並設定 start 指令碼來執行 node index.js。完成後,檔案會如下所示:

      {
        "name": "hello-service",
        "version": "1.0.0",
        "description": "",
        "main": "index.js",
        "scripts": {
            "start": "node index.js",
            "test": "echo \"Error: no test specified\" && exit 1"
        },
        "keywords": [],
        "author": "",
        "license": "ISC",
        "dependencies": {
            "express": "^4.17.1"
        }
      }

    如果您在完成本教學課程後繼續開發這項服務,請考慮填寫說明、作者和評估授權。詳情請參閱 package.json 說明文件。

    Python

    1. 建立新的 hello-service 目錄:

      mkdir hello-service
      cd hello-service
      
    2. 建立 requirements.txt 檔案,並將依附元件複製到檔案中:

      pytest==9.0.3; python_version >= "3.10"
      Flask==3.1.3; python_version >= '3.9'
      gunicorn==23.0.0
      Werkzeug==3.1.9; python_version >= '3.10'
      

    Go

    1. 建立 hello-service 目錄:

      mkdir hello-service
      cd hello-service
      
    2. 初始化新的 Go 模組,建立 Go 專案:

      go mod init <var>my-domain</var>.com/hello-service
      

    您可以視需要更新特定名稱:如果程式碼發布至可透過網路存取的程式碼存放區,則應更新名稱。

    Java

    1. 建立 Maven 專案:

      mvn archetype:generate \
        -DgroupId=com.example \
        -DartifactId=hello-service \
        -DarchetypeArtifactId=maven-archetype-quickstart \
        -DinteractiveMode=false
      
    2. 將依附元件複製到 pom.xml 依附元件清單 (<dependencies> 元素之間):

      <dependency>
        <groupId>com.sparkjava</groupId>
        <artifactId>spark-core</artifactId>
        <version>2.9.4</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>2.0.12</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.12</version>
      </dependency>
      
    3. 將建構設定複製到 pom.xml (位於 <dependencies> 元素下方):

      <build>
        <plugins>
          <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>jib-maven-plugin</artifactId>
            <version>3.4.0</version>
            <configuration>
              <to>
                <image>gcr.io/PROJECT_ID/hello-service</image>
              </to>
            </configuration>
          </plugin>
        </plugins>
      </build>
      

  2. 建立 HTTP 服務來處理傳入要求:

    Node.js

    const express = require('express');
    const app = express();
    
    app.get('/', (req, res) => {
      console.log('hello: received request.');
    
      const {NAME} = process.env;
      if (!NAME) {
        // Plain error logs do not appear in Stackdriver Error Reporting.
        console.error('Environment validation failed.');
        console.error(new Error('Missing required server parameter'));
        return res.status(500).send('Internal Server Error');
      }
      res.send(`Hello ${NAME}!`);
    });
    const port = parseInt(process.env.PORT) || 8080;
    app.listen(port, () => {
      console.log(`hello: listening on port ${port}`);
    });

    Python

    import json
    import os
    
    from flask import Flask
    
    
    app = Flask(__name__)
    
    
    @app.route("/", methods=["GET"])
    def index():
        """Example route for testing local troubleshooting.
    
        This route may raise an HTTP 5XX error due to missing environment variable.
        """
        print("hello: received request.")
    
        NAME = os.getenv("NAME")
    
        if not NAME:
            print("Environment validation failed.")
            raise Exception("Missing required service parameter.")
    
        return f"Hello {NAME}"
    
    
    if __name__ == "__main__":
        PORT = int(os.getenv("PORT")) if os.getenv("PORT") else 8080
    
        # This is used when running locally. Gunicorn is used to run the
        # application on Cloud Run. See entrypoint in Dockerfile.
        app.run(host="127.0.0.1", port=PORT, debug=True)

    Go

    
    // Sample hello demonstrates a difficult to troubleshoot service.
    package main
    
    import (
    	"fmt"
    	"log"
    	"net/http"
    	"os"
    )
    
    func main() {
    	log.Print("hello: service started")
    
    	http.HandleFunc("/", helloHandler)
    
    
    	port := os.Getenv("PORT")
    	if port == "" {
    		port = "8080"
    		log.Printf("Defaulting to port %s", port)
    	}
    
    	log.Printf("Listening on port %s", port)
    	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
    }
    
    func helloHandler(w http.ResponseWriter, r *http.Request) {
    	log.Print("hello: received request")
    
    	name := os.Getenv("NAME")
    	if name == "" {
    		log.Printf("Missing required server parameter")
    		// The panic stack trace appears in Cloud Error Reporting.
    		panic("Missing required server parameter")
    	}
    
    	fmt.Fprintf(w, "Hello %s!\n", name)
    }
    

    Java

    import static spark.Spark.get;
    import static spark.Spark.port;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class App {
    
      private static final Logger logger = LoggerFactory.getLogger(App.class);
    
      public static void main(String[] args) {
        int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
        port(port);
    
        get(
            "/",
            (req, res) -> {
              logger.info("Hello: received request.");
              String name = System.getenv("NAME");
              if (name == null) {
                // Standard error logs do not appear in Stackdriver Error Reporting.
                System.err.println("Environment validation failed.");
                String msg = "Missing required server parameter";
                logger.error(msg, new Exception(msg));
                res.status(500);
                return "Internal Server Error";
              }
              res.status(200);
              return String.format("Hello %s!", name);
            });
      }
    }

  3. 建立 Dockerfile,定義用於部署服務的容器映像檔:

    Node.js

    
    # Use the official lightweight Node.js image.
    # https://hub.docker.com/_/node
    FROM node:20-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 copying both package.json AND package-lock.json (when available).
    # Copying this first prevents re-running npm install on every code change.
    COPY package*.json ./
    
    # Install dependencies.
    # if you need a deterministic and repeatable build create a
    # package-lock.json file and use npm ci:
    # RUN npm ci --omit=dev
    # if you need to include development dependencies during development
    # of your application, use:
    # RUN npm install --dev
    
    RUN npm install --omit=dev
    
    # Copy local code to the container image.
    COPY . ./
    
    # Run the web service on container startup.
    CMD [ "npm", "start" ]
    

    Python

    
    # Use the official Python image.
    # https://hub.docker.com/_/python
    FROM python:3.14
    
    # Allow statements and log messages to immediately appear in the Cloud Run logs
    ENV PYTHONUNBUFFERED True
    
    # Copy application dependency manifests to the container image.
    # Copying this separately prevents re-running pip install on every code change.
    COPY requirements.txt ./
    
    # Install production dependencies.
    RUN pip install -r requirements.txt
    
    # Copy local code to the container image.
    ENV APP_HOME /app
    WORKDIR $APP_HOME
    COPY . ./
    
    # Run the web service on container startup.
    # Use 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.
    # Timeout is set to 0 to disable the timeouts of the workers to allow Cloud Run to handle instance scaling.
    CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
    

    Go

    
    # Use the official Go image to create a binary.
    # This is based on Debian and sets the GOPATH to /go.
    # https://hub.docker.com/_/golang
    FROM golang:1.25-bookworm as builder
    
    # Create and change to the app directory.
    WORKDIR /app
    
    # Retrieve application dependencies.
    # This allows the container build to reuse cached dependencies.
    # Expecting to copy go.mod and if present go.sum.
    COPY go.* ./
    RUN go mod download
    
    # Copy local code to the container image.
    COPY . ./
    
    # Build the binary.
    RUN go build -v -o server
    
    # Use the official Debian slim image for a lean production container.
    # https://hub.docker.com/_/debian
    # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
    FROM debian:bookworm-slim
    RUN set -x && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
        ca-certificates && \
        rm -rf /var/lib/apt/lists/*
    
    # 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"]
    

    Java

    這個範例使用 Jib,透過常見的 Java 工具建構 Docker 映像檔。Jib 可最佳化容器建構作業,不需要 Dockerfile 或安裝 Docker。

    <plugin>
      <groupId>com.google.cloud.tools</groupId>
      <artifactId>jib-maven-plugin</artifactId>
      <version>3.4.0</version>
      <configuration>
        <to>
          <image>gcr.io/PROJECT_ID/hello-service</image>
        </to>
      </configuration>
    </plugin>
    

推送程式碼

推送程式碼包含三個步驟:使用 Cloud Build 建構容器映像檔、將容器映像檔上傳至 Container Registry,以及將容器映像檔部署至 Knative serving。

如要推送程式碼:

  1. 建構容器並發布至 Container Registry:

    Node.js

    gcloud builds submit --tag gcr.io/PROJECT_ID/hello-service

    其中 PROJECT_ID 是您的 Google Cloud 專案 ID。您可以使用 gcloud config get-value project 查看目前的專案 ID。

    成功後,您會看到包含 ID、建立時間和映像檔名稱的「SUCCESS」訊息。映像檔會儲存在 Container Registry 中,日後如有需要,可以重複使用。

    Python

    gcloud builds submit --tag gcr.io/PROJECT_ID/hello-service

    其中 PROJECT_ID 是您的 Google Cloud 專案 ID。您可以使用 gcloud config get-value project 查看目前的專案 ID。

    成功後,您會看到包含 ID、建立時間和映像檔名稱的「SUCCESS」訊息。映像檔會儲存在 Container Registry 中,日後如有需要,可以重複使用。

    Go

    gcloud builds submit --tag gcr.io/PROJECT_ID/hello-service

    其中 PROJECT_ID 是您的 Google Cloud 專案 ID。您可以使用 gcloud config get-value project 查看目前的專案 ID。

    成功後,您會看到包含 ID、建立時間和映像檔名稱的「SUCCESS」訊息。映像檔會儲存在 Container Registry 中,日後如有需要,可以重複使用。

    Java

    mvn compile jib:build -Dimage=gcr.io/PROJECT_ID/hello-service

    其中 PROJECT_ID 是您的 Google Cloud 專案 ID。您可以使用 gcloud config get-value project 查看目前的專案 ID。

    成功後,您應該會看到「BUILD SUCCESS」訊息。映像檔會儲存在 Container Registry 中,日後如有需要,可以重複使用。

  2. 執行下列指令來部署您的應用程式:

    gcloud run deploy hello-service --image gcr.io/PROJECT_ID/hello-service

    將 PROJECT_ID 替換為 Google Cloud 專案 ID。hello-service 是容器映像檔名稱,也是 Knative Serving 服務的名稱。請注意,容器映像檔會部署到您先前在「設定 gcloud」部分設定的服務和叢集

    等待部署作業完成,這項作業約需 30 秒。部署成功後,指令列會顯示服務網址。

立即體驗

試用服務,確認已成功部署。要求應失敗,並傳回 HTTP 500 或 503 錯誤 (5xx 伺服器錯誤類別的成員)。本教學課程會逐步說明如何排解這項錯誤回應。

如果叢集已設定可轉送的預設網域,請略過上述步驟,改為將網址複製到網路瀏覽器。

如果不使用自動 TLS 憑證和網域對應,系統就不會為服務提供可導覽的網址。

請改用提供的網址和服務輸入閘道的 IP 位址,建立可向服務提出要求的 curl 指令:

  1. 執行下列指令,取得負載平衡器的外部 IP:

    kubectl get svc istio-ingressgateway -n ASM-INGRESS-NAMESPACE

    將 ASM-INGRESS-NAMESPACE 替換為 Cloud Service Mesh 輸入所在的命名空間。如果您使用預設設定安裝 Cloud Service Mesh,請指定 istio-system。

    輸出內容大致如下:

    NAME                   TYPE           CLUSTER-IP     EXTERNAL-IP  PORT(S)
    istio-ingressgateway   LoadBalancer   XX.XX.XXX.XX   pending      80:32380/TCP,443:32390/TCP,32400:32400/TCP

    其中 EXTERNAL-IP 值是負載平衡器的外部 IP 位址。

  2. 在網址中使用這個 GATEWAY_IP 位址,執行 curl 指令。

     curl -G -H "Host: SERVICE-DOMAIN" https://EXTERNAL-IP/

    將 SERVICE-DOMAIN 替換為服務的預設指派網域。只要採用預設網址並移除通訊協定 http://,即可取得這個網址。

  3. 查看 HTTP 500 或 HTTP 503 錯誤訊息。

調查問題

想像一下,您在試用時遇到的 HTTP 5xx 錯誤,是實際執行階段錯誤。本教學課程將逐步說明處理這類問題的正式程序。雖然正式環境錯誤解決程序有很大的差異,但本教學課程會提供一系列步驟,說明如何應用實用的工具和技術。

如要調查這個問題,您需要完成下列階段:

  • 收集更多有關回報錯誤的詳細資料,以便進一步調查並制定緩解策略。
  • 決定要推送修正程式,還是復原至已知正常的版本,以減輕對使用者的影響。
  • 重現錯誤,確認已收集正確的詳細資料,且錯誤並非一次性故障
  • 對錯誤進行根本原因分析,找出造成這項錯誤的程式碼、設定或程序

調查開始時,您會取得網址、時間戳記和「內部伺服器錯誤」訊息。

收集更多詳細資料

收集更多問題相關資訊,瞭解發生什麼事,並決定後續步驟。

使用可用工具收集更多詳細資料:

  1. 詳情請查看記錄檔。

  2. 使用 Cloud Logging 查看導致問題的操作順序,包括錯誤訊息。

復原至健康狀態良好的版本

如果您知道某個修訂版本運作正常,可以將服務復原至該修訂版本。舉例來說,您無法對在本教學課程中部署的新 hello-service 服務執行復原作業,因為該服務只包含一個修訂版本。

如要找出修訂版本並復原服務,請按照下列步驟操作:

  1. 列出服務的所有修訂版本。

  2. 將所有流量遷移至健康狀態良好的修訂版本。

重現錯誤

使用先前取得的詳細資料,確認問題在測試條件下會持續發生。

再次試用,傳送相同的 HTTP 要求,看看是否會回報相同的錯誤和詳細資料。錯誤詳細資料可能需要一些時間才會顯示。

本教學課程中的範例服務為唯讀,不會觸發任何複雜的副作用,因此在實際工作環境中重現錯誤是安全的。不過,許多實際服務並非如此:您可能需要在測試環境中重現錯誤,或將這個步驟限制在本機調查。

重現錯誤可為後續工作建立背景資訊。舉例來說,如果開發人員無法重現錯誤,可能需要對服務進行額外檢測,才能進一步調查。

執行根本原因分析

根本原因分析是有效疑難排解的重要步驟,可確保您修正問題,而非只是解決症狀。

在本教學課程的前面部分,您已在 Knative serving 重現問題,確認服務託管於 Knative serving 時,問題確實存在。現在,請在本機重現問題,判斷問題是否僅限於程式碼,或只在實際工作環境代管中出現。

  1. 如果您尚未在本機搭配使用 Docker CLI 與 Container Registry,請透過 gcloud 進行驗證:

    gcloud auth configure-docker

    如需其他方法,請參閱「Container Registry 驗證方式」。

  2. 如果無法取得最近使用的容器映像檔名稱,服務說明會提供最近部署的容器映像檔資訊:

    gcloud run services describe hello-service

    在 spec 物件中找出容器映像檔名稱。您可以使用更精確的指令直接擷取:

    gcloud run services describe hello-service \
       --format="value(spec.template.spec.containers.image)"

    這個指令會顯示容器映像檔名稱,例如 gcr.io/PROJECT_ID/hello-service。

  3. 將容器映像檔從 Container Registry 提取至環境。這個步驟會下載容器映像檔,可能需要幾分鐘才能完成:

    docker pull gcr.io/PROJECT_ID/hello-service

    日後更新容器映像檔時,如果重複使用這個名稱,可以使用相同指令擷取。如果略過這個步驟,當本機電腦上沒有容器映像檔時,下方的 docker run 指令會提取容器映像檔。

  4. 在本機執行,確認問題並非 Knative serving 獨有:

    PORT=8080 && docker run --rm -e PORT=$PORT -p 9000:$PORT \
       gcr.io/PROJECT_ID/hello-service

    上述指令的元素細分如下:

    • 服務會使用 PORT 環境變數,判斷要在容器內監聽的通訊埠。
    • run 指令會啟動容器,預設為 Dockerfile 或父項容器映像檔中定義的進入點指令。
    • --rm 旗標會在退出時刪除容器執行個體。
    • -e 旗標會為環境變數指派值。-e PORT=$PORT 會將 PORT 變數從本機系統傳播到具有相同變數名稱的容器中。
    • -p 旗標會將容器發布為 localhost 上通訊埠 9000 的可用服務。傳送至 localhost:9000 的要求會轉送至通訊埠 8080 上的容器。這表示服務輸出的使用中通訊埠編號,與服務的存取方式不符。
    • 最後一個引數 gcr.io/PROJECT_ID/hello-service 是存放區路徑,指向最新版本的容器映像檔。如果本機沒有,Docker 會嘗試從遠端登錄檔擷取映像檔。

    在瀏覽器中開啟 http://localhost:9000。查看終端機輸出內容,找出與 Google Cloud Observability 相同的錯誤訊息。

    如果問題無法在本機重現,則可能是 Knative serving 環境特有的問題。請參閱 Knative Serving 疑難排解指南,瞭解要調查的特定領域。

    在本例中,錯誤是在本機重現。

現在,您已再次確認錯誤持續發生,且是由服務程式碼 (而非託管平台) 造成,接下來就要仔細檢查程式碼。

在本教學課程中,您可以放心地假設容器內的程式碼與本機系統中的程式碼相同。

Node.js

在記錄顯示的堆疊追蹤中,找出錯誤訊息的來源檔案 index.js,並查看該行號附近的程式碼:
const {NAME} = process.env;
if (!NAME) {
  // Plain error logs do not appear in Stackdriver Error Reporting.
  console.error('Environment validation failed.');
  console.error(new Error('Missing required server parameter'));
  return res.status(500).send('Internal Server Error');
}

Python

在記錄顯示的堆疊追蹤中,找出錯誤訊息的來源檔案 main.py,並查看該行號附近的程式碼:
NAME = os.getenv("NAME")

if not NAME:
    print("Environment validation failed.")
    raise Exception("Missing required service parameter.")

Go

在 main.go 檔案中,找出記錄顯示的堆疊追蹤所指出的行號,並在該行號附近找出錯誤訊息的來源:

name := os.Getenv("NAME")
if name == "" {
	log.Printf("Missing required server parameter")
	// The panic stack trace appears in Cloud Error Reporting.
	panic("Missing required server parameter")
}

Java

在 App.java 檔案中,找出錯誤訊息的來源,位置大約在記錄顯示的堆疊追蹤中標出的行號附近:

String name = System.getenv("NAME");
if (name == null) {
  // Standard error logs do not appear in Stackdriver Error Reporting.
  System.err.println("Environment validation failed.");
  String msg = "Missing required server parameter";
  logger.error(msg, new Exception(msg));
  res.status(500);
  return "Internal Server Error";
}

查看這段程式碼,當未設定 NAME 環境變數時,會採取下列動作:

  • 錯誤會記錄到 Google Cloud Observability
  • 傳送 HTTP 錯誤回應

問題是由缺少變數引起,但根本原因更具體:程式碼變更增加了對環境變數的硬依賴,但未包含對部署指令碼和執行階段需求文件的相關變更。

修正根本原因

現在我們已收集程式碼並找出潛在根本原因,可以採取步驟修正問題。

  • 檢查服務是否可在本機運作,並提供 NAME 環境:

    1. 在本機執行容器,並加入環境變數:

      PORT=8080 && docker run --rm -e PORT=$PORT -p 9000:$PORT \
       -e NAME="Local World!" \
       gcr.io/PROJECT_ID/hello-service
    2. 在瀏覽器中前往 http://localhost:9000

    3. 頁面上會顯示「Hello Local World!」

  • 修改執行中的 Knative serving 服務環境,加入以下變數:

    1. 執行服務更新指令,並加入 --update-env-vars 參數,新增環境變數:

      gcloud run services update hello-service \
        --update-env-vars NAME=Override
      
    2. 請稍候幾秒,Knative serving 會根據先前的修訂版本建立新的修訂版本,並加入新的環境變數。

  • 確認服務已修正:

    1. 在瀏覽器中前往 Knative serving 服務網址。
    2. 頁面上會顯示「Hello Override!」。
    3. 確認 Cloud Logging 中沒有出現非預期的訊息或錯誤。

加快日後疑難排解的速度

在這個正式環境問題範例中,錯誤與作業設定有關。我們已修改程式碼,盡可能降低這個問題日後造成的影響。

  • 改善錯誤記錄,納入更具體的詳細資料。
  • 請讓服務改用安全的預設值,而非傳回錯誤。如果使用預設值會改變正常功能,請使用警告訊息進行監控。

接下來,我們將逐步移除 NAME 環境變數,不再將其視為硬性依附元件。

  1. 移除現有的 NAME 處理程式碼:

    Node.js

    const {NAME} = process.env;
    if (!NAME) {
      // Plain error logs do not appear in Stackdriver Error Reporting.
      console.error('Environment validation failed.');
      console.error(new Error('Missing required server parameter'));
      return res.status(500).send('Internal Server Error');
    }

    Python

    NAME = os.getenv("NAME")
    
    if not NAME:
        print("Environment validation failed.")
        raise Exception("Missing required service parameter.")

    Go

    name := os.Getenv("NAME")
    if name == "" {
    	log.Printf("Missing required server parameter")
    	// The panic stack trace appears in Cloud Error Reporting.
    	panic("Missing required server parameter")
    }

    Java

    String name = System.getenv("NAME");
    if (name == null) {
      // Standard error logs do not appear in Stackdriver Error Reporting.
      System.err.println("Environment validation failed.");
      String msg = "Missing required server parameter";
      logger.error(msg, new Exception(msg));
      res.status(500);
      return "Internal Server Error";
    }

  2. 新增程式碼來設定備用值:

    Node.js

    const NAME = process.env.NAME || 'World';
    if (!process.env.NAME) {
      console.log(
        JSON.stringify({
          severity: 'WARNING',
          message: `NAME not set, default to '${NAME}'`,
        })
      );
    }

    Python

    NAME = os.getenv("NAME")
    
    if not NAME:
        NAME = "World"
        error_message = {
            "severity": "WARNING",
            "message": f"NAME not set, default to {NAME}",
        }
        print(json.dumps(error_message))

    Go

    name := os.Getenv("NAME")
    if name == "" {
    	name = "World"
    	log.Printf("warning: NAME not set, default to %s", name)
    }

    Java

    String name = System.getenv().getOrDefault("NAME", "World");
    if (System.getenv("NAME") == null) {
      logger.warn(String.format("NAME not set, default to %s", name));
    }

  3. 透過受影響的設定案例,重新建構並執行容器,在本機進行測試:

    Node.js

    docker build --tag gcr.io/PROJECT_ID/hello-service .

    Python

    docker build --tag gcr.io/PROJECT_ID/hello-service .

    Go

    docker build --tag gcr.io/PROJECT_ID/hello-service .

    Java

    mvn compile jib:build

    確認 NAME 環境變數仍可運作:

    PORT=8080 && docker run --rm -e $PORT -p 9000:$PORT \
     -e NAME="Robust World" \
     gcr.io/PROJECT_ID/hello-service

    確認服務在沒有 NAME 變數的情況下運作:

    PORT=8080 && docker run --rm -e $PORT -p 9000:$PORT \
     gcr.io/PROJECT_ID/hello-service

    如果服務未傳回結果,請確認在第一個步驟中移除程式碼時,沒有移除額外的程式碼行,例如用於寫入回應的程式碼行。

  4. 請返回「部署程式碼」一節,部署這項變更。

    每次部署服務都會建立新的修訂版本,並在準備就緒時自動開始提供流量。

    如要清除先前設定的環境變數,請執行下列指令:

    gcloud run services update hello-service --clear-env-vars

將預設值的新功能新增至服務的自動化測試涵蓋範圍。

在記錄中尋找其他問題

您可能會在記錄檢視器中看到這項服務的其他問題。舉例來說,不支援的系統呼叫會在記錄中顯示為「容器沙箱限制」。

舉例來說,Node.js 服務有時會產生下列記錄訊息:

Container Sandbox Limitation: Unsupported syscall statx(0xffffff9c,0x3e1ba8e86d88,0x0,0xfff,0x3e1ba8e86970,0x3e1ba8e86a90). Please, refer to https://gvisor.dev/c/linux/amd64/statx for more information.

在這種情況下,不支援這項功能並不會影響 hello-service 範例服務。

清除所用資源

您可以刪除為本教學課程建立的資源,以免產生費用。

刪除教學課程資源

  1. 刪除您在本教學課程中部署的 Knative serving 服務:

    gcloud run services delete SERVICE-NAME

    其中 SERVICE-NAME 是您選擇的服務名稱。

    您也可以透過Google Cloud 控制台刪除 Knative serving 服務:

    前往 Knative serving

  2. 移除您在教學課程設定期間新增的 gcloud 預設設定:

     gcloud config unset run/platform
     gcloud config unset run/cluster
     gcloud config unset run/cluster_location
    
  3. 移除專案設定:

     gcloud config unset project
    
  4. 刪除在本教學課程中建立的其他 Google Cloud 資源:

後續步驟