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](https://docs.cloud.google.com/kubernetes-engine/enterprise/knative-serving/docs/configuring/using-secrets.md.txt) 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](https://docs.cloud.google.com/kubernetes-engine/docs/concepts/configmap).

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](https://docs.cloud.google.com/kubernetes-engine/enterprise/knative-serving/docs/configuring/environment-variables.md.txt).

## Creating a ConfigMap

There are several ways to create a ConfigMap, as described in the
[ConfigMaps](https://docs.cloud.google.com/kubernetes-engine/docs/concepts/configmap) 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:

```bash
kubectl create configmap NAME DATA
```

Replace:

- <var translate="no">NAME</var> is the name of your ConfigMap object.
- <var translate="no">DATA</var> 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:

```bash
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](https://docs.cloud.google.com/kubernetes-engine/enterprise/knative-serving/docs/deploying.md.txt#service) or update an
existing service and deploy a [revision](https://docs.cloud.google.com/kubernetes-engine/enterprise/knative-serving/docs/deploying.md.txt#revision):

### Console


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

   [Go to Knative serving](https://console.cloud.google.com/kubernetes/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 \& Deploy New Revision**.

3. Under *Advanced settings* , click **Variables and Secrets**.

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

   <br />

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:

<br />

- For existing services, update a ConfigMap by running the
  [`gcloud run services update`](https://docs.cloud.google.com/sdk/gcloud/reference/run/services/update.md.txt)
  command with one of the following parameters:

  - [`--set-config-maps`](https://docs.cloud.google.com/sdk/gcloud/reference/run/services/update.md.txt#--set-config-maps)
  - [`--update-config-maps`](https://docs.cloud.google.com/sdk/gcloud/reference/run/services/update.md.txt#--update-config-maps)
  - [`--clear-config-maps`](https://docs.cloud.google.com/sdk/gcloud/reference/run/services/update.md.txt#--clear-config-maps)
  - [`--remove-config-maps`](https://docs.cloud.google.com/sdk/gcloud/reference/run/services/update.md.txt#--remove-config-maps)

  Example:

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

  Replace:
  - <var translate="no">SERVICE</var> with the name of your service.
  - <var translate="no">KEY1=VALUE1,KEY2=VALUE2</var> with a comma separated list of name and value pairs for each ConfigMap. For each <var translate="no">KEY</var> 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 <var translate="no">VALUE</var>, specify the ConfigMap name. [How to specify multiple parameters](https://docs.cloud.google.com/kubernetes-engine/enterprise/knative-serving/docs/configuring/using-configmaps.md.txt#command-line).

  ### Command parameter options

  To specify several sets of key-value pairs, you can specify multiple parameters for readability. Example:

  ```bash
  [...]
  --update-config-maps "KEY=VALUE1" \
  --update-config-maps "KEY=VALUE2" \
  --update-config-maps "KEY=VALUE3"
  ```
  <button class="devsite-dialog-close">OK</button>
- For new services, set the ConfigMap by running the
  `gcloud run deploy` command with the
  [`--set-config-maps`](https://docs.cloud.google.com/sdk/gcloud/reference/run/deploy.md.txt#--set-config-maps)
  parameter:

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

  Replace:
  - <var translate="no">IMAGE_URL</var> with a reference to the container image, for example, `gcr.io/cloudrun/hello`.
  - <var translate="no">SERVICE</var> with the name of your service.
  - <var translate="no">KEY1=VALUE1,KEY2=VALUE2</var> with a comma separated list of name and value pairs for each ConfigMap. For each <var translate="no">KEY</var> 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 <var translate="no">VALUE</var>, specify the ConfigMap name. [How to specify multiple parameters](https://docs.cloud.google.com/kubernetes-engine/enterprise/knative-serving/docs/configuring/using-configmaps.md.txt#command-line).

  ### Command parameter options

  To specify several sets of key-value pairs, you can specify multiple parameters for readability. Example:

  ```bash
  [...]
  --update-config-maps "KEY=VALUE1" \
  --update-config-maps "KEY=VALUE2" \
  --update-config-maps "KEY=VALUE3"
  ```
  <button class="devsite-dialog-close">OK</button>