Configuring Redis using a ConfigMap

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

    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 .

    • The example shown on this page works with kubectl 1.14 and above.
    • Understand .

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

    First create a ConfigMap with an empty configuration block:

    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.

    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. volumeMounts:
    20. - mountPath: /redis-master-data
    21. name: data
    22. name: config
    23. volumes:
    24. - name: data
    25. emptyDir: {}
    26. - name: config
    27. configMap:
    28. name: example-redis-config
    29. items:
    30. - key: redis-config
    31. 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
    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:

    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 Configuring Redis using a ConfigMap - 图2

    1. metadata:
    2. name: example-redis-config
    3. data:
    4. redis-config: |
    5. maxmemory 2mb
    6. 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
    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:

    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
    1. 1) "maxmemory-policy"

    Clean up your work by deleting the created resources: