Configuring Redis using a ConfigMap
Kubernetes Docs · نسخهٔ f284a9e7c87f · CC BY 4.0 برای مستندات و مجوزهای همراه
<!-- overview -->
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.
{{% heading "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.
{{% heading "prerequisites" %}}
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
- The example shown on this page works with
kubectl1.14 and above. - Understand Configure a Pod to Use a ConfigMap.
<!-- lessoncontent -->
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:
cat <<EOF >./example-redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: example-redis-config
data:
redis-config: ""
EOFApply the ConfigMap created above, along with a Redis pod manifest:
kubectl apply -f example-redis-config.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yamlExamine the contents of the Redis pod manifest and note the following:
- A volume named
configis created byspec.volumes[1] - The
keyandpathunderspec.volumes[1].configMap.items[0]exposes theredis-configkey from the
example-redis-config ConfigMap as a file named redis.conf on the config volume.
- The
configvolume is then mounted at/redis-masterbyspec.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.
{{% code_sample file="pods/config/redis-pod.yaml" %}}
Examine the created objects:
kubectl get pod/redis configmap/example-redis-configYou 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 14sRecall that we left redis-config key in the example-redis-config ConfigMap blank:
kubectl describe configmap/example-redis-configYou should see an empty redis-config key:
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:
kubectl exec -it pod/redis -- redis-cliCheck maxmemory:
127.0.0.1:6379> CONFIG GET maxmemoryIt should show the default value of 0:
1) "maxmemory"
2) "0"Similarly, check maxmemory-policy:
127.0.0.1:6379> CONFIG GET maxmemory-policyWhich should also yield its default value of noeviction:
1) "maxmemory-policy"
2) "noeviction"Now let's add some configuration values to the example-redis-config ConfigMap:
{{% code_sample file="pods/config/example-redis-config.yaml" %}}
Apply the updated ConfigMap:
kubectl apply -f example-redis-config.yamlConfirm that the ConfigMap was updated:
kubectl describe configmap/example-redis-configYou should see the configuration values we just added:
Name: example-redis-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
redis-config:
----
maxmemory 2mb
maxmemory-policy allkeys-lruCheck the Redis Pod again using redis-cli via kubectl exec to see if the configuration was applied:
kubectl exec -it pod/redis -- redis-cliCheck maxmemory:
127.0.0.1:6379> CONFIG GET maxmemoryIt remains at the default value of 0:
1) "maxmemory"
2) "0"Similarly, maxmemory-policy remains at the noeviction default setting:
127.0.0.1:6379> CONFIG GET maxmemory-policyReturns:
1) "maxmemory-policy"
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:
kubectl delete pod redis
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yamlNow re-check the configuration values one last time:
kubectl exec -it pod/redis -- redis-cliCheck maxmemory:
127.0.0.1:6379> CONFIG GET maxmemoryIt should now return the updated value of 2097152:
1) "maxmemory"
2) "2097152"Similarly, maxmemory-policy has also been updated:
127.0.0.1:6379> CONFIG GET maxmemory-policyIt now reflects the desired value of allkeys-lru:
1) "maxmemory-policy"
2) "allkeys-lru"Clean up your work by deleting the created resources:
kubectl delete pod/redis configmap/example-redis-config{{% heading "whatsnext" %}}
- Learn more about ConfigMaps.
- Follow an example of Updating configuration via a ConfigMap.