使用 ConfigMap 配置 Redis

此页面提供了一个使用 ConfigMap 配置 Redis 的真实示例,并基于 配置 Pod 以使用 ConfigMap 任务。

目标

  • 使用 Redis 配置值创建 ConfigMap
  • 创建一个挂载并使用创建的 ConfigMap 的 Redis Pod
  • 验证配置是否已正确应用。

开始之前

您需要拥有一个 Kubernetes 集群,并且 kubectl 命令行工具必须配置为与您的集群通信。建议在至少有两个节点(不充当控制平面主机)的集群上运行本教程。如果您还没有集群,可以使用 minikube 创建一个,或者您可以使用以下 Kubernetes 游乐场之一

要检查版本,请输入 kubectl version

真实示例:使用 ConfigMap 配置 Redis

按照以下步骤使用存储在 ConfigMap 中的数据配置 Redis 缓存。

首先创建一个带有空配置块的 ConfigMap

cat <<EOF >./example-redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-redis-config
data:
  redis-config: ""
EOF

应用上面创建的 ConfigMap,以及 Redis pod 清单

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

检查 Redis pod 清单的内容,并注意以下内容

  • spec.volumes[1] 创建了一个名为 config 的卷
  • spec.volumes[1].configMap.items[0] 下的 keypathexample-redis-config ConfigMap 中的 redis-config 密钥作为名为 redis.conf 的文件暴露在 config 卷上。
  • 然后,spec.containers[0].volumeMounts[1]config 卷挂载到 /redis-master

这将使上面 example-redis-config ConfigMap 中 data.redis-config 中的数据在 Pod 内作为 /redis-master/redis.conf 暴露。

apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis:5.0.4
    command:
      - redis-server
      - "/redis-master/redis.conf"
    env:
    - name: MASTER
      value: "true"
    ports:
    - containerPort: 6379
    resources:
      limits:
        cpu: "0.1"
    volumeMounts:
    - mountPath: /redis-master-data
      name: data
    - mountPath: /redis-master
      name: config
  volumes:
    - name: data
      emptyDir: {}
    - name: config
      configMap:
        name: example-redis-config
        items:
        - key: redis-config
          path: redis.conf

检查创建的对象

kubectl get pod/redis configmap/example-redis-config 

您应该看到以下输出

NAME        READY   STATUS    RESTARTS   AGE
pod/redis   1/1     Running   0          8s

NAME                             DATA   AGE
configmap/example-redis-config   1      14s

回想一下,我们在 example-redis-config ConfigMap 中将 redis-config 密钥留空

kubectl describe configmap/example-redis-config

您应该看到一个空的 redis-config 密钥

Name:         example-redis-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
redis-config:

使用 kubectl exec 进入 pod 并运行 redis-cli 工具以检查当前配置

kubectl exec -it redis -- redis-cli

检查 maxmemory

127.0.0.1:6379> CONFIG GET maxmemory

它应该显示默认值 0

1) "maxmemory"
2) "0"

同样,检查 maxmemory-policy

127.0.0.1:6379> CONFIG GET maxmemory-policy

它也应该产生其默认值 noeviction

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

现在让我们向 example-redis-config ConfigMap 添加一些配置值

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-redis-config
data:
  redis-config: |
    maxmemory 2mb
    maxmemory-policy allkeys-lru    

应用更新的 ConfigMap

kubectl apply -f example-redis-config.yaml

确认 ConfigMap 已更新

kubectl describe configmap/example-redis-config

您应该看到我们刚刚添加的配置值

Name:         example-redis-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
redis-config:
----
maxmemory 2mb
maxmemory-policy allkeys-lru

使用 kubectl exec 通过 redis-cli 再次检查 Redis Pod,以查看配置是否已应用

kubectl exec -it redis -- redis-cli

检查 maxmemory

127.0.0.1:6379> CONFIG GET maxmemory

它仍然保持默认值 0

1) "maxmemory"
2) "0"

同样,maxmemory-policy 仍然保持 noeviction 默认设置

127.0.0.1:6379> CONFIG GET maxmemory-policy

返回

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

配置值没有改变,因为 Pod 需要重新启动才能从关联的 ConfigMap 中获取更新的值。让我们删除并重新创建 Pod

kubectl delete pod redis
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yaml

现在最后一次重新检查配置值

kubectl exec -it redis -- redis-cli

检查 maxmemory

127.0.0.1:6379> CONFIG GET maxmemory

它现在应该返回更新的值 2097152

1) "maxmemory"
2) "2097152"

同样,maxmemory-policy 也已更新

127.0.0.1:6379> CONFIG GET maxmemory-policy

它现在反映了所需的值 allkeys-lru

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

通过删除创建的资源来清理您的工作

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

下一步

上次修改时间:2023 年 12 月 27 日下午 3:37 PST:通过 Configmap 更新配置 (3efc5cde2f)