Search code examples
dockerkubernetesgoogle-cloud-rungoogle-cloud-build

How to use an image deployed on cloud run for kubernetes deployment


I want to deploy an image from cloud run to kubernetes cluster. I have successfully built the docker image in cloudbuild.yaml file. Now the challenge is how to use the image in deploy.yaml

The task fails with errors indicating the the variables were not substituted as expected.

Here are the config excerpts:

cloudbuild.yaml

//push image to container registry
    ....
    .......

  - id: "deploy container image to GKE"
    name: "gcr.io/cloud-builders/gke-deploy"
    args: [
    "--cluster", "${_CLUSTER_NAME}",
    "run",
    "--filename", "deployment.yaml",
    "--image", "us-west1-docker.pkg.dev/$PROJECT_ID/abc/abc-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA",
    "--location", "${_COMPUTE_ZONE}"
    ]

deployment.yaml

    ....
    ........
    spec:
      containers:
        - name: idr-server-k8ts-test
          image: us-west1-docker.pkg.dev/$PROJECT_ID/abc/abc-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA
          ports:
            - containerPort: 8080

Vars defined in build trigger

_CLUSTER_NAME
_DEPLOYMENT_TYPE
_COMPUTE_ZONE

I get the error:

Error: failed to prepare deployment: failed to update container of objects: failed to parse reference from image "us-west1-docker.pkg.dev/$PROJECT_ID/abc/abc-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA": could not parse reference: us-west1-docker.pkg.dev/$PROJECT_ID/abc/abc-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA


Solution

  • Kubernetes doesn't understand any variables written in manifest files. Your image's name (us-west1-docker.pkg.dev/$PROJECT_ID/abc/abc-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA) contains environment variables. Plain manifests require constant/hardcoded values.

    If you want to populate your manifests dynamically through environment variables, create a helm chart and apply it in the cluster.