Using ConfigMaps

Learn how to create a ConfigMap and then configure your Knative serving services and revisions to use that ConfigMap.

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

Creating a ConfigMap

There are several ways to create a ConfigMap, as described in the ConfigMaps page of the GKE documentation. 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 Knative serving service. These instructions use the default namespace.

You can create a ConfigMap from the command line:

kubectl create configmap NAME DATA

Replace:

  • NAME is the name of your ConfigMap object.
  • DATA can be either:

    • The --from-file flag and path for each directory that contains one or more configuration files.
    • The --from-literal flag and key-value pair for each that you want to add to the ConfigMap.

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 Knative serving service either as a volume or as environment variables using the Google Cloud console or the Google Cloud CLI when you deploy a new service or update an existing service and deploy a revision:

Console

  1. Go to Knative serving in the Google Cloud console:

    Go to Knative serving

  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 & Deploy New Revision.

  3. Under Advanced settings, click Variables and Secrets.

    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 Next to continue to the next section.

  6. In the Configure how this service is triggered section, select which connectivity you would like to use to invoke the service.

  7. Click Create to deploy the image to Knative serving and wait for the deployment to finish.

Command line

You can use the Google Cloud CLI to set ConfigMaps for new services or to update an existing services:

  • For existing services, update a ConfigMap by running the gcloud run services update command with one of the following parameters:

    Example:

    gcloud run services update SERVICE --update-config-maps KEY1=VALUE1,KEY2=VALUE2

    Replace:

    • SERVICE with the name of your service.
    • KEY1=VALUE1,KEY2=VALUE2 with a comma separated list of name and value pairs for each ConfigMap. For each KEY you can specify a mount path, or provide an environment variable. Specify a mount path by starting with a forward slash /. All other keys correspond to environment variables. For each VALUE, specify the ConfigMap name. How to specify multiple parameters.
    • Command parameter options

      To specify several sets of key-value pairs, you can specify multiple parameters for readability. Example:
      [...]
      --update-config-maps "KEY=VALUE1" \
      --update-config-maps "KEY=VALUE2" \
      --update-config-maps "KEY=VALUE3"
  • For new services, set the ConfigMap by running the gcloud run deploy command with the --set-config-maps parameter:

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

    Replace:

    • IMAGE_URL with a reference to the container image, for example, gcr.io/myproject/my-image:latest.
    • SERVICE with the name of your service.
    • KEY1=VALUE1,KEY2=VALUE2 with a comma separated list of name and value pairs for each ConfigMap. For each KEY you can specify a mount path, or provide an environment variable. Specify a mount path by starting with a forward slash /. All other keys correspond to environment variables. For each VALUE, specify the ConfigMap name. How to specify multiple parameters.
    • Command parameter options

      To specify several sets of key-value pairs, you can specify multiple parameters for readability. Example:
      [...]
      --update-config-maps "KEY=VALUE1" \
      --update-config-maps "KEY=VALUE2" \
      --update-config-maps "KEY=VALUE3"