ConifgMap
kubernetes通过ConfigMap来实现对容器中应用的配置管理,所有的配置内容都存储在etcd中。
创建ConfigMap
创建ConfigMap的方式有两种,一种是通过yaml文件来创建,另一种是通过kubectl直接在命令行下创建。
- 使用目录创建
ls configmap
game.properties
ui.properties
//文件信息
cat configmap/game.properties
enemies=aliens
livese=3
emenies.cheat=true
secret.code.allowed=true
cat configmap/ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
//根据目录创建,包含目录中的所有配置文件
kubectl create configmap game-config --from-file=./configmap
//查看
[root@k8s-master xhyan]# kubectl get configmap
NAME DATA AGE
game-config 2 21s
//查看具体信息
[root@k8s-master xhyan]# kubectl get configmap game-config -o yaml
apiVersion: v1
data:
game.properties: |
enemies=aliens
livese=3
emenies.cheat=true
secret.code.allowed=true
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
kind: ConfigMap
metadata:
creationTimestamp: "2020-08-31T05:19:46Z"
name: game-config
namespace: default
resourceVersion: "370183"
selfLink: /api/v1/namespaces/default/configmaps/game-config
uid: c011a3dd-9bab-4255-bd95-3e24a1aa4b7c
- 使用文件创建(与目录方法一样,指定的是文件名)
[root@k8s-master xhyan]# kubectl create cm game-config2 --from-file=configmap/game.properties
configmap/game-config2 created
[root@k8s-master xhyan]# kubectl get cm
NAME DATA AGE
game-config 2 7m53s
game-config2 1 8s
kubectl get方式查看方式查看
[root@k8s-master xhyan]# kubectl get cm game-config2 -o yaml
apiVersion: v1
data:
game.properties: |
enemies=aliens
livese=3
emenies.cheat=true
secret.code.allowed=true
kind: ConfigMap
metadata:
creationTimestamp: "2020-08-31T05:27:31Z"
name: game-config2
namespace: default
resourceVersion: "370919"
selfLink: /api/v1/namespaces/default/configmaps/game-config2
uid: 1a088ce6-94ed-4c95-89d7-b2a975239c5c
kubectl describe方式查看
[root@k8s-master configmap]# kubectl describe cm game-config
Name: game-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
game.properties:
----
enemies=aliens
livese=3
emenies.cheat=true
secret.code.allowed=true
Events: <none>
- kubectl直接在命令行下创建。
使用--from-literal参数传递配置信息,该参数可以使用多次
kubectl create configmap test-config3 --from-literal=db.host=10.5.10.116 --from-literal=db.port='3306'
configmap/test-config3 created
[root@k8s-master xhyan]# kubectl get cm test-config3 -o yaml
apiVersion: v1
data:
db.host: 10.5.10.116
db.port: "3306"
kind: ConfigMap
metadata:
creationTimestamp: "2020-08-31T05:31:27Z"
name: test-config3
namespace: default
resourceVersion: "371292"
selfLink: /api/v1/namespaces/default/configmaps/test-config3
uid: 4ec05371-b09c-4779-a5a1-3b1abafae1c5
- yaml格式文件创建
yaml资源清单使用kubectl apply 方法创建
cat test-cfg.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test-cfg
namespace: default
data:
cache_host: memcached-gcxt
cache_port: "11211"
cache_prefix: gcxt
my.cnf: |
[mysqld]
log-bin = mysql-bin
app.properties: |
property.1 = value-1
property.2 = value-2
property.3 = value-3
//yaml资源清单方式创建
[root@k8s-master configmap]# kubectl apply -f test-cfg.yaml
configmap/test-cfg created
[root@k8s-master configmap]# kubectl get cm test-cfg -o yaml
apiVersion: v1
data:
test-cfg.yaml: |
apiVersion: v1
kind: ConfigMap
metadata:
name: test-cfg
namespace: default
data:
cache_host: memcached-gcxt
cache_port: "11211"
cache_prefix: gcxt
my.cnf: |
[mysqld]
log-bin = mysql-bin
app.properties: |
property.1 = value-1
property.2 = value-2
property.3 = value-3
kind: ConfigMap
metadata:
creationTimestamp: "2020-08-31T05:38:13Z"
name: test-cfg
namespace: default
resourceVersion: "371938"
selfLink: /api/v1/namespaces/default/configmaps/test-cfg
uid: 940f21f6-1e33-4051-a3d1-ecd69ce6d933
使用ConfigMap
使用ConfigMap有三种方式,一种是通过环境变量的方式,直接传递pod,另一种是使用volume的方式挂载入到pod内
- 通过环境变量的方式,直接传递pod
ConfigMap文件:
cat special-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm
pod示例:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: [ "/bin/sh", "-c", "env" ]
env:
- name: SPECIAL_LEVEL_KEY //将special-config下的special.how映射为SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
restartPolicy: Never
- 使用volume将ConfigMap作为文件或目录直接挂载,其中每一个key-value键值对都会生成一个文件,key为文件名,value为内容
第一个pod示例,简单的将上面创建的ConfigMap直接挂载至pod的/etc/config目录下:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: [ "/bin/sh", "-c", "cat /etc/config/special.how" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never
第二个pod示例,只将ConfigMap的special.how这个key挂载到/etc/config目录下的一个相对路径path/to/special-key,如果存在同名文件,直接覆盖。其他的key不挂载:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: [ "/bin/sh","-c","cat /etc/config/path/to/special-key" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
items:
- key: special.how
path: path/to/special-key
restartPolicy: Never
最后需要说明两点:
- ConfigMap必须在Pod之前创建
- ConfigMap必须和pod在同一个namespace内才能使用,也就是说,ConfigMap不能跨命名空间调用。