@@ -19,17 +19,13 @@ This page provides a real world example of how to configure Redis using a Config
19
19
20
20
21
21
<!--
22
- * * Create a `kustomization.yaml` file containing:
23
- * a ConfigMap generator
24
- * a Pod resource config using the ConfigMap
25
- * Apply the directory by running `kubectl apply -k ./`
22
+ * Create a ConfigMap with Redis configuration values
23
+ * Create a Redis Pod that mounts and uses the created ConfigMap
26
24
* Verify that the configuration was correctly applied.
27
25
-->
28
26
29
- * * 创建一个包含以下内容的 ` kustomization.yaml ` 文件:
30
- * 一个 ConfigMap 生成器
31
- * 一个使用 ConfigMap 的 Pod 资源配置
32
- * 使用 ` kubectl apply -k ./ ` 应用整个路径的配置
27
+ * 使用 Redis 配置的值创建一个 ConfigMap
28
+ * 创建一个 Redis Pod,挂载并使用创建的 ConfigMap
33
29
* 验证配置已经被正确应用。
34
30
35
31
@@ -55,105 +51,311 @@ This page provides a real world example of how to configure Redis using a Config
55
51
<!--
56
52
## Real World Example: Configuring Redis using a ConfigMap
57
53
58
- You can follow the steps below to configure a Redis cache using data stored in a ConfigMap.
54
+ Follow the steps below to configure a Redis cache using data stored in a ConfigMap.
59
55
60
- First create a `kustomization.yaml` containing a ConfigMap from the `redis-config` file :
56
+ First create a ConfigMap with an empty configuration block :
61
57
-->
62
58
## 真实世界的案例:使用 ConfigMap 来配置 Redis
63
59
64
- 按照下面的步骤,您可以使用ConfigMap中的数据来配置Redis缓存 。
60
+ 按照下面的步骤,使用 ConfigMap 中的数据来配置 Redis 缓存 。
65
61
66
- 1 . 根据 ` docs/user-guide/configmap/redis/redis-config ` 来创建一个ConfigMap :
62
+ 首先创建一个配置模块为空的 ConfigMap :
67
63
64
+ ``` shell
65
+ cat << EOF >./example-redis-config.yaml
66
+ apiVersion: v1
67
+ kind: ConfigMap
68
+ metadata:
69
+ name: example-redis-config
70
+ data:
71
+ redis-config: ""
72
+ EOF
73
+ ```
68
74
69
- {{< codenew file="pods/config/redis-config" >}}
75
+ <!--
76
+ Apply the ConfigMap created above, along with a Redis pod manifest:
77
+ -->
78
+ 应用上面创建的 ConfigMap 以及 Redis pod 清单:
70
79
71
80
``` shell
72
- curl -OL https://k8s.io/examples/pods/config/redis-config
73
-
74
- cat << EOF >./kustomization.yaml
75
- configMapGenerator:
76
- - name: example-redis-config
77
- files:
78
- - redis-config
79
- EOF
81
+ kubectl apply -f example-redis-config.yaml
82
+ kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/config/redis-pod.yaml
80
83
```
81
84
82
85
<!--
83
- Add the pod resource config to the `kustomization.yaml`:
86
+ Examine the contents of the Redis pod manifest and note the following:
87
+
88
+ * A volume named `config` is created by `spec.volumes[1]`
89
+ * The `key` and `path` under `spec.volumes[1].items[0]` exposes the `redis-config` key from the
90
+ `example-redis-config` ConfigMap as a file named `redis.conf` on the `config` volume.
91
+ * The `config` volume is then mounted at `/redis-master` by `spec.containers[0].volumeMounts[1]`.
92
+
93
+ This has the net effect of exposing the data in `data.redis-config` from the `example-redis-config`
94
+ ConfigMap above as `/redis-master/redis.conf` inside the Pod.
84
95
-->
85
- 将 pod 的资源配置添加到 ` kustomization.yaml ` 文件中:
96
+ 检查 Redis pod 清单的内容,并注意以下几点:
97
+
98
+ * 由 ` spec.volumes[1] ` 创建一个名为 ` config ` 的卷。
99
+ * ` spec.volumes[1].items[0] ` 下的 ` key ` 和 ` path ` 会将来自 ` example-redis-config `
100
+ ConfigMap 中的 ` redis-config ` 密钥公开在 ` config ` 卷上一个名为 ` redis-config ` 的文件中。
101
+ * 然后 ` config ` 卷被 ` spec.containers[0].volumeMounts[1] ` 挂载在 ` /redis-master ` 。
102
+
103
+ 这样做的最终效果是将上面 ` example-redis-config ` 配置中 ` data.redis-config ` 的数据作为 Pod 中的 ` /redis-master/redis.conf ` 公开。
86
104
87
105
{{< codenew file="pods/config/redis-pod.yaml" >}}
88
106
107
+ <!--
108
+ Examine the created objects:
109
+ -->
110
+ 检查创建的对象:
111
+
89
112
``` shell
90
- curl -OL https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/config/redis-pod.yaml
113
+ kubectl get pod/redis configmap/example-redis-config
114
+ ```
91
115
92
- cat << EOF >>./kustomization.yaml
93
- resources:
94
- - redis-pod.yaml
95
- EOF
116
+ <!--
117
+ You should see the following output:
118
+ -->
119
+ 你应该可以看到以下输出:
120
+
121
+ ``` shell
122
+ NAME READY STATUS RESTARTS AGE
123
+ pod/redis 1/1 Running 0 8s
124
+
125
+ NAME DATA AGE
126
+ configmap/example-redis-config 1 14s
96
127
```
97
128
98
129
<!--
99
- Apply the kustomization directory to create both the ConfigMap and Pod objects:
130
+ Recall that we left `redis-config` key in the `example-redis-config` ConfigMap blank:
100
131
-->
101
- 应用整个 kustomization 文件夹以创建 ConfigMap 和 Pod 对象 :
132
+ 回顾一下,我们在 ` example-redis-config ` ConfigMap 保留了空的 ` redis-config ` 键 :
102
133
103
134
``` shell
104
- kubectl apply -k .
135
+ kubectl describe configmap/example-redis-config
105
136
```
106
137
107
138
<!--
108
- Examine the created objects by
139
+ You should see an empty `redis-config` key:
109
140
-->
110
- 使用以下命令检查创建的对象
141
+ 你应该可以看到一个空的 ` redis-config ` 键:
111
142
112
143
``` shell
113
- > kubectl get -k .
114
- NAME DATA AGE
115
- configmap/example-redis-config-dgh9dg555m 1 52s
144
+ Name: example-redis-config
145
+ Namespace: default
146
+ Labels: < none>
147
+ Annotations: < none>
148
+
149
+ Data
150
+ ====
151
+ redis-config:
152
+ ```
116
153
117
- NAME READY STATUS RESTARTS AGE
118
- pod/redis 1/1 Running 0 52s
154
+ <!--
155
+ Use `kubectl exec` to enter the pod and run the `redis-cli` tool to check the current configuration:
156
+ -->
157
+ 使用 ` kubectl exec ` 进入 pod,运行 ` redis-cli ` 工具检查当前配置:
158
+
159
+ ``` shell
160
+ kubectl exec -it redis -- redis-cli
161
+ ```
162
+
163
+ <!--
164
+ Check `maxmemory`:
165
+ -->
166
+ 查看 ` maxmemory ` :
167
+
168
+ ``` shell
169
+ 127.0.0.1:6379> CONFIG GET maxmemory
170
+ ```
171
+
172
+ <!--
173
+ It should show the default value of 0:
174
+ -->
175
+ 它应该显示默认值 0:
176
+
177
+ ``` shell
178
+ 1) " maxmemory"
179
+ 2) " 0"
180
+ ```
181
+
182
+ <!--
183
+ Similarly, check `maxmemory-policy`:
184
+ -->
185
+ 同样,查看 ` maxmemory-policy ` :
186
+
187
+ ``` shell
188
+ 127.0.0.1:6379> CONFIG GET maxmemory-policy
189
+ ```
190
+
191
+ <!--
192
+ Which should also yield its default value of `noeviction`:
193
+ -->
194
+ 它也应该显示默认值 ` noeviction ` :
195
+
196
+ ``` shell
197
+ 1) " maxmemory-policy"
198
+ 2) " noeviction"
199
+ ```
200
+
201
+ <!--
202
+ Now let's add some configuration values to the `example-redis-config` ConfigMap:
203
+ -->
204
+ 现在,向 ` example-redis-config ` ConfigMap 添加一些配置:
205
+
206
+ {{< codenew file="pods/config/example-redis-config.yaml" >}}
207
+
208
+ <!--
209
+ Apply the updated ConfigMap:
210
+ -->
211
+ 应用更新的 ConfigMap:
212
+
213
+ ``` shell
214
+ kubectl apply -f example-redis-config.yaml
119
215
```
120
216
121
217
<!--
122
- In the example, the config volume is mounted at `/redis-master`.
123
- It uses `path` to add the `redis-config` key to a file named `redis.conf`.
124
- The file path for the redis config, therefore, is `/redis-master/redis.conf`.
125
- This is where the image will look for the config file for the redis master.
218
+ Confirm that the ConfigMap was updated:
126
219
-->
127
- 在示例中,配置卷挂载在 ` /redis-master ` 下。
128
- 它使用 ` path ` 将 ` redis-config ` 密钥添加到名为 ` redis.conf ` 的文件中。
129
- 因此,redis配置的文件路径为 ` /redis-master/redis.conf ` 。
130
- 这是镜像将在其中查找 redis master 的配置文件的位置。
220
+ 确认 ConfigMap 已更新:
221
+
222
+ ``` shell
223
+ kubectl describe configmap/example-redis-config
224
+ ```
131
225
132
226
<!--
133
- Use `kubectl exec` to enter the pod and run the `redis-cli` tool to verify that
134
- the configuration was correctly applied:
227
+ You should see the configuration values we just added:
135
228
-->
136
- 使用 ` kubectl exec ` 进入 pod 并运行 ` redis-cli ` 工具来验证配置已正确应用:
229
+ 你应该可以看到我们刚刚添加的配置:
230
+
231
+ ``` shell
232
+ Name: example-redis-config
233
+ Namespace: default
234
+ Labels: < none>
235
+ Annotations: < none>
236
+
237
+ Data
238
+ ====
239
+ redis-config:
240
+ ----
241
+ maxmemory 2mb
242
+ maxmemory-policy allkeys-lru
243
+ ```
244
+
245
+ <!--
246
+ Check the Redis Pod again using `redis-cli` via `kubectl exec` to see if the configuration was applied:
247
+ -->
248
+ 通过 ` kubectl exec ` 使用 ` redis-cli ` 再次检查 Redis Pod,查看是否已应用配置:
137
249
138
250
``` shell
139
251
kubectl exec -it redis -- redis-cli
252
+ ```
253
+
254
+ <!--
255
+ Check `maxmemory`:
256
+ -->
257
+ 查看 ` maxmemory ` :
258
+
259
+ ``` shell
140
260
127.0.0.1:6379> CONFIG GET maxmemory
261
+ ```
262
+
263
+ <!--
264
+ It remains at the default value of 0:
265
+ -->
266
+ 它保持默认值 0:
267
+
268
+ ``` shell
141
269
1) " maxmemory"
142
- 2) " 2097152"
270
+ 2) " 0"
271
+ ```
272
+
273
+ <!--
274
+ Similarly, `maxmemory-policy` remains at the `noeviction` default setting:
275
+ -->
276
+ 同样,` maxmemory-policy ` 保留为默认设置 ` noeviction ` :
277
+
278
+ ``` shell
143
279
127.0.0.1:6379> CONFIG GET maxmemory-policy
280
+ ```
281
+
282
+ <!--
283
+ Returns:
284
+ -->
285
+ 返回:
286
+
287
+ ``` shell
144
288
1) " maxmemory-policy"
145
- 2) " allkeys-lru "
289
+ 2) " noeviction "
146
290
```
147
291
148
292
<!--
149
- Delete the created pod:
293
+ The configuration values have not changed because the Pod needs to be restarted to grab updated
294
+ values from associated ConfigMaps. Let's delete and recreate the Pod:
150
295
-->
151
- 删除创建的 pod:
296
+ 配置值未更改,因为需要重新启动 Pod 才能从关联的 ConfigMap 中获取更新的值。
297
+ 让我们删除并重新创建 Pod:
298
+
152
299
``` shell
153
300
kubectl delete pod redis
301
+ kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/config/redis-pod.yaml
154
302
```
155
303
304
+ <!--
305
+ Now re-check the configuration values one last time:
306
+ -->
307
+ 现在,最后一次重新检查配置值:
308
+
309
+ ``` shell
310
+ kubectl exec -it redis -- redis-cli
311
+ ```
156
312
313
+ <!--
314
+ Check `maxmemory`:
315
+ -->
316
+ 查看 ` maxmemory ` :
317
+
318
+ ``` shell
319
+ 127.0.0.1:6379> CONFIG GET maxmemory
320
+ ```
321
+
322
+ <!--
323
+ It should now return the updated value of 2097152:
324
+ -->
325
+ 现在,它应该返回更新后的值 2097152:
326
+
327
+ ``` shell
328
+ 1) " maxmemory"
329
+ 2) " 2097152"
330
+ ```
331
+
332
+ <!--
333
+ Similarly, `maxmemory-policy` has also been updated:
334
+ -->
335
+ 同样,` maxmemory-policy ` 也已更新:
336
+
337
+ ``` shell
338
+ 127.0.0.1:6379> CONFIG GET maxmemory-policy
339
+ ```
340
+
341
+ <!--
342
+ It now reflects the desired value of `allkeys-lru`:
343
+ -->
344
+ 现在它反映了期望值 ` allkeys-lru ` :
345
+
346
+ ``` shell
347
+ 1) " maxmemory-policy"
348
+ 2) " allkeys-lru"
349
+ ```
350
+
351
+ <!--
352
+ Clean up your work by deleting the created resources:
353
+ -->
354
+ 删除创建的资源,清理你的工作:
355
+
356
+ ``` shell
357
+ kubectl delete pod/redis configmap/example-redis-config
358
+ ```
157
359
158
360
## {{% heading "whatsnext" %}}
159
361
0 commit comments