如何在GCP(Cloud Run)上部署Go应用程序?

huangapple go评论71阅读模式
英文:

How to deploy an Go application on GCP (Cloud run)?

问题

我创建了一个简单的 Golang 应用程序,使用 Gin web 框架创建了一个服务器,该服务器接受请求并提供响应。现在,为了部署它,我构建了一个 Docker 容器,然后将其托管在 Google Cloud 平台上的 Cloud Run 服务上。

英文:

Created a simple Golang Application which has a server created via Gin web framework, which accepts a requests and provides responses for it. now to deploy it built a docker container and then host that on google cloud platform services that is cloud run.

答案1

得分: 5

要求 -> Google Cloud SDK,Docker

  • 步骤1 -> 为应用程序创建一个Docker文件,以便您可以从中创建一个镜像,并将其添加到GCP上的容器注册表中,从而可以使用该容器在云端运行部署应用程序。
    dockerfile示例

  • 步骤2 -> 在本地设备上构建和运行容器,以检查是否存在问题。
    在本地设备上构建和运行

  • 步骤3 -> 使用以下命令从终端登录到Google Cloud:

    gcloud auth login

并使用以下命令授权docker配置:

gcloud auth configure-docker
  • 步骤4 -> 给镜像打标签

    docker tag goapp gcr.io/project-name-from-GCP/go-app

步骤5 -> 使用以下命令将您的镜像推送到容器注册表:

docker push gcr.io/project-name-from-GCP/go-app

将镜像推送到Google容器注册表

  • 步骤6 -> 在Google Cloud Run上使用上述标记的镜像创建服务并部署应用程序。

额外信息 - 如果设备是Mac M1,则容器镜像中的可执行文件必须编译为Linux 64位。Cloud Run专门支持Linux x86_64 ABI格式,因此在docker build时使用--platform linux/amd64以防止在部署到Cloud Run时出现问题。

英文:

Requirements -> Google Cloud SDK, Docker

  • Step 1 -> Create a docker file for the application so that you can create an image from it and add to Container registry on GCP, from where one can use that container to deploy the application on cloud run.
    dockerfile example

  • Step 2 -> Build and run the container on local device to check for issues.
    build and run on local device

  • Step 3 -> Login to google cloud from terminal using command

    gcloud auth login

and also authorise docker-configure using command

gcloud auth configure-docker
  • Step 4 -> Tag the image

    docker tag goapp gcr.io/project-name-from-GCP/go-app

Step 5 -> push your image onto container registry using commands

docker push gcr.io/project-name-from-GCP/go-app

pushed image to Google container registry

  • Step 6-> Use the above tagged image on google cloud run to create a service and deploy the application.

extra - if device is Mac M1 , then Executables in the container image must be compiled for Linux 64-bit. Cloud Run specifically supports the Linux x86_64 ABI format , hence use --platform linux/amd64 in docker build to prevent issues while deployment on cloud run

huangapple
  • 本文由 发表于 2022年1月25日 02:03:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/70838390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定