Openshift Origin ConfigMaps
ConfigMaps 是openshift提供的一种管理配置参数的工具,在容器运行过程中,需要配置很多变量和参数,这些变量和参数可能随时都有变化,因此这些变量不适合保存在容器中,若保存在容器中,需要经常替换镜像,维护成本高;再者,应用使用过程中,需要进行环境的迁移,变量和参数需要备份和移植到别的平台,需要有一种简单、快速的变量和参数的移植方式,这种方式在openshift中就是configMaps。
下面是一个简单的configMaps的配置文件:
kind: ConfigMap
apiVersion: v1
metadata:
creationTimestamp: 2016-02-18T19:14:38Z
name: example-config
namespace: default
data:
example.property.1: hello
example.property.2: world
example.property.file: |-
property.1=value-1
property.2=value-2
property.3=value-3
1. ConfigMaps创建
1) 基于目录创建
假设有目录example-files,目录下有如下文件:
$ ls example-files
game.properties
ui.properties
$ cat example-files/game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
$ cat example-files/ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
针对上述目录创建和查看的操作如下:
# oc create configmap game-config --from-file=example-files/
configmap "game-config" created
# oc describe configmaps game-config
Name: game-config
Namespace: configmaps
Labels: <none>
Annotations: <none>
Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
Events: <none>
# oc get configmaps game-config -o yaml
apiVersion: v1
data:
game.properties: |
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
creationTimestamp: 2018-06-07T11:56:50Z
name: game-config
namespace: configmaps
resourceVersion: "3882446"
selfLink: /api/v1/namespaces/configmaps/configmaps/game-config
uid: dcee28c2-6a49-11e8-8b2d-002590c89d6e
2) 基于文件创建
# oc create configmap game-config-2 --from-file=example-files/game.properties --from-file=example-files/ui.properties
configmap "game-config-2" created
# oc get configmaps game-config-2 -o yaml
apiVersion: v1
data:
game.properties: |
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
creationTimestamp: 2018-06-07T12:00:06Z
name: game-config-2
namespace: configmaps
resourceVersion: "3882833"
selfLink: /api/v1/namespaces/configmaps/configmaps/game-config-2
uid: 51949ecd-6a4a-11e8-8b2d-002590c89d6e
3) 基于key-value的文件创建
# oc create configmap game-config-3 --from-file=game-special-key=example-files/game.properties
configmap "game-config-3" created
# oc get configmaps game-config-3 -o yaml
apiVersion: v1
data:
game-special-key: |
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
kind: ConfigMap
metadata:
creationTimestamp: 2018-06-07T12:04:20Z
name: game-config-3
namespace: configmaps
resourceVersion: "3883335"
selfLink: /api/v1/namespaces/configmaps/configmaps/game-config-3
uid: e9462ee8-6a4a-11e8-8b2d-002590c89d6e
4) 基于文字值的创建
# oc create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap "special-config" created
# oc get configmaps special-config -o yaml
apiVersion: v1
data:
special.how: very
special.type: charm
kind: ConfigMap
metadata:
creationTimestamp: 2018-06-07T12:07:58Z
name: special-config
namespace: configmaps
resourceVersion: "3883764"
selfLink: /api/v1/namespaces/configmaps/configmaps/special-config
uid: 6b087a3b-6a4b-11e8-8b2d-002590c89d6e
5) 基于yaml文件创建
# cat env-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: env-config
namespace: configmaps
data:
log_level: INFO
# oc create -f env-config.yaml
configmap "env-config" created
# cat special-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: configmaps
data:
special.how: very
special.type: charm
# oc create -f special-config.yaml
configmap "special-config" created
# oc get configmaps
NAME DATA AGE
env-config 1 7m
special-config 2 7m
2. ConfigMaps 使用
1) 配置环境变量
# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: docker.io/busybox:latest
command: [ "/bin/sh", "-c", "env" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
optional: true
envFrom:
- configMapRef:
name: env-config
restartPolicy: Never
# oc create -f pod.yaml
pod "dapi-test-pod" created
# oc get pods
NAME READY STATUS RESTARTS AGE
dapi-test-pod 0/1 Completed 0 29s
# oc logs dapi-test-pod
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://172.30.0.1:443
HOSTNAME=dapi-test-pod
SHLVL=1
HOME=/root
KUBERNETES_PORT_53_TCP_ADDR=172.30.0.1
KUBERNETES_PORT_53_UDP_ADDR=172.30.0.1
KUBERNETES_PORT_53_TCP_PORT=53
KUBERNETES_PORT_53_TCP_PROTO=tcp
SPECIAL_TYPE_KEY=charm
KUBERNETES_PORT_53_UDP_PORT=53
KUBERNETES_PORT_53_UDP_PROTO=udp
KUBERNETES_SERVICE_PORT_DNS=53
KUBERNETES_PORT_53_TCP=tcp://172.30.0.1:53
KUBERNETES_PORT_443_TCP_ADDR=172.30.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_53_UDP=udp://172.30.0.1:53
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
SPECIAL_LEVEL_KEY=very
KUBERNETES_SERVICE_PORT_DNS_TCP=53
KUBERNETES_PORT_443_TCP=tcp://172.30.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_HOST=172.30.0.1
PWD=/
发现环境变量SPECIAL_TYPE_KEY 和SPECIAL_LEVEL_KEY
2) Volume 使用configmaps
# oc get configmaps special-config -o yaml
apiVersion: v1
data:
special.how: very
special.type: charm
kind: ConfigMap
metadata:
creationTimestamp: 2018-06-07T12:11:55Z
name: special-config
namespace: configmaps
resourceVersion: "3884236"
selfLink: /api/v1/namespaces/configmaps/configmaps/special-config
uid: f873c69b-6a4b-11e8-8b2d-002590c89d6e
# cat volumepod.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: docker.io/busybox:latest
command: [ "/bin/sh", "cat", "/etc/config/special.how" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never
# oc create -f volumepod.yaml
pod "dapi-test-pod" created