Google cloud platform creating a pipeline with Kubernetes and replacing the same container

10/23/2019

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

This is my cloudbuild.yaml file.

steps:

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

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.

-- Patrick Vibild
google-cloud-platform
google-kubernetes-engine
kubernetes

1 Answer

10/24/2019

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

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.

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.

-- guillaume blaquiere
Source: StackOverflow