@@ -15,10 +15,8 @@ This page provides a real world example of how to configure Redis using a Config
15
15
## {{% heading "objectives" %}}
16
16
17
17
18
- * Create a ` kustomization.yaml ` file containing:
19
- * a ConfigMap generator
20
- * a Pod resource config using the ConfigMap
21
- * Apply the directory by running ` kubectl apply -k ./ `
18
+ * Create a ConfigMap with Redis configuration values
19
+ * Create a Redis Pod that mounts and uses the created ConfigMap
22
20
* Verify that the configuration was correctly applied.
23
21
24
22
@@ -38,82 +36,218 @@ This page provides a real world example of how to configure Redis using a Config
38
36
39
37
## Real World Example: Configuring Redis using a ConfigMap
40
38
41
- You can follow the steps below to configure a Redis cache using data stored in a ConfigMap.
39
+ Follow the steps below to configure a Redis cache using data stored in a ConfigMap.
42
40
43
- First create a ` kustomization.yaml ` containing a ConfigMap from the ` redis-config ` file:
44
-
45
- {{< codenew file="pods/config/redis-config" >}}
41
+ First create a ConfigMap with an empty configuration block:
46
42
47
43
``` shell
48
- curl -OL https://k8s.io/examples/pods/config/ redis-config
49
-
50
- cat << EOF >./kustomization.yaml
51
- configMapGenerator :
52
- - name: example-redis-config
53
- files :
54
- - redis-config
44
+ cat << EOF >./example- redis-config.yaml
45
+ apiVersion: v1
46
+ kind: ConfigMap
47
+ metadata :
48
+ name: example-redis-config
49
+ data :
50
+ redis-config: ""
55
51
EOF
56
52
```
57
53
58
- Add the pod resource config to the ` kustomization.yaml ` :
54
+ Apply the ConfigMap created above, along with a Redis pod manifest:
55
+
56
+ ``` shell
57
+ kubectl apply -f example-redis-config.yaml
58
+ kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/config/redis-pod.yaml
59
+ ```
60
+
61
+ Examine the contents of the Redis pod manifest and note the following:
62
+
63
+ * A volume named ` config ` is created by ` spec.volumes[1] `
64
+ * The ` key ` and ` path ` under ` spec.volumes[1].items[0] ` exposes the ` redis-config ` key from the
65
+ ` example-redis-config ` ConfigMap as a file named ` redis.conf ` on the ` config ` volume.
66
+ * The ` config ` volume is then mounted at ` /redis-master ` by ` spec.containers[0].volumeMounts[1] ` .
67
+
68
+ This has the net effect of exposing the data in ` data.redis-config ` from the ` example-redis-config `
69
+ ConfigMap above as ` /redis-master/redis.conf ` inside the Pod.
59
70
60
71
{{< codenew file="pods/config/redis-pod.yaml" >}}
61
72
73
+ Examine the created objects:
74
+
62
75
``` shell
63
- curl -OL https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/config/redis-pod.yaml
76
+ kubectl get pod/redis configmap/example-redis-config
77
+ ```
64
78
65
- cat << EOF >>./kustomization.yaml
66
- resources:
67
- - redis-pod.yaml
68
- EOF
79
+ You should see the following output:
80
+
81
+ ``` shell
82
+ NAME READY STATUS RESTARTS AGE
83
+ pod/redis 1/1 Running 0 8s
84
+
85
+ NAME DATA AGE
86
+ configmap/example-redis-config 1 14s
69
87
```
70
88
71
- Apply the kustomization directory to create both the ConfigMap and Pod objects :
89
+ Recall that we left ` redis-config ` key in the ` example-redis-config ` ConfigMap blank :
72
90
73
91
``` shell
74
- kubectl apply -k .
92
+ kubectl describe configmap/example-redis-config
75
93
```
76
94
77
- Examine the created objects by
95
+ You should see an empty ` redis-config ` key:
96
+
78
97
``` shell
79
- > kubectl get -k .
80
- NAME DATA AGE
81
- configmap/example-redis-config-dgh9dg555m 1 52s
98
+ Name: example-redis-config
99
+ Namespace: default
100
+ Labels: < none>
101
+ Annotations: < none>
102
+
103
+ Data
104
+ ====
105
+ redis-config:
106
+ ```
82
107
83
- NAME READY STATUS RESTARTS AGE
84
- pod/redis 1/1 Running 0 52s
108
+ Use ` kubectl exec ` to enter the pod and run the ` redis-cli ` tool to check the current configuration:
109
+
110
+ ``` shell
111
+ kubectl exec -it redis -- redis-cli
112
+ ```
113
+
114
+ Check ` maxmemory ` :
115
+
116
+ ``` shell
117
+ 127.0.0.1:6379> CONFIG GET maxmemory
118
+ ```
119
+
120
+ It should show the default value of 0:
121
+
122
+ ``` shell
123
+ 1) " maxmemory"
124
+ 2) " 0"
125
+ ```
126
+
127
+ Similarly, check ` maxmemory-policy ` :
128
+
129
+ ``` shell
130
+ 127.0.0.1:6379> CONFIG GET maxmemory-policy
131
+ ```
132
+
133
+ Which should also yield its default value of ` noeviction ` :
134
+
135
+ ``` shell
136
+ 1) " maxmemory-policy"
137
+ 2) " noeviction"
138
+ ```
139
+
140
+ Now let's add some configuration values to the ` example-redis-config ` ConfigMap:
141
+
142
+ {{< codenew file="pods/config/example-redis-config.yaml" >}}
143
+
144
+ Apply the updated ConfigMap:
145
+
146
+ ``` shell
147
+ kubectl apply -f example-redis-config.yaml
148
+ ```
149
+
150
+ Confirm that the ConfigMap was updated:
151
+
152
+ ``` shell
153
+ kubectl describe configmap/example-redis-config
85
154
```
86
155
87
- In the example, the config volume is mounted at ` /redis-master ` .
88
- It uses ` path ` to add the ` redis-config ` key to a file named ` redis.conf ` .
89
- The file path for the redis config, therefore, is ` /redis-master/redis.conf ` .
90
- This is where the image will look for the config file for the redis master.
156
+ You should see the configuration values we just added:
91
157
92
- Use ` kubectl exec ` to enter the pod and run the ` redis-cli ` tool to verify that
93
- the configuration was correctly applied:
158
+ ``` shell
159
+ Name: example-redis-config
160
+ Namespace: default
161
+ Labels: < none>
162
+ Annotations: < none>
163
+
164
+ Data
165
+ ====
166
+ redis-config:
167
+ ----
168
+ maxmemory 2mb
169
+ maxmemory-policy allkeys-lru
170
+ ```
171
+
172
+ Check the Redis Pod again using ` redis-cli ` via ` kubectl exec ` to see if the configuration was applied:
94
173
95
174
``` shell
96
175
kubectl exec -it redis -- redis-cli
176
+ ```
177
+
178
+ Check ` maxmemory ` :
179
+
180
+ ``` shell
97
181
127.0.0.1:6379> CONFIG GET maxmemory
182
+ ```
183
+
184
+ It remains at the default value of 0:
185
+
186
+ ``` shell
98
187
1) " maxmemory"
99
- 2) " 2097152"
188
+ 2) " 0"
189
+ ```
190
+
191
+ Similarly, ` maxmemory-policy ` remains at the ` noeviction ` default setting:
192
+
193
+ ``` shell
100
194
127.0.0.1:6379> CONFIG GET maxmemory-policy
195
+ ```
196
+
197
+ Returns:
198
+
199
+ ``` shell
101
200
1) " maxmemory-policy"
102
- 2) " allkeys-lru "
201
+ 2) " noeviction "
103
202
```
104
203
105
- Delete the created pod:
204
+ The configuration values have not changed because the Pod needs to be restarted to grab updated
205
+ values from associated ConfigMaps. Let's delete and recreate the Pod:
206
+
106
207
``` shell
107
208
kubectl delete pod redis
209
+ kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/config/redis-pod.yaml
108
210
```
109
211
212
+ Now re-check the configuration values one last time:
110
213
214
+ ``` shell
215
+ kubectl exec -it redis -- redis-cli
216
+ ```
111
217
112
- ## {{% heading "whatsnext" %}}
218
+ Check ` maxmemory ` :
113
219
220
+ ``` shell
221
+ 127.0.0.1:6379> CONFIG GET maxmemory
222
+ ```
114
223
115
- * Learn more about [ ConfigMaps ] ( /docs/tasks/configure-pod-container/configure-pod-configmap/ ) .
224
+ It should now return the updated value of 2097152:
116
225
226
+ ``` shell
227
+ 1) " maxmemory"
228
+ 2) " 2097152"
229
+ ```
230
+
231
+ Similarly, ` maxmemory-policy ` has also been updated:
117
232
233
+ ``` shell
234
+ 127.0.0.1:6379> CONFIG GET maxmemory-policy
235
+ ```
118
236
237
+ It now reflects the desired value of ` allkeys-lru ` :
119
238
239
+ ``` shell
240
+ 1) " maxmemory-policy"
241
+ 2) " allkeys-lru"
242
+ ```
243
+
244
+ Clean up your work by deleting the created resources:
245
+
246
+ ``` shell
247
+ kubectl delete pod/redis configmap/example-redis-config
248
+ ```
249
+
250
+ ## {{% heading "whatsnext" %}}
251
+
252
+
253
+ * Learn more about [ ConfigMaps] ( /docs/tasks/configure-pod-container/configure-pod-configmap/ ) .
0 commit comments