真实世界的案例:使用 ConfigMap 来配置 Redis

在这里插入图片描述

博客原文

前置知识

k8s 集群配置

IPHost配置
11.0.1.150master1 (keepalived+haproxy)2C 4G 30G
11.0.1.151master2 (keepalived+haproxy)2C 4G 30G
11.0.1.152node12C 4G 30G

vip 地址: https://11.0.1.100:16443

目标

  • 使用 Redis 配置的值创建一个 ConfigMap
  • 创建一个 Redis Pod,挂载并使用创建的 ConfigMap
  • 验证配置已经被正确应用
  • 验证 ConfigMap 挂载情况

创建

创建 configmap

redis-configmap.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-configmap
data:
  redis-config: ""

创建

$ kubectl create -f redis-configmap.yaml

创建 redis pod

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   # redis 会自动读取环境变量
      value: "true"
    ports:
    - containerPort: 6379   # 暴露容器 6379 端口, 协议 tcp, 未指定端口名
    resources:
      limits:  # container 的资源限制
        cpu: "0.1"
    volumeMounts:
    - mountPath: /redis-master-data
      name: data
    - mountPath: /redis-master
      name: config
  volumes:
    - name: data     # 用于写 redis 缓存的临时目录, 随 pod 生命周期创建和销毁
      emptyDir: {}
    - name: config
      configMap:
        name: redis-configmap  # configmap 名
        items:
        - key: redis-config
          path: redis.conf   # 挂载进 pod 后的文件名

创建

$ kubectl create -f redis-pod.yaml

检验 redis 配置

获取 redis 配置信息

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

修改 configmap

$ kubectl edit cm redis-configmap

redis-configmap

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

再次查看 redis 配置

确认 ConfigMap 已更新:

kubectl describe configmap/redis-configmap

你应该可以看到我们刚刚添加的配置:

Name:         redis-configmap
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 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

参考

  1. https://ai-feier.github.io/p/k8s-configmap-%E8%AF%A6%E8%A7%A3-%E4%BB%8E%E7%90%86%E8%A7%A3-k8s-volumes-%E6%8C%82%E8%BD%BD%E6%96%B9%E5%BC%8F-%E5%B8%A6%E4%BD%A0%E8%A7%A3%E5%86%B3%E6%8C%82%E8%BD%BD-configmap-%E7%9A%84%E7%A7%8D%E7%A7%8D%E7%96%91%E9%9A%BE%E6%9D%82%E7%97%87-%E4%BB%A5%E5%8F%8A-k8s-%E4%B8%AD%E5%A6%82%E4%BD%95%E5%88%9B%E5%BB%BA-configmap/
  2. https://kubernetes.io/zh-cn/docs/tutorials/configuration/configure-redis-using-configmap/#real-world-example-configuring-redis-using-a-configmap
  • 43
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

M·K·T

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值