docs(configure-redis-using-configmap): update for clarity (#25712)
* docs(configure-redis-using-configmap): update for clarity Signed-off-by: Jai Govindani <jai@honestbank.com> * fix(configure-redis-using-configmap): incorrect volumeMount index Signed-off-by: Jai Govindani <jai@honestbank.com> * fix(configure-redis-using-configmap): show Pod status as Running, separate commands from output Signed-off-by: Jai Govindani <jai@honestbank.com> * fix(configure-redis-using-configmap): typo Signed-off-by: Jai Govindani <jai@honestbank.com> * fix(configure-redis-using-configmap): configmap name Signed-off-by: Jai Govindani <jai@honestbank.com>
This commit is contained in:
@@ -15,10 +15,8 @@ This page provides a real world example of how to configure Redis using a Config
|
|||||||
## {{% heading "objectives" %}}
|
## {{% heading "objectives" %}}
|
||||||
|
|
||||||
|
|
||||||
* Create a `kustomization.yaml` file containing:
|
* Create a ConfigMap with Redis configuration values
|
||||||
* a ConfigMap generator
|
* Create a Redis Pod that mounts and uses the created ConfigMap
|
||||||
* a Pod resource config using the ConfigMap
|
|
||||||
* Apply the directory by running `kubectl apply -k ./`
|
|
||||||
* Verify that the configuration was correctly applied.
|
* Verify that the configuration was correctly applied.
|
||||||
|
|
||||||
|
|
||||||
@@ -38,82 +36,218 @@ This page provides a real world example of how to configure Redis using a Config
|
|||||||
|
|
||||||
## Real World Example: Configuring Redis using a ConfigMap
|
## 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.
|
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:
|
First create a ConfigMap with an empty configuration block:
|
||||||
|
|
||||||
{{< codenew file="pods/config/redis-config" >}}
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
curl -OL https://k8s.io/examples/pods/config/redis-config
|
cat <<EOF >./example-redis-config.yaml
|
||||||
|
apiVersion: v1
|
||||||
cat <<EOF >./kustomization.yaml
|
kind: ConfigMap
|
||||||
configMapGenerator:
|
metadata:
|
||||||
- name: example-redis-config
|
name: example-redis-config
|
||||||
files:
|
data:
|
||||||
- redis-config
|
redis-config: ""
|
||||||
EOF
|
EOF
|
||||||
```
|
```
|
||||||
|
|
||||||
Add the pod resource config to the `kustomization.yaml`:
|
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/master/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.
|
||||||
|
|
||||||
{{< codenew file="pods/config/redis-pod.yaml" >}}
|
{{< codenew file="pods/config/redis-pod.yaml" >}}
|
||||||
|
|
||||||
```shell
|
Examine the created objects:
|
||||||
curl -OL https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/config/redis-pod.yaml
|
|
||||||
|
|
||||||
cat <<EOF >>./kustomization.yaml
|
```shell
|
||||||
resources:
|
kubectl get pod/redis configmap/example-redis-config
|
||||||
- redis-pod.yaml
|
|
||||||
EOF
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Apply the kustomization directory to create both the ConfigMap and Pod objects:
|
You should see the following output:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
kubectl apply -k .
|
|
||||||
```
|
|
||||||
|
|
||||||
Examine the created objects by
|
|
||||||
```shell
|
|
||||||
> kubectl get -k .
|
|
||||||
NAME DATA AGE
|
|
||||||
configmap/example-redis-config-dgh9dg555m 1 52s
|
|
||||||
|
|
||||||
NAME READY STATUS RESTARTS AGE
|
NAME READY STATUS RESTARTS AGE
|
||||||
pod/redis 1/1 Running 0 52s
|
pod/redis 1/1 Running 0 8s
|
||||||
|
|
||||||
|
NAME DATA AGE
|
||||||
|
configmap/example-redis-config 1 14s
|
||||||
```
|
```
|
||||||
|
|
||||||
In the example, the config volume is mounted at `/redis-master`.
|
Recall that we left `redis-config` key in the `example-redis-config` ConfigMap blank:
|
||||||
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
|
```shell
|
||||||
the configuration was correctly applied:
|
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
|
```shell
|
||||||
kubectl exec -it redis -- redis-cli
|
kubectl exec -it redis -- redis-cli
|
||||||
|
```
|
||||||
|
|
||||||
|
Check `maxmemory`:
|
||||||
|
|
||||||
|
```shell
|
||||||
127.0.0.1:6379> CONFIG GET maxmemory
|
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:
|
||||||
|
|
||||||
|
{{< codenew file="pods/config/example-redis-config.yaml" >}}
|
||||||
|
|
||||||
|
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 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 have not changed because the Pod needs to be restarted to grab updated
|
||||||
|
values from associated ConfigMaps. Let's delete and recreate the Pod:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
kubectl delete pod redis
|
||||||
|
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/config/redis-pod.yaml
|
||||||
|
```
|
||||||
|
|
||||||
|
Now re-check the configuration values one last time:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
kubectl exec -it redis -- redis-cli
|
||||||
|
```
|
||||||
|
|
||||||
|
Check `maxmemory`:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
127.0.0.1:6379> CONFIG GET maxmemory
|
||||||
|
```
|
||||||
|
|
||||||
|
It should now return the updated value of 2097152:
|
||||||
|
|
||||||
|
```shell
|
||||||
1) "maxmemory"
|
1) "maxmemory"
|
||||||
2) "2097152"
|
2) "2097152"
|
||||||
|
```
|
||||||
|
|
||||||
|
Similarly, `maxmemory-policy` has also been updated:
|
||||||
|
|
||||||
|
```shell
|
||||||
127.0.0.1:6379> CONFIG GET maxmemory-policy
|
127.0.0.1:6379> CONFIG GET maxmemory-policy
|
||||||
|
```
|
||||||
|
|
||||||
|
It now reflects the desired value of `allkeys-lru`:
|
||||||
|
|
||||||
|
```shell
|
||||||
1) "maxmemory-policy"
|
1) "maxmemory-policy"
|
||||||
2) "allkeys-lru"
|
2) "allkeys-lru"
|
||||||
```
|
```
|
||||||
|
|
||||||
Delete the created pod:
|
Clean up your work by deleting the created resources:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
kubectl delete pod redis
|
kubectl delete pod/redis configmap/example-redis-config
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## {{% heading "whatsnext" %}}
|
## {{% heading "whatsnext" %}}
|
||||||
|
|
||||||
|
|
||||||
* Learn more about [ConfigMaps](/docs/tasks/configure-pod-container/configure-pod-configmap/).
|
* Learn more about [ConfigMaps](/docs/tasks/configure-pod-container/configure-pod-configmap/).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: example-redis-config
|
||||||
|
data:
|
||||||
|
redis-config: |
|
||||||
|
maxmemory 2mb
|
||||||
|
maxmemory-policy allkeys-lru
|
||||||
Reference in New Issue
Block a user