Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,34 @@ weight: 30

<!-- 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](/docs/tasks/configure-pod-container/configure-pod-configmap/) task.
This page provides a real-world example of how to configure Redis using a ConfigMap.


## Why Configure Redis Using a ConfigMap?

<!-- Here is where I would add the use case / business reason. Would need SME or PM input. However, here's a guess: -->

Redis is a popular open-source database. When deployed on Kubernetes, it gains the benefits of container orchestration,
including increased reliability and performance. To deploy Redis on Kubernetes, you must set up a Redis Pod using a
ConfigMap.

## Who Should Do This Tutorial?

<!-- Again, this is guessing, and would need SME input. But it's useful to let people know whether they should be in this page. -->

This Tutorial is for Redis administrators who want to use Kubernetes to manage their Redis servers.

## Terms


* **ConfigMap**: A Kubernetes configuration file used to set values in a Pod. See [ConfigMaps](https://kubernetes.io/docs/concepts/configuration/configmap/).
* **Redis**: (REmote DIctionary Server) An open-source, in-memory database. See [Introduction to Redis](https://redis.io/about/) (external link).
* **Pod**: The smallest unit of computing that you can deploy in Kubernetes. See [Pods](https://kubernetes.io/docs/concepts/workloads/pods/).


## {{% heading "objectives" %}}

By the end of this Tutorial, you will:

* Create a ConfigMap with Redis configuration values
* Create a Redis Pod that mounts and uses the created ConfigMap
Expand All @@ -27,19 +49,29 @@ This page provides a real world example of how to configure Redis using a Config

{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}

* The example shown on this page works with `kubectl` 1.14 and above.
* Understand [Configure a Pod to Use a ConfigMap](/docs/tasks/configure-pod-container/configure-pod-configmap/).
The example shown on this page works with `kubectl` 1.14 and above.
Understand the concepts and procedures in [Configure a Pod to Use a ConfigMap](/docs/tasks/configure-pod-container/configure-pod-configmap/).



<!-- lessoncontent -->


## Real World Example: Configuring Redis using a ConfigMap
## Tutorial Steps: Configuring Redis using a ConfigMap

Follow these steps 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.
* [Step 1: Create a ConfigMap](#step-1-create-a-configmap)
* [Step 2: Apply the ConfigMap and Pod Manifest](#step-2-apply-the-configmap-and-pod-manifest)
* [Step 3: Examine the Created Objects](#step-3-examine-the-created-objects)
* [Step 4: Check the Current Pod Configuration](#step-4-check-the-current-pod-configuration)
* [Step 5: Add Configuration Values](#step-5-add-configuration-values)
* [Step 6: Restart the Redis Pod](#step-6-restart-the-redis-pod)
* [Step 7: Clean Up](#step-7-clean-up)

First create a ConfigMap with an empty configuration block:
### Step 1: Create a ConfigMap

Create a ConfigMap named `example-redis-config` with an empty configuration block (`redis-config: ""`):

```shell
cat <<EOF >./example-redis-config.yaml
Expand All @@ -52,26 +84,40 @@ data:
EOF
```

Apply the ConfigMap created above, along with a Redis pod manifest:
### Step 2: Apply the ConfigMap and Pod Manifest

Apply the ConfigMap you created, along with the example Redis Pod manifest `redis-pod.yaml` that has been provided to you as part of this Tutorial:

```shell
kubectl apply -f example-redis-config.yaml
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:
In this step, you used the `kubectl apply` command. This command applies a configuration to a resource - in this case, your Pod.
Because the Pod did not already exist, the command creates it.

For more information about this command, see [kubectl apply](https://kubernetes.io/docs/reference/kubectl/generated/kubectl_apply/).

Before moving on, you can learn more about what you did in this step.
Examine the contents of the Redis Pod manifest by opening the file `kubernetes/website/main/content/en/examples/pods/config/redis-pod.yaml`.

{{% code_sample file="pods/config/redis-pod.yaml" %}}

In the manifest, you can see the following:

* A volume named `config` is created by `spec.volumes[1]`
* A volume named `config` is created by `spec.volumes[1]`.
* The `key` and `path` under `spec.volumes[1].configMap.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.
ConfigMap above as `/redis-master/redis.conf` inside the new Pod.

{{% code_sample file="pods/config/redis-pod.yaml" %}}
### Step 3: Examine the Created Objects

Examine the created objects:
To verify that the Pod was created correctly, examine the created objects.

First, get the information about the Pod and ConfigMap:

```shell
kubectl get pod/redis configmap/example-redis-config
Expand All @@ -87,7 +133,8 @@ NAME DATA AGE
configmap/example-redis-config 1 14s
```

Recall that we left `redis-config` key in the `example-redis-config` ConfigMap blank:
Next, take a closer look at the ConfigMap. Recall that you left the `redis-config` key blank in the `example-redis-config` ConfigMap.
Run the following command to confirm:

```shell
kubectl describe configmap/example-redis-config
Expand All @@ -106,19 +153,31 @@ Data
redis-config:
```

Use `kubectl exec` to enter the pod and run the `redis-cli` tool to check the current configuration:
In this step, you used two commands. If you want to learn more about them, see:

* [kubectl get](https://kubernetes.io/docs/reference/kubectl/generated/kubectl_get/)
* [kubectl describe](https://kubernetes.io/docs/reference/kubectl/generated/kubectl_describe/)

### Step 4: Check the Current Pod Configuration


To check the current configuration of your new Redis Pod, you need to run commands on Redis.
You can use the `kubectl exec` command to execute commands inside a container, like your Redis Pod.
Access the Pod and start the Redis command-line tool `redis-cli`:

```shell
kubectl exec -it redis -- redis-cli
```

Now you can use Redis commands to check the current configuration.

Check `maxmemory`:

```shell
127.0.0.1:6379> CONFIG GET maxmemory
```

It should show the default value of 0:
It should show the default value, 0:

```shell
1) "maxmemory"
Expand All @@ -131,18 +190,27 @@ Similarly, check `maxmemory-policy`:
127.0.0.1:6379> CONFIG GET maxmemory-policy
```

Which should also yield its default value of `noeviction`:
It should show the default value, `noeviction`:

```shell
1) "maxmemory-policy"
2) "noeviction"
```

Now let's add some configuration values to the `example-redis-config` ConfigMap:
For more information about the commands you used in this step, see:
* [kubectl exec](https://kubernetes.io/docs/reference/kubectl/generated/kubectl_exec/)
* [Redis CLI](https://redis.io/docs/latest/develop/tools/cli/) (external link)
* [CONFIG GET](https://redis.io/docs/latest/commands/config-get/) (external link)

### Step 5: Add Configuration Values

Now that the Redis Pod has been created, add some configuration values to the `example-redis-config` ConfigMap
and apply them to the Pod.
Open the file `pods/config/example-redis-config.yaml` and edit the `redis-config` section to look like the following:

{{% code_sample file="pods/config/example-redis-config.yaml" %}}

Apply the updated ConfigMap:
Apply the updated ConfigMap to the Redis Pod:

```shell
kubectl apply -f example-redis-config.yaml
Expand All @@ -154,7 +222,7 @@ Confirm that the ConfigMap was updated:
kubectl describe configmap/example-redis-config
```

You should see the configuration values we just added:
You should see the configuration values you just added:

```shell
Name: example-redis-config
Expand All @@ -170,7 +238,10 @@ maxmemory 2mb
maxmemory-policy allkeys-lru
```

Check the Redis Pod again using `redis-cli` via `kubectl exec` to see if the configuration was applied:
### Step 6: Restart the Redis Pod

The ConfigMap is updated with new configuration values, but you have to restart the Pod for the new configuration to take effect.
If you want to confirm that the new configuration has not yet been applied, check the Redis Pod again using `kubectl exec` and `redis-cli`:

```shell
kubectl exec -it redis -- redis-cli
Expand All @@ -189,28 +260,29 @@ It remains at the default value of 0:
2) "0"
```

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

```shell
127.0.0.1:6379> CONFIG GET maxmemory-policy
```

Returns:
It remains at the `noeviction` default setting:

```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:
When you restart a Pod, it gets updated values from all of its associated ConfigMaps.

To restart the Redis Pod, delete and recreate it:

```shell
kubectl delete pod redis
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:
To verify, check the configuration values again:

```shell
kubectl exec -it redis -- redis-cli
Expand All @@ -222,7 +294,7 @@ Check `maxmemory`:
127.0.0.1:6379> CONFIG GET maxmemory
```

It should now return the updated value of 2097152:
It should now show the updated value of 2097152:

```shell
1) "maxmemory"
Expand All @@ -235,19 +307,27 @@ Similarly, `maxmemory-policy` has also been updated:
127.0.0.1:6379> CONFIG GET maxmemory-policy
```

It now reflects the desired value of `allkeys-lru`:
It now shows the desired value of `allkeys-lru`:

```shell
1) "maxmemory-policy"
2) "allkeys-lru"
```

Clean up your work by deleting the created resources:
### Step 7: Clean Up

Clean up your work by deleting the resources you created:

```shell
kubectl delete pod/redis configmap/example-redis-config
```

## Troubleshooting

<!-- Would need help from SME, maybe Customer Support, to write this section -->

If you need help at any point while doing this Tutorial, contact our [Tutorial Hotline](https://fictionalsite.com).

## {{% heading "whatsnext" %}}


Expand Down