博客原文
IP | Host | 配置 |
---|---|---|
11.0.1.150 | master1 (keepalived+haproxy) | 2C 4G 30G |
11.0.1.151 | master2 (keepalived+haproxy) | 2C 4G 30G |
11.0.1.152 | node1 | 2C 4G 30G |
vip 地址: https://11.0.1.100:16443
redis-configmap.yaml:
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-configmap
data:
redis-config: ""
创建
$ kubectl create -f redis-configmap.yaml
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
使用 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"
$ kubectl edit cm redis-configmap
redis-configmap
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-configmap
data:
redis-config: |
maxmemory 2mb
maxmemory-policy allkeys-lru
确认 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
- 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/
- https://kubernetes.io/zh-cn/docs/tutorials/configuration/configure-redis-using-configmap/#real-world-example-configuring-redis-using-a-configmap