將 Go 服務部署到 GCP Cloud Run 涉及多個步驟,包括設定 Dockerfile 和設定環境變數。
本指南將引導您完成整個過程。
如果尚未建立帳戶,請先前往 GCP 建立帳戶。
前往 GCP 控制台並建立一個新專案。
記下部署的項目 ID。
確保您的 Go 應用程式可以在本地運行並設定 Dockerfile。
cmd/main.go
// cmd/main.go func main() { flag.Parse() a := app.Application{} if err := a.LoadConfigurations(); err != nil { log.Fatalf("Failed to load configurations: %v", err) } if err := runtime.Start(&a); err != nil { log.Fatalf("Failed to start the application: %v", err) } }
運行時/base.go
func Start(a *app.Application) error { router := gin.New() router.Use(cors.New(md.CORSMiddleware())) api.SetCache(router, a.RedisClient) api.SetRoutes(router, a.FireClient, a.FireAuth, a.RedisClient) err := router.Run(":" + a.ListenPort) log.Printf("Starting server on port: %s", a.ListenPort) if err != nil { return err } return nil }
建立 Dockerfile
# Use the official Go image as the base image FROM golang:1.18 WORKDIR /app # Copy the Go module files COPY go.mod go.sum ./ RUN go mod download # Copy the rest of the application code COPY . . RUN go build -o main ./cmd/main.go CMD ["./main"]
設定環境變數
使用 shell 腳本自動設定 GCP 的環境變數
作為env-variables.sh.
// env-variables.sh #!/bin/bash # Environment variables export PROJECT_ID=recepies-6e7c0 export REGION=europe-west1 export REDIS_URL="rediss://default:AVrvA....-lemur-23279.u....:6379" export FIREBASE_ACCOUNT_KEY="/app/config/account_key.json" export CLIENT_URL="https://.....vercel.app/"
部署腳本為deploy-with-yaml.sh.
#!/bin/bash source env-variables.sh #Comment if correctly deployed docker build -t gcr.io/$PROJECT_ID/recipe-server:latest . docker push gcr.io/$PROJECT_ID/recipe-server:latest #Uncomment if json needs to be added to GCP # gcloud secrets create firebase-account-key --data-file=/mnt/c/own_dev/RecipesApp/server/config/account_key.json --project=recepies-6e7c0 #Add permission IAM gcloud projects add-iam-policy-binding recepies-6e7c0 \ --member="serviceAccount:service-988443547488@serverless-robot-prod.iam.gserviceaccount.com" \ --role="roles/artifactregistry.reader" gcloud run deploy recipe-service \ --image gcr.io/$PROJECT_ID/recipe-server:latest \ --region $REGION \ --platform managed \ --set-env-vars REDIS_URL=$REDIS_URL,CLIENT_URL=$CLIENT_URL,FIREBASE_ACCOUNT_KEY=$FIREBASE_ACCOUNT_KEY
執行部署腳本
env-variables.sh
根據需要完成所有設定後,您將看到正在建立映像並將其推送到您的 GCP 專案 Artifact Registry。最後我得到了這個。
a9099c3159f5: Layer already exists latest: digest: sha256:8c98063cd5b383df0b444c5747bb729ffd17014d42b049526b8760a4b09e5df1 size: 2846 Deploying container to Cloud Run service [recipe-service] in project [recepies-6e7c0] region [europe-west1] ✓ Deploying... Done. ✓ Creating Revision... ✓ Routing traffic... Done. Service [recipe-service] revision [recipe-service-00024-5mh] has been deployed and is serving 100 percent of traffic. Service URL: https://recipe-service-819621241045.europe-west1.run.app
有一個我多次遇到的標準錯誤?
Deploying container to Cloud Run service [recipe-service] in project [recepies-6e7c0] region [europe-west1] X Deploying… - Creating Revision… . Routing traffic… Deployment failed ERROR: (gcloud.run.deploy) Revision 'recipe-service-00005-b6h' is not ready and cannot serve traffic. Google Cloud Run Service Agent service-819621241045@serverless-robot-prod.iam.gserviceaccount.com must have permission to read the image, gcr.io/loyal-venture-436807-p7/recipe-server:latest. Ensure that the provided container image URL is correct and that the above account has permission to access the image. If you just enabled the Cloud Run API, the permissions might take a few minutes to propagate. Note that the image is from project [loyal-venture-436807-p7], which is not the same as this project [recepies-6e7c0]. Permission must be granted to the Google Cloud Run Service Agent service-819621241045@serverless-robot-prod.iam.gserviceaccount.com from this project. See https://cloud.google.com/run/docs/deploying#other-projects
通常它指出無法設定 PORT=8080,但主要問題是其他問題,例如未設定 env 變量,或在我的情況下,firebase account_key.json 的部署設定不正確。
一切設定完畢後,您可以測試連線並執行要求。
我的前端部署在 Vercel 中,您可以在下面看到我的 Cloud Run 日誌
透過一些關鍵配置和自動化腳本可以簡化將 Go 服務部署到 GCP Cloud Run 的過程。
雖然可能會出現一些常見錯誤,例如權限問題或不正確的環境變量,但了解如何透過 Cloud Run 日誌排查這些錯誤可確保部署順利。
你可以在這裡找到我的倉庫。
以上是如何將 Go 服務部署到 GCP Cloud Run的詳細內容。更多資訊請關注PHP中文網其他相關文章!