kubernetes 1.2 新增加ConfigMap

一般在使用docker images的时候都会把conf文件打包到image中,如果修改配置的时候则需要重新打包整个images,非常不方便,在kubernetes1.2版本中引入了configMap功能,支持实时更新可以很方便解决这个问题,毕竟官方的方案使用起来比较方便。

官方样例:使用configMap功能加载redis的配置

1. 新建立redis-config 的文件 :

[source language=”cpp”]
$cat redis-config
maxmemory 2mb
maxmemory-policy allkeys-lru
[/source]

2. 建立k8s的配置文件

[source language=”cpp”]
$kubectl create configmap example-redis-config –from-file=redis-config
$kubectl get configmap example-redis-config -o yaml
apiVersion: v1
data:
redis-config: |
maxmemory 2mb
maxmemory-policy allkeys-lru
kind: ConfigMap
metadata:
creationTimestamp: 2016-03-30T18:14:41Z
name: example-redis-config
namespace: default
resourceVersion: "24686"
selfLink: /api/v1/namespaces/default/configmaps/example-redis-config
uid: 460a2b6e-f6a3-11e5-8ae5-42010af00002
[/source]

3. 创建redis-pod.yaml并加载到redis配置文件的目录

[source language=”cpp”]
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
– name: redis
image: index.tenxcloud.com/tenxcloud/redis
env:
– name: MASTER
value: "true"
ports:
– containerPort: 6379
resources:
limits:
cpu: "0.1"
volumeMounts:
– mountPath: /data
name: data
– mountPath: /etc/redis/
name: config
volumes:
– name: data
emptyDir: {}
– name: config
configMap:
name: example-redis-config
items:
– key: redis-config
path: redis_default.conf
[/source]

4. 使用redis-cli验证

[source language=”cpp”]
$ kubectl exec -it redis redis-cli
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "2097152"
127.0.0.1:6379> CONFIG GET maxmemory-policy
1) "maxmemory-policy"
2) "allkeys-lru"
[/source]

5. 已知限制:同一个namespace内可以使用

6. 如果热更新,可以先删除再重新创建,那么在容器内的配置会同步更新。

kubectl delete configmap example-redis-config 
kubectl create configmap --from-file=redis-config-new

官网文档地址: http://kubernetes.io/docs/user-guide/configmap/

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注