Google云平台使用Kubernetes创建管道并替换相同的容器 [英] Google cloud platform creating a pipeline with Kubernetes and replacing the same container

查看:90
本文介绍了Google云平台使用Kubernetes创建管道并替换相同的容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力用Google Cloud Platform的容器注册中的容器替换现有容器.

I am struggling trying to replace an existing container with a container from my container-register from Google Cloud Platform.

这是我的cloudbuild.yaml文件.

This is my cloudbuild.yaml file.

步骤:

  # This steps clone the repository into GCP
  - name: gcr.io/cloud-builders/git
    args: ['clone', 'https:///user/:password@github.com/PatrickVibild/scrappercontroller']

  # This step runs the unit tests on the src
  - name: 'docker.io/library/python:3.7'
    id: Test
    entrypoint: /bin/sh
    args:
      - -c
      - 'pip install -r requirements.txt && python -m pytest src/tests/**'

  #This step creates a container and leave it on CloudBuilds repository.
  - name: 'gcr.io/cloud-builders/docker'
    args: ['build', '-t', 'gcr.io/abiding-robot-255320/scrappercontroller', '.']

  #Adds the container to Google container registry as an artefact
  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/abiding-robot-255320/scrappercontroller']

  #Uses the container and replaces the existing one in Kubernetes
  - name: 'gcr.io/cloud-builders/kubectl'
    args: ['set', 'image', 'deployment/scrappercontroller', 'scrappercontroller-sha256=gcr.io/abiding-robot-255320/scrappercontroller:latest']
    env:
      - 'CLOUDSDK_COMPUTE_ZONE=us-central1-a'
      - 'CLOUDSDK_CONTAINER_CLUSTER=scrapper-admin'

我在构建项目时没有问题,并且所有步骤都变绿了,在最后一步中可能会丢失,但是我找不到用新版本的代码替换群集中的容器的方法.

I have no issues building my project and I get green on all the steps, I might be missing in the last step but I cant find a way to replace my container in my cluster with a newer version of my code.

我可以使用GUI并在容器注册表中选择一个容器,从而在现有集群中手动创建新的工作负载,但是从那里用我的新版本从云端替换该工作负载容器的步骤失败了.

I can create a new workload inside my existing cluster manually using the GUI and selecting a container from my container registry, but from there the step to replace that workload container with my new version from the clouds fails.

推荐答案

这是一个常见的陷阱.根据文档:

It's a common pitfall. According with the documentation:

注意:仅当更改部署的Pod模板(即.spec.template)(例如,模板的标签或容器图像已更新)时,才会触发部署的推出.其他更新(例如扩展Deployment)不会触发部署.

Note: A Deployment’s rollout is triggered if and only if the Deployment’s Pod template (that is, .spec.template) is changed, for example if the labels or container images of the template are updated. Other updates, such as scaling the Deployment, do not trigger a rollout.

您的问题来自映像的标记,该问题没有改变::latest已部署,并且您要求部署:latest.图像名称没有更改,没有展示.

Your issue come from the tag of your image doesn't change: the :latest is deployed and you ask for deploying :latest. No image name change, no rollout.

要更改此设置,我建议您使用

For changing this, I propose you to use substitution variables, especially COMMIT_SHA or SHORT_SHA. You can not this in the documentation:

仅适用于触发的版本

only available for triggered builds

这意味着仅在自动触发构建而不是手动触发构建时填充此变量.

This means that this variable is only populated when the build is automatically triggered and not manually.

对于手动运行,您必须指定自己的变量,像这样

For manual run, you have to specify your own variable, like this

gcloud builds submit --substitutions=COMMIT_SHA=<what you want>

并像这样更新您的构建脚本:

And update your build script like this:

  # This steps clone the repository into GCP
  - name: gcr.io/cloud-builders/git
    args: ['clone', 'https:///user/:password@github.com/PatrickVibild/scrappercontroller']

  # This step runs the unit tests on the src
  - name: 'docker.io/library/python:3.7'
    id: Test
    entrypoint: /bin/sh
    args:
      - -c
      - 'pip install -r requirements.txt && python -m pytest src/tests/**'

  #This step creates a container and leave it on CloudBuilds repository.
  - name: 'gcr.io/cloud-builders/docker'
    args: ['build', '-t', 'gcr.io/abiding-robot-255320/scrappercontroller:$COMMIT_SHA', '.']

  #Adds the container to Google container registry as an artefact
  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/abiding-robot-255320/scrappercontroller:$COMMIT_SHA']

  #Uses the container and replaces the existing one in Kubernetes
  - name: 'gcr.io/cloud-builders/kubectl'
    args: ['set', 'image', 'deployment/scrappercontroller', 'scrappercontroller-sha256=gcr.io/abiding-robot-255320/scrappercontroller:COMMIT_SHA']
    env:
      - 'CLOUDSDK_COMPUTE_ZONE=us-central1-a'
      - 'CLOUDSDK_CONTAINER_CLUSTER=scrapper-admin'

在部署过程中,您应该看到以下行:

And during the deployment, you should see this line:

Step #2: Running: kubectl set image deployment.apps/test-deploy go111=gcr.io/<projectID>/postip:<what you want>
Step #2: deployment.apps/test-deploy image updated

如果您没有看到它,则表示您的部署未考虑在内.

If you don't see it, this mean that your rollout has not take into account.

这篇关于Google云平台使用Kubernetes创建管道并替换相同的容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆