应用配置
Configure Redis Using Configmap Zh Cn

使用 ConfigMap 配置 Redis

本页面提供了一个在实际场景中如何使用 ConfigMap 配置 Redis 的示例,它基于 “配置 Pod 以使用 ConfigMap” 任务构建。

目标

创建一个包含 Redis 配置值的 ConfigMap。

创建一个挂载并使用所创建 ConfigMap 的 Redis Pod。

验证配置是否正确应用。

开始之前

你需要拥有一个 Kubernetes 集群,并且必须配置好kubectl命令行工具,使其能够与你的集群进行通信。建议在至少有两个非控制平面节点的集群上运行本教程。如果你还没有集群,可以使用 Minikube 创建一个,或者使用以下 Kubernetes 在线实验环境之一:

  • Killercoda
  • KodeKloud
  • Play with Kubernetes

要查看版本,输入kubectl version。

本页面展示的示例适用于kubectl 1.14 及以上版本。

请先理解 “配置 Pod 以使用 ConfigMap” 相关内容。

实际示例:使用 ConfigMap 配置 Redis

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

步骤一:创建一个带有空配置块的 ConfigMap

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

上述命令通过 cat 命令将一段 YAML 内容写入到 ./example-redis-config.yaml 文件中,创建了一个名为 example-redis-config 的 ConfigMap,其中 redis-config 键的值为空字符串。

步骤二:应用创建的 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

第一条命令将刚刚创建的 example-redis-config.yaml 配置应用到 Kubernetes 集群中,创建对应的 ConfigMap 资源。第二条命令从指定的 GitHub 地址获取 redis-pod.yaml 文件并应用到集群中,创建一个 Redis Pod。

步骤三:检查 Redis Pod 清单的内容并注意以下几点 在 spec.volumes[1] 中创建了一个名为 config 的卷。 spec.volumes[1].configMap.items[0] 下的 key 和 path 将 example-redis-config ConfigMap 中的 redis-config 键作为 config 卷上名为 redis.conf 的文件暴露出来。 然后,config 卷通过 spec.containers[0].volumeMounts[1] 挂载到 /redis-master 路径下。 这样做的最终效果是将上述 example-redis-config ConfigMapdata.redis-config 的数据作为 Pod 内的 /redis-master/redis.conf 文件暴露出来。 以下是 redis-pod.yaml 的具体内容:

# pods/config/redis-pod.yaml
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

在这个 Pod 定义中,使用 redis:5.0.4 镜像启动一个名为 redis 的容器,容器启动命令为 redis-server /redis-master/redis.conf,表示使用挂载在 /redis-master 路径下的 redis.conf 文件来配置 Redis 服务器。

步骤四:检查创建的对象

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

此命令用于查看名为 redis 的 Pod 和名为 example-redis-config 的 ConfigMap 的状态信息,以确认它们是否成功创建。

你应该会看到以下输出:

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:
----
maxmemory 2mb
maxmemory-policy allkeys-lru

使用 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"

重启 Pod 以更新配置:由于配置值的更新需要重启 Pod 才能从关联的 ConfigMap 获取到新值,所以要先删除现有的 redis 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 进入 redis Pod 并启动 redis-cli 客户端,进而检查 Redis 的配置。

kubectl exec -it redis -- redis-cli

检查 maxmemory:在 redis-cli 中执行以下命令查看 maxmemory 的值,正常情况下应该返回更新后的值 2097152。

127.0.0.1:6379> CONFIG GET maxmemory

预期输出:

1) "maxmemory"
2) "2097152"

检查 maxmemory-policy:在 redis-cli 中执行以下命令查看 maxmemory-policy 的值,应该返回更新后的值 allkeys-lru。

127.0.0.1:6379> CONFIG GET maxmemory-policy

预期输出:

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

清理资源:完成检查之后,删除之前创建的 redis Pod 和 example-redis-config ConfigMap 以释放资源。

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

通过以上步骤,你可以实现使用 ConfigMap 配置 Redis 并验证配置更新,最后清理掉创建的资源。