⎈ k8s knowledge compiler

Configuring Redis using a ConfigMap [page]deterministic

tutorials

This page provides a real world example of how to configure Redis using a ConfigMap and builds upon the [Configure a Pod to Use a ConfigMap](/docs/tasks/configure-pod-container/configure-pod-configmap/) task.

##

* Create a ConfigMap with Redis configuration values * Create a Redis Pod that mounts and uses the created ConfigMap * Verify that the configuration was correctly applied.

##

* The example shown on this page works with `kubectl` 1.14 and above. * Understand [Configure a Pod to Use a ConfigMap](/docs/tasks/configure-pod-container/configure-pod-configmap/).

## Real World Example: Configuring Redis using a ConfigMap

Follow the steps below to configure a Redis cache using data stored in a ConfigMap.

First create a ConfigMap with an empty configuration block:

```shell cat <<EOF >./example-redis-config.yaml apiVersion: v1 kind: ConfigMap metadata: name: example-redis-config data: redis-config: "" EOF ```

Apply the ConfigMap created above, along with a Redis pod manifest:

```shell kubectl apply -f example-redis-config.yaml kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yaml ```

Examine the contents of the Redis pod manifest and note the following:

* A volume named `config` is created by `spec.volumes[1]` * The `key` and `path` under `spec.volumes[1].configMap.items[0]` exposes the `redis-config` key from the `example-redis-config` ConfigMap as a file named `redis.conf` on the `config` volume. * The `config` volume is then mounted at `/redis-master` by `spec.containers[0].volumeMounts[1]`.

This has the net effect of exposing the data in `data.redis-config` from the `example-redis-config` ConfigMap above as `/redis-master/redis.conf` inside the Pod.

Examine the created objects:

```shell kubectl get pod/redis configmap/example-redis-config ```

You should see the following output:

``` NAME READY STATUS RESTARTS AGE pod/redis 1/1 Running 0 8s

NAME DATA AGE configmap/example-redis-config 1 14s ```

Recall that we left `redis-config` key in the `example-redis-config` ConfigMap blank:

```shell kubectl describe configmap/example-redis-config ```

You should see an empty `redis-config` key:

```shell Name: example-redis-config Namespace: default Labels: <none> Annotations: <none>

Data ==== redis-config: ```

Use `kubectl exec` to enter the pod and run the `redis-cli` tool to check the current configuration:

```shell kubectl exec -it pod/redis -- redis-cli ```

Check `maxmemory`:

```shell 127.0.0.1:6379> CONFIG GET maxmemory ```

It should show the default value of 0:

```shell 1) "maxmemory" 2) "0" ```

Similarly, check `maxmemory-policy`:

```shell 127.0.0.1:6379> CONFIG GET maxmemory-policy ```

Which should also yield its default value of `noeviction`:

```shell 1) "maxmemory-policy" 2) "noeviction" ```

Now let's add some configuration values to the `example-redis-config` ConfigMap:

Apply the updated ConfigMap:

```shell kubectl apply -f example-redis-config.yaml ```

Confirm that the ConfigMap was updated:

```shell kubectl describe configmap/example-redis-config ```

You should see the configuration values we just added:

```shell Name: example-redis-config Namespace: default Labels: <none> Annotations: <none>

Data ==== redis-config: ---- maxmemory 2mb maxmemory-policy allkeys-lru ```

Check the Redis Pod again using `redis-cli` via `kubectl exec` to see if the configuration was applied:

```shell kubectl exec -it pod/redis -- redis-cli ```

Check `maxmemory`:

```shell 127.0.0.1:6379> CONFIG GET maxmemory ```

It remains at the default value of 0:

```shell 1) "maxmemory" 2) "0" ```

Similarly, `maxmemory-policy` remains at the `noeviction` default setting:

```shell 127.0.0.1:6379> CONFIG GET maxmemory-policy ```

Returns:

```shell 1) "maxmemory-policy" 2) "noeviction" ```

The configuration values hav …(trimmed)

Sources

tutorials/configuration/configure-redis-using-configmap.md · docConfiguring Redis using a ConfigMap

Related (5)

part_of {{% heading "objectives" %}}describes conf=1
part_of {{% heading "prerequisites" %}}describes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
api_for ConfigMapdocuments API object conf=1

← all Docs