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 Containers Using a ConfigMap task.

Objectives

  • Create a kustomization.yaml file containing:
    • a ConfigMap generator
    • a Pod resource config using the ConfigMap
  • Apply the directory by running kubectl apply -k ./
  • 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. 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

You can follow the steps below to configure a Redis cache using data stored in a ConfigMap.

First create a kustomization.yaml containing a ConfigMap from the redis-config file:

pods/config/redis-config Configuring Redis using a ConfigMap - 图1

  1. maxmemory 2mb
  2. maxmemory-policy allkeys-lru
  1. curl -OL https://k8s.io/examples/pods/config/redis-config
  2. cat <<EOF >./kustomization.yaml
  3. configMapGenerator:
  4. - name: example-redis-config
  5. files:
  6. - redis-config
  7. EOF

Add the pod resource config to the kustomization.yaml:

pods/config/redis-pod.yaml Configuring Redis using a ConfigMap - 图2

  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
  1. curl -OL https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/config/redis-pod.yaml
  2. cat <<EOF >>./kustomization.yaml
  3. resources:
  4. - redis-pod.yaml
  5. EOF

Apply the kustomization directory to create both the ConfigMap and Pod objects:

  1. kubectl apply -k .

Examine the created objects by

  1. > kubectl get -k .
  2. NAME DATA AGE
  3. configmap/example-redis-config-dgh9dg555m 1 52s
  4. NAME READY STATUS RESTARTS AGE
  5. pod/redis 1/1 Running 0 52s

In the example, the config volume is mounted at /redis-master. It uses path to add the redis-config key to a file named redis.conf. The file path for the redis config, therefore, is /redis-master/redis.conf. This is where the image will look for the config file for the redis master.

Use kubectl exec to enter the pod and run the redis-cli tool to verify that the configuration was correctly applied:

  1. kubectl exec -it redis -- redis-cli
  2. 127.0.0.1:6379> CONFIG GET maxmemory
  3. 1) "maxmemory"
  4. 2) "2097152"
  5. 127.0.0.1:6379> CONFIG GET maxmemory-policy
  6. 1) "maxmemory-policy"
  7. 2) "allkeys-lru"

Delete the created pod:

  1. kubectl delete pod redis

What’s next