ConfigMap 抽取应用配置,并且可以自动更新 1、redis示例 1、把之前的配置文件创建为配置集
kubectl create cm redis-conf --from-file=redis.conf
[root@bigdata01 k8s]
[root@bigdata01 k8s]
appendonly yes
[root@bigdata01 k8s]
configmap/redis-conf created
[root@bigdata01 k8s]
NAME DATA AGE
kube-root-ca.crt 1 10d
redis-conf 1 7s
[root@bigdata01 k8s]
apiVersion: v1
data:
redis.conf: |
appendonly yes
kind: ConfigMap
metadata:
creationTimestamp: "2022-03-05T07:47:52Z"
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:data:
.: {}
f:redis.conf: {}
manager: kubectl-create
operation: Update
time: "2022-03-05T07:47:52Z"
name: redis-conf
namespace: default
resourceVersion: "195660"
uid: 187c182d-eedc-49da-b498-207a9ffbed3e
精简一下就是这样啦~
apiVersion: v1
data:
redis.conf: |
appendonly yes
kind: ConfigMap
metadata:
name: redis-conf
namespace: default
创建pod
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis
command:
- redis-server
- "/redis-master/redis.conf"
ports:
- containerPort: 6379
volumeMounts:
- mountPath: /data
name: data
- mountPath: /redis-master
name: config
volumes:
- name: data
emptyDir: {}
- name: config
configMap:
name: redis-conf
items:
- key: redis.conf
path: redis.conf
[root@bigdata01 k8s]
[root@bigdata01 k8s]
calico.yaml dash.yaml images.sh ingress.yaml origin_store.yaml redis.conf redis.yaml
[root@bigdata01 k8s]
pod/redis created
[root@bigdata01 k8s]
[root@bigdata01 k8s]
configmap/redis-conf edited
[root@bigdata01 k8s]
pod/redis created
修改ConfigMap
kubectl exec -it redis -- redis-cli
127.0.0.1:6379> CONFIG GET appendonly
127.0.0.1:6379> CONFIG GET requirepass
检查配置是否更新
kubectl exec -it redis -- redis-cli
127.0.0.1:6379> CONFIG GET maxmemory
127.0.0.1:6379> CONFIG GET maxmemory-policy
检查指定文件内容是否已经更新 修改了CM。Pod里面的配置文件会跟着变
配置值未更改,因为需要重新启动 Pod 才能从关联的 ConfigMap 中获取更新的值。 原因:我们的Pod部署的中间件自己本身没有热更新能力
|