登录
首页 >  Golang >  Go问答

容器在Google App Engine Flex中的部署挑战

来源:stackoverflow

时间:2024-03-10 16:45:25 154浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《容器在Google App Engine Flex中的部署挑战》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

问题内容

我正在尝试将我的 go 1.14 微服务应用程序部署到 google 的灵活环境中。我读到查找 goroot 时存在问题,以及它如何无法获取正确的依赖项。

我想使用灵活的环境,因为我需要进行端口转发。由于我的域名用于运行实际应用程序,因此我希望端口 8081 来运行我的微服务。

我按照此链接中的说明进行操作:

https://blog.cubieserver.de/2019/go-modules-with-app-engine-flexible/

我尝试了选项 3。 这是我的 gitlab-ci.yaml 配置文件。

# .gitlab-ci.yaml
stages:
  - build
  - deploy

build_server:
  stage: build
  image: golang:1.14
  script:
    - go mod vendor
    - go install farmcycle.us/user/farmcycle
  artifacts:
    paths:
      - vendor/

deploy_app_engine:
  stage: deploy 
  image: google/cloud-sdk:270.0.0
  script:
    - echo $service_account > /tmp/$ci_pipeline_id.json 
    - gcloud auth activate-service-account --key-file /tmp/$ci_pipeline_id.json
    - gcloud --quiet --project $project_id app deploy app.yaml
    
  after_script:
  - rm /tmp/$ci_pipeline_id.json

这是我的 app.yaml 配置文件

runtime: go
env: flex

network:
  forwarded_ports:
    - 8081/tcp

当我使用 git ci 管道部署它时。构建阶段通过,但部署阶段失败。

Running with gitlab-runner 13.4.1 (e95f89a0)
  on docker-auto-scale 72989761
Preparing the "docker+machine" executor
Preparing environment
00:03
Getting source from Git repository
00:04
Downloading artifacts
00:02
Executing "step_script" stage of the job script
00:03
$ echo $SERVICE_ACCOUNT > /tmp/$CI_PIPELINE_ID.json
$ gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
Activated service account credentials for: [[email protected]]
$ gcloud --quiet --project $PROJECT_ID app deploy app.yaml
ERROR: (gcloud.app.deploy) Staging command [/usr/lib/google-cloud-sdk/platform/google_appengine/go-app-stager /builds/JLiu1272/farmcycle-backend/app.yaml /builds/JLiu1272/farmcycle-backend /tmp/tmprH6xQd/tmpSIeACq] failed with return code [1].
------------------------------------ STDOUT ------------------------------------
------------------------------------ STDERR ------------------------------------
2020/10/10 20:48:27 staging for go1.11
2020/10/10 20:48:27 Staging Flex app: failed analyzing /builds/JLiu1272/farmcycle-backend: cannot find package "farmcycle.us/user/farmcycle/api" in any of:
    ($GOROOT not set)
    /root/go/src/farmcycle.us/user/farmcycle/api (from $GOPATH)
GOPATH: /root/go
--------------------------------------------------------------------------------
Running after_script
00:01
Running after script...
$ rm /tmp/$CI_PIPELINE_ID.json
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 1

这是错误。老实说,我不太确定这个错误是什么以及如何修复它。


解决方案


令人惊讶的是,即使使用最新的 go 运行时 runtime: go1.15,go 模块似乎也无法使用。请参阅golang-docker

但是,无论 runtime 如何,flex 都会将您的应用程序构建到容器中,因此,在这种情况下,最好使用自定义运行时并构建您自己的 dockerfile?

runtime: custom
env: flex

然后你就可以使用例如go 1.15 和 go 模块(无供应商)以及您想要的任何其他内容。对于使用模块的简单 main.go,例如:

FROM golang:1.15 as build

ARG PROJECT="flex"
WORKDIR /${PROJECT}

COPY go.mod .
RUN go mod download

COPY main.go .

RUN GOOS=linux \
    go build -a -installsuffix cgo \
    -o /bin/server \
    .

FROM gcr.io/distroless/base-debian10

COPY --from=build /bin/server /

USER 999

ENV PORT=8080
EXPOSE ${PORT}

ENTRYPOINT ["/server"]

google 最近发布的 support for buildpacks 应该可以实现这一点,但我还没有尝试过。

今天关于《容器在Google App Engine Flex中的部署挑战》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>