Jimmy的文件: openshift origin ConfigMaps
阿新 • • 發佈:2018-12-31
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_KEY2) 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