Configuring Redis using a ConfigMap

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

Objectives

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

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

To check the version, enter kubectl version.

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:

  1. cat <<EOF >./example-redis-config.yaml
  2. apiVersion: v1
  3. kind: ConfigMap
  4. metadata:
  5. name: example-redis-config
  6. data:
  7. redis-config: ""
  8. EOF

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

  1. kubectl apply -f example-redis-config.yaml
  2. 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].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.

pods/config/redis-pod.yaml

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: redis
  5. spec:
  6. containers:
  7. - name: redis
  8. image: redis:5.0.4
  9. command:
  10. - redis-server
  11. - "/redis-master/redis.conf"
  12. env:
  13. - name: MASTER
  14. value: "true"
  15. ports:
  16. - containerPort: 6379
  17. resources:
  18. limits:
  19. cpu: "0.1"
  20. volumeMounts:
  21. - mountPath: /redis-master-data
  22. name: data
  23. - mountPath: /redis-master
  24. name: config
  25. volumes:
  26. - name: data
  27. emptyDir: {}
  28. - name: config
  29. configMap:
  30. name: example-redis-config
  31. items:
  32. - key: redis-config
  33. path: redis.conf

Examine the created objects:

  1. kubectl get pod/redis configmap/example-redis-config

You should see the following output:

  1. NAME READY STATUS RESTARTS AGE
  2. pod/redis 1/1 Running 0 8s
  3. NAME DATA AGE
  4. configmap/example-redis-config 1 14s

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

  1. kubectl describe configmap/example-redis-config

You should see an empty redis-config key:

  1. Name: example-redis-config
  2. Namespace: default
  3. Labels: <none>
  4. Annotations: <none>
  5. Data
  6. ====
  7. redis-config:

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

  1. kubectl exec -it redis -- redis-cli

Check maxmemory:

  1. 127.0.0.1:6379> CONFIG GET maxmemory

It should show the default value of 0:

  1. 1) "maxmemory"
  2. 2) "0"

Similarly, check maxmemory-policy:

  1. 127.0.0.1:6379> CONFIG GET maxmemory-policy

Which should also yield its default value of noeviction:

  1. 1) "maxmemory-policy"
  2. 2) "noeviction"

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

pods/config/example-redis-config.yaml

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: example-redis-config
  5. data:
  6. redis-config: |
  7. maxmemory 2mb
  8. maxmemory-policy allkeys-lru

Apply the updated ConfigMap:

  1. kubectl apply -f example-redis-config.yaml

Confirm that the ConfigMap was updated:

  1. kubectl describe configmap/example-redis-config

You should see the configuration values we just added:

  1. Name: example-redis-config
  2. Namespace: default
  3. Labels: <none>
  4. Annotations: <none>
  5. Data
  6. ====
  7. redis-config:
  8. ----
  9. maxmemory 2mb
  10. maxmemory-policy allkeys-lru

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

  1. kubectl exec -it redis -- redis-cli

Check maxmemory:

  1. 127.0.0.1:6379> CONFIG GET maxmemory

It remains at the default value of 0:

  1. 1) "maxmemory"
  2. 2) "0"

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

  1. 127.0.0.1:6379> CONFIG GET maxmemory-policy

Returns:

  1. 1) "maxmemory-policy"
  2. 2) "noeviction"

The configuration values have not changed because the Pod needs to be restarted to grab updated values from associated ConfigMaps. Let’s delete and recreate the Pod:

  1. kubectl delete pod redis
  2. kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yaml

Now re-check the configuration values one last time:

  1. kubectl exec -it redis -- redis-cli

Check maxmemory:

  1. 127.0.0.1:6379> CONFIG GET maxmemory

It should now return the updated value of 2097152:

  1. 1) "maxmemory"
  2. 2) "2097152"

Similarly, maxmemory-policy has also been updated:

  1. 127.0.0.1:6379> CONFIG GET maxmemory-policy

It now reflects the desired value of allkeys-lru:

  1. 1) "maxmemory-policy"
  2. 2) "allkeys-lru"

Clean up your work by deleting the created resources:

  1. kubectl delete pod/redis configmap/example-redis-config

What’s next