Setting a container in Kubernetes with cloudbuild.yaml error: unable to find container named "XXX" error

10/15/2019

First time creating a pipeline in Google Cloud Platform.

I have been following their guide, and the last step I want to set the build container into Kubernetes cluster.

This is my yaml file that is failling in the last step.

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 app
  - name: 'docker.io/library/python:3.7'
    id: Test
    entrypoint: /bin/sh
    args:
      - -c
      - 'pip install -r requirements.txt && python -m pytest app/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/scrapper-config', 'scrappercontroller=gcr.io/abiding-robot-255320/scrappercontroller']
    env:
      - 'CLOUDSDK_COMPUTE_ZONE=us-central1-a'
      - 'CLOUDSDK_CONTAINER_CLUSTER=n1scrapping'

I have been using GCP guideline

- name: 'gcr.io/cloud-builders/kubectl'
  args: ['set', 'image', 'deployment/myimage', 'frontend=gcr.io/myproject/myimage']
  env:
  - 'CLOUDSDK_COMPUTE_ZONE=us-east1-b'
  - 'CLOUDSDK_CONTAINER_CLUSTER=node-example-cluster'

But I dont know what do I have to replace in the last argument. frontend=gcr.io/myproject/myimage in my case.

Also my intention is to replace the container that is running on kubernetes, if this help identifying any issues.

Thanks for any help!

-- Patrick Vibild
google-cloud-build
google-cloud-platform
kubernetes

1 Answer

10/16/2019

I'm going to guess from the title you're seeing a message like this in your CloudBuild logs:

+ kubectl set image deployment/scrapper-config scrappercontroller=gcr.io/abiding-robot-255320/scrappercontroller
error: unable to find container named "scrappercontroller"

I dont know what do I have to replace in the last argument. frontend=gcr.io/myproject/myimage in my case.

The meaning of this argument is <container_name>=<image_ref>.
You're setting this value to scrappercontroller=gcr.io/abiding-robot-255320/scrappercontroller.
That means: "set the image for the 'scrappercontroller' container in my Pods to this image from GCR".

You can learn more about this by running kubectl set image --help:

Update existing container image(s) of resources.

 Possible resources include (case insensitive):

  pod (po), replicationcontroller (rc), deployment (deploy), daemonset (ds), replicaset (rs)

Examples:
  # Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox container image to 'busybox'.
  kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1

  # Update all deployments' and rc's nginx container's image to 'nginx:1.9.1'
  kubectl set image deployments,rc nginx=nginx:1.9.1 --all

  # Update image of all containers of daemonset abc to 'nginx:1.9.1'
  kubectl set image daemonset abc *=nginx:1.9.1

  # Print result (in yaml format) of updating nginx container image from local file, without hitting the server
  kubectl set image -f path/to/file.yaml nginx=nginx:1.9.1 --local -o yaml

You're working with a Deployment object. Deployments create Pods using their spec.template.

Pods can have multiple containers, and each one will have a name.
This command will show you your container names for the Pods in your Deployment:

kubectl get --output=wide deploy/scrapper-config

Here's an example of a Deployment that creates Pods with two containers: "myapp" and "cool-sidecar". (See the CONTAINERS column.)

kubectl get --output=wide deploy/myapp
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS           IMAGES        SELECTOR
myapp   1/1     1            0           10m   myapp,cool-sidecar   nginx,nginx   run=myapp

You can use that container name in your final argument:

'my-container-name=gcr.io/abiding-robot-255320/scrappercontroller'

You can also just use a wildcard(*) if your Pods only have a single container each:

'*=gcr.io/abiding-robot-255320/scrappercontroller'

Hopefully that helps

-- stealthybox
Source: StackOverflow