將 Go 服務部署到 GCP Cloud Run 涉及多個步驟,包括設定 Dockerfile 和設定環境變數。
本指南將引導您完成整個過程。
設定您的 GCP 項目
如果尚未建立帳戶,請先前往 GCP 建立帳戶。
- 建立一個 GCP 專案。
前往 GCP 控制台並建立一個新專案。
記下部署的項目 ID。
- 啟用所需的 API。
- 啟用 Cloud Run API 和 Container Registry API。
- 安裝 Google Cloud SDK
- 使用 gcloud init 初始化您的儲存庫。
創建您的 Go 服務
確保您的 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
部署到您的 GCP Cloud Run
執行部署腳本
env-variables.sh
常見問題和故障排除
- 權限問題:確保 Cloud Run Service Agent 具有讀取映像的權限。
- 環境變數:驗證所有必要的環境變數是否設定正確。
- 連接埠配置:確保 PORT 環境變數設定正確。
根據需要完成所有設定後,您將看到正在建立映像並將其推送到您的 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中文網其他相關文章!

OpenSSL,作為廣泛應用於安全通信的開源庫,提供了加密算法、密鑰和證書管理等功能。然而,其歷史版本中存在一些已知安全漏洞,其中一些危害極大。本文將重點介紹Debian系統中OpenSSL的常見漏洞及應對措施。 DebianOpenSSL已知漏洞:OpenSSL曾出現過多個嚴重漏洞,例如:心臟出血漏洞(CVE-2014-0160):該漏洞影響OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻擊者可利用此漏洞未經授權讀取服務器上的敏感信息,包括加密密鑰等。

本文演示了創建模擬和存根進行單元測試。 它強調使用接口,提供模擬實現的示例,並討論最佳實踐,例如保持模擬集中並使用斷言庫。 文章

本文探討了GO的仿製藥自定義類型約束。 它詳細介紹了界面如何定義通用功能的最低類型要求,從而改善了類型的安全性和代碼可重複使用性。 本文還討論了局限性和最佳實踐

本文討論了GO的反思軟件包,用於運行時操作代碼,對序列化,通用編程等有益。它警告性能成本,例如較慢的執行和更高的內存使用,建議明智的使用和最佳

本文使用跟踪工具探討了GO應用程序執行流。 它討論了手冊和自動儀器技術,比較諸如Jaeger,Zipkin和Opentelemetry之類的工具,並突出顯示有效的數據可視化

本文討論了GO中使用表驅動的測試,該方法使用測試用例表來測試具有多個輸入和結果的功能。它突出了諸如提高的可讀性,降低重複,可伸縮性,一致性和A


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 Linux新版
SublimeText3 Linux最新版

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),