Skip to content

1. Redis结合ConfigMaps

使⽤ Redis 配置的值创建⼀个 ConfigMap⽂档

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

1.1 创建ConfigMap

yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-configmap
data:
  redis-config: |
    bind 0.0.0.0
    port 6379
---
apiVersion: v1
kind: Pod
metadata:
  name: redis-pod
spec:
  volumes:
  - name: redis-cm  #定义名字,在volumemount中使用
    configMap:
      name: redis-configmap
  containers:
  - name: redis
    image: redis:latest
    command: ["redis-server", "/redis-master/redis.conf"]
    env:
    - name: MASTER   # redis 会自动读取环境变量
      value: "true"
    ports:
    - containerPort: 6379
    volumeMounts:
    - name: redis-cm  #挂载的名字
      mountPath: /redis-master/redis.conf  #挂载到pod中的名字
      subPath: redis-config  #configmap中key
      readOnly: true
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-configmap
data:
  redis-config: |
    bind 0.0.0.0
    port 6379
---
apiVersion: v1
kind: Pod
metadata:
  name: redis-pod
spec:
  volumes:
  - name: redis-cm  #定义名字,在volumemount中使用
    configMap:
      name: redis-configmap
  containers:
  - name: redis
    image: redis:latest
    command: ["redis-server", "/redis-master/redis.conf"]
    env:
    - name: MASTER   # redis 会自动读取环境变量
      value: "true"
    ports:
    - containerPort: 6379
    volumeMounts:
    - name: redis-cm  #挂载的名字
      mountPath: /redis-master/redis.conf  #挂载到pod中的名字
      subPath: redis-config  #configmap中key
      readOnly: true
  • 执行
bash
kubectl apply -f redis-config.yaml
kubectl apply -f redis-config.yaml

1.2 检验redis

bash
[root@kube-master redis_example]# kubectl exec -it redis-pod -- redis-cli
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "0"
[root@kube-master redis_example]# kubectl exec -it redis-pod -- redis-cli
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "0"
  • 修改port
yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-configmap
data:
  redis-config: |
    bind 0.0.0.0
    port 9379
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-configmap
data:
  redis-config: |
    bind 0.0.0.0
    port 9379
  • 删除pod
bash
kubectl delete -f redis-config.yaml

#重新创建
kubectl apply -f redis-config.yaml

#再次验证
[root@kube-master redis_example]# kubectl exec -it redis-pod -- redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>

[root@kube-master redis_example]# kubectl exec -it redis-pod -- redis-cli -p 9379
127.0.0.1:9379> config get maxclients
1) "maxclients"
2) "10000"
kubectl delete -f redis-config.yaml

#重新创建
kubectl apply -f redis-config.yaml

#再次验证
[root@kube-master redis_example]# kubectl exec -it redis-pod -- redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>

[root@kube-master redis_example]# kubectl exec -it redis-pod -- redis-cli -p 9379
127.0.0.1:9379> config get maxclients
1) "maxclients"
2) "10000"
  • 清理环境
bash
kubectl delete -f redis-config.yaml
kubectl delete -f redis-config.yaml