Using ConfigMaps in Cloud Run for Anthos on Google Cloud clusters

A common use case for a service is to use ConfigMaps to separate application code from configuration. ConfigMap is similar to Secret except that you use a Secret for sensitive information and you use a ConfigMap to store non-sensitive data such as connection strings, public credentials, hostnames, and URLs, etc. You can learn more about using ConfigMaps in the Google Kubernetes Engine documentation.

When you enable containers to access ConfigMaps, you can choose either of these options:

  • Mount the ConfigMap as a volume, with ConfigMap entries available as files in the mounted volume. This is recommended because it ensures that you get the latest version of the ConfigMap when you are reading it.
  • Pass the ConfigMap using environment variables.

This page describes how to create a ConfigMap and then how to configure Cloud Run for Anthos on Google Cloud to use that ConfigMap.

Creating a ConfigMap

There are several ways to create a ConfigMap, as described in the GKE documentation on ConfigMaps. For your convenience, the following steps show a simple way to create a ConfigMap.

When you create a ConfigMap, make sure you create it in the same namespace as the cluster that is running your Cloud Run service. These instructions use the default namespace.

You can create a ConfigMap from the command line:

kubectl create configmap name data

where:

  • name is the name of your ConfigMap object.
  • data can be:

    • a path to a directory containing one or more configuration files, indicated using the --from-file flag.
    • key-value pairs, each specified using --from-literal flags.

For example:

kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm

The ConfigMap is created and uploaded where it is available to clusters in the default namespace.

Making a ConfigMap available to a service

After you create a ConfigMap, you can make it available to your Cloud Run service either as a volume or as environment variables using the Cloud Console, or the gcloud command line when you create a new service or deploy a new revision.

Console

  1. Go to Cloud Run

  2. Click Create Service if you are configuring a new service you are deploying to. If you are configuring an existing service, click on the service, then click Edit and Deploy New Revision.

  3. Under Advanced Settings, click Variables.

    image

  4. Under Reference a ConfigMap, select the desired ConfigMap from the pulldown menu.

    • In the Reference method pulldown menu, select the way you want to use your ConfigMap, mounted as a volume or exposed as environment variables.
    • If you are using mount as a volume, specify the path, then click Done
    • If you are exposing as environment variables,
      1. Supply the Name of the variable and select the corresponding ConfigMap value from the Key pulldown menu.
      2. Click Add to add another ConfigMap value.
      3. Supply the Name of the variable and select the corresponding ConfigMap value from the Key pulldown menu.
      4. Click Done

  5. Click Create or Deploy.

Command line

To set, update, clear, or remove a ConfigMap for an existing service, use the gcloud run services update command. You can use any of the following flags, as needed:

For example to add or update a ConfigMap:

gcloud run services update SERVICE --update-config-maps KEY1=VALUE1,KEY2=VALUE2
  • Replace SERVICE with the name of your service.
  • Replace KEY1=VALUE1,KEY2=VALUE2 with the comma separated list of desired config-maps key names and values.

You can also set a ConfigMap during deployment using the command:

gcloud run deploy SERVICE --image IMAGE_URL --update-config-maps KEY1=VALUE1,KEY2=VALUE2

Replace IMAGE_URL with the container image URL, for example, gcr.io/myproject/my-image:latest.