首页 > 解决方案 > 谷歌云平台使用 Kubernetes 创建管道并替换相同的容器

问题描述

我正在努力尝试用来自 Google Cloud Platform 的容器注册中的容器替换现有容器。

这是我的 cloudbuild.yaml 文件。

脚步:

  # 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'

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

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

标签: kubernetesgoogle-cloud-platformgoogle-kubernetes-engine

解决方案


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

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

您的问题来自图像的标签没有改变::latest已部署并且您要求部署:latest。没有图像名称更改,没有推出。

为了改变这一点,我建议您使用替换变量,尤其是COMMIT_SHAor SHORT_SHA。您不能在文档中这样做:

仅适用于触发构建

这意味着只有在自动触发构建而不是手动触发时才会填充此变量。

对于手动运行,您必须指定自己的变量,如下所示

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

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

  # 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'

在部署过程中,您应该会看到这一行:

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

如果你没有看到它,这意味着你的推出没有考虑到。


推荐阅读