使用 ConfigMap 配置 Redis动态更新

一、创建空的 ConfigMap

1. 创建初始为空的 ConfigMap


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

2. 应用 ConfigMap


kubectl apply -f redis-config.yaml

二、部署 Redis Pod(直接使用 Pod,非 Deployment)

1. 编写并部署 redis-pod.yaml

内容如下(已优化排版):


apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis:8.0.2
    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: redis-config
      items:
      - key: redis-config
        path: redis.conf

2. 应用 Pod


kubectl apply -f redis-pod.yaml

3. 验证对象是否创建成功


kubectl get pod/redis configmap/redis-config

预期输出:


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

NAME               DATA   AGE
redis-config       1      3s

4. 查看 ConfigMap 详情(初始为空)


kubectl describe configmap/redis-config

你应该会看到:


Data
====
redis-config:

(即值为空字符串)

5. 进入 Redis Pod 检查默认配置


kubectl exec -it redis -- redis-cli

在 Redis CLI 中执行:


CONFIG GET maxmemory
CONFIG GET maxmemory-policy

预期输出:


1) "maxmemory"
2) "0"

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

✅ 说明 Redis 使用的是默认配置,未加载自定义配置。


三、更新 ConfigMap,添加 Redis 配置参数

1. 编辑 redis-config.yaml,添加实际配置


cat <<EOF > ./redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
data:
  redis-config: |
    maxmemory 2mb
    maxmemory-policy allkeys-lru
EOF

2. 应用更新后的 ConfigMap


kubectl apply -f redis-config.yaml

3. 检查 ConfigMap 是否更新成功


kubectl describe configmap/redis-config

预期输出内容包含:


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

⚠️ 注意:虽然 ConfigMap 已更新,但 已运行的 Pod 不会自动加载新配置


四、验证 Redis 配置(此时尚未生效)

再次进入 Redis 容器查看配置:


kubectl exec -it redis -- redis-cli

执行:


CONFIG GET maxmemory
CONFIG GET maxmemory-policy

你仍然会看到:


1) "maxmemory"
2) "0"

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

❗ 原因:Kubernetes 中 ConfigMap 的更新 不会自动热加载到正在运行的容器中


五、迁移到 Deployment(推荐生产使用)

1. 创建 redis-deployment.yaml

内容如下(已优化排版 & 添加注释):


# redis-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  labels:
    app: redis
  annotations:
    config/last-applied: "v1"  # 可用于标识配置版本,辅助触发更新
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:7.4.3
        command:
          - redis-server
          - "/redis-master/redis.conf"
        ports:
          - containerPort: 6379
        env:
          - name: MASTER
            value: "true"
        resources:
          limits:
            cpu: "0.1"
        volumeMounts:
          - name: data
            mountPath: /redis-master-data
          - name: config
            mountPath: /redis-master
            readOnly: true  # ConfigMap 是只读的,建议加上
      volumes:
      - name: data
        emptyDir: {}  # 临时存储,Pod 删除后数据丢失
      - name: config
        configMap:
          name: redis-config
          items:
            - key: redis-config
              path: redis.conf

2. 应用 Deployment


kubectl apply -f redis-deployment.yaml

六、创建 Service 暴露 Redis

1. 创建 redis-service.yaml


# redis-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  selector:
    app: redis
  ports:
    - protocol: TCP
      port: 6379
      targetPort: 6379
  type: ClusterIP  # 可选:NodePort / LoadBalancer(根据需求)

2. 应用 Service


kubectl apply -f redis-service.yaml

3. 检查 Endpoints 是否正常(确保 Service 能路由到 Pod)


kubectl get endpoints redis

如果 ENDPOINTS 列为空,说明 Service 的
selector
没有匹配到任何 Pod,需检查 label 是否一致。


七、更新 ConfigMap 并触发滚动更新(针对 Deployment)

1. 更新 ConfigMap,添加更多 Redis 配置项

比如设置
bind

port

protected-mode
等:


cat <<EOF > ./redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
data:
  redis-config: |
    bind 0.0.0.0
    port 6379
    protected-mode no
    maxmemory 2mb
    maxmemory-policy allkeys-lru
EOF

2. 应用更新


kubectl apply -f redis-config.yaml

3. 检查 ConfigMap


kubectl describe configmap/redis-config

你将看到完整的配置内容。


八、触发 Deployment 滚动更新(重要!)

Kubernetes 不会因为 ConfigMap 内容变化而自动重新部署 Pod。但我们可以通过以下方法之一触发更新:

方法一:修改 Deployment 的 annotation(推荐简单方式)


kubectl edit deployment redis

找到
annotations:
部分,例如将:


config/last-applied: "v1"

修改为:


config/last-applied: "v2"

保存退出后,Kubernetes 会认为 Pod 模板有变化,从而触发 滚动更新,使用最新的 ConfigMap 数据重新创建 Pod。

方法二:直接修改镜像版本号(或其他字段)也可触发更新


九、验证最终配置(Deployment 方式)

1. 查看当前运行的 Pod


kubectl get pods

记下 Pod 名称,例如:
redis-xxxxx

2. 进入 Redis CLI 检查配置


kubectl exec -it <pod-name> -- redis-cli

例如:


kubectl exec -it redis-5669d68bff-tnzhz -- redis-cli

3. 检查关键配置项


CONFIG GET maxmemory
# 期望:2097152 (2MB)

CONFIG GET maxmemory-policy
# 期望:allkeys-lru

CONFIG GET protected-mode
# 期望:no

CONFIG GET bind
# 期望:0.0.0.0

CONFIG GET port
# 期望:6379

✅ 至此,你已成功通过 Kubernetes ConfigMap + Deployment 实现了 Redis 动态配置管理,并支持安全地更新配置和滚动重启

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容