$ git clone https://github.com/liuxiaoyu-git/configmap
$ cat ./configmap/color.properties
color.good=green
color.bad=red
$ cat ./configmap/url.properties
url.google=www.google.com
url.redhat=www.redhat.com
$ oc new-project USER-ID-config-env
$ oc create configmap config1 --from-file=./configmap/color.properties --from-file=./configmap/url.properties
$ oc create configmap config2 --from-env-file=./configmap/url.properties
$ oc get configmap config1 -o yaml
apiVersion: v1
data:
color.properties: "color.good=green\r\ncolor.bad=red"
url.properties: "url.google=www.google.com\r\nurl.redhat=www.redhat.com"
kind: ConfigMap
$ oc get configmap config2 -o yaml
apiVersion: v1
data:
url.google: www.google.com
url.redhat: www.redhat.com
kind: ConfigMap
$ cat ./configmap/configmap-test.yaml
apiVersion: v1
kind: Pod
metadata:
name: configmap-test
spec:
containers:
- name: test-container
image: docker.io/busybox:latest
command: [ "/bin/sh", "-c", "env" ]
env:
- name: REDHAT_URL
valueFrom:
configMapKeyRef:
name: config1
key: url.properties
envFrom:
- configMapRef:
name: config2
restartPolicy: Never
$ oc create -f ./configmap/configmap-test.yaml
$ oc logs pod/configmap-test
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://172.30.0.1:443
url.redhat=www.redhat.com
HOSTNAME=configmap-test
SHLVL=1
HOME=/root
url.google=www.google.com
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=172.30.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
REDHAT_URL=url.google=www.google.com
url.redhat=www.redhat.com
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://172.30.0.1:443
KUBERNETES_SERVICE_HOST=172.30.0.1
PWD=/
本实验使用Env环境变量向DeploymentConfig中传递参数。
$ oc new-app https://github.com/liuxiaoyu-git/PrintEnv
$ oc expose svc printenv
$ oc get pod
NAME READY STATUS RESTARTS AGE
printenv-1-8sb4n 1/1 Running 0 58s
printenv-1-build 0/1 Completed 0 2m28s
printenv-1-deploy 0/1 Completed 0 66s
$ oc set env dc/printenv --list
# deploymentconfigs/printenv, container printenv
$ curl $(oc get route printenv|awk '{print $2}'|grep printenv) | jq -S
{
"APP_ROOT": "/opt/app-root",
"DEBUG_PORT": "5858",
"DESCRIPTION": "Node.js 10 available as container is a base platform for building and running various Node.js 10 applications and frameworks. Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.",
"DEV_MODE": "false",
"HOME": "/opt/app-root/src",
"HOSTNAME": "printenv-1-8sb4n",
"INIT_CWD": "/opt/app-root/src",
"KUBERNETES_PORT": "tcp://172.30.0.1:443",
"KUBERNETES_PORT_443_TCP": "tcp://172.30.0.1:443",
"KUBERNETES_PORT_443_TCP_ADDR": "172.30.0.1",
"KUBERNETES_PORT_443_TCP_PORT": "443",
"KUBERNETES_PORT_443_TCP_PROTO": "tcp",
"KUBERNETES_SERVICE_HOST": "172.30.0.1",
"KUBERNETES_SERVICE_PORT": "443",
"KUBERNETES_SERVICE_PORT_HTTPS": "443",
"LD_LIBRARY_PATH": "/opt/rh/rh-nodejs10/root/usr/lib64",
"LD_PRELOAD": "libnss_wrapper.so",
"MANPATH": "/opt/rh/rh-nodejs10/root/usr/share/man:",
"NAME": "nodejs",
。。。。
$ oc set env dc/printenv APP_VAR_1=Value1 APP_VAR_2=Value2
deploymentconfig.apps.openshift.io/printenv updated
$ oc get pod
NAME READY STATUS RESTARTS AGE
printenv-1-build 0/1 Completed 0 10m
printenv-1-deploy 0/1 Completed 0 9m14s
printenv-2-deploy 0/1 Completed 0 41s
printenv-2-mn9np 1/1 Running 0 32s
$ oc set env dc/printenv --list
# deploymentconfigs/printenv, container printenv
APP_VAR_1=Value1
APP_VAR_2=Value2
$ curl $(oc get route printenv|awk '{print $2}'| grep printenv) | jq -S
{
"APP_ROOT": "/opt/app-root",
"APP_VAR_1": "Value1",
"APP_VAR_2": "Value2",
"DEBUG_PORT": "5858",
"DESCRIPTION": "Node.js 10 available as container is a base platform for building and running various Node.js 10 applications and frameworks. Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.",
"DEV_MODE": "false",
"HOME": "/opt/app-root/src",
"HOSTNAME": "printenv-2-mn9np",
"INIT_CWD": "/opt/app-root/src",
"KUBERNETES_PORT": "tcp://172.30.0.1:443",
"KUBERNETES_PORT_443_TCP": "tcp://172.30.0.1:443",
"KUBERNETES_PORT_443_TCP_ADDR": "172.30.0.1",
"KUBERNETES_PORT_443_TCP_PORT": "443",
。。。。
$ oc set env dc/printenv APP_VAR_2-
本实验将ConfigMap配置参数放在容器能访问的目录中。
$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/app-promotion-configmap/master/example1/configmap-example.json
configmap/config created
$ oc get configmap config -o yaml
apiVersion: v1
data:
message: Hello World!
ui.properties: color=blue
kind: ConfigMap
$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/app-promotion-configmap/master/example1/node-app-deployment.json
$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/app-promotion-configmap/master/example1/node-app-build.json
$ oc get pod
NAME READY STATUS RESTARTS AGE
node-app-1-5fg5p 1/1 Running 0 50s
node-app-1-build 0/1 Completed 0 2m32s
node-app-1-deploy 0/1 Completed 0 59s
$ curl $(oc get route node-app|awk '{print $2}'| grep node-app)
<html><head><title></title></head><body bgcolor="blue"><h1>Hello world!</h1></body>
var properties = PropertiesReader('/etc/node-app/node-app.config');
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(' ');
res.write('+ properties.get('color') + '">');
res.write(''
+ process.env.BACKGROUND_MSG + '');
$ oc get pod
NAME READY STATUS RESTARTS AGE
node-app-1-build 0/1 Completed 0 52m
node-app-1-deploy 0/1 Completed 0 51m
node-app-2-deploy 0/1 Completed 0 13m
node-app-2-z58mx 1/1 Running 0 13m
$ oc rsh node-app-2-z58mx
sh-4.2$ ls /etc/node-app
node-app.config node-app1.config
sh-4.2$ cat /etc/node-app/node-app1.config
color=blue
$ oc new-project USER-ID-dev-node-app
$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/app-promotion-configmap/master/example2/configmap-dev.json
$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/app-promotion-configmap/master/example2/node-app-deployment.json
$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/app-promotion-configmap/master/example2/node-app-build.json
$ oc logs -f bc/node-app
$ curl $(oc get route node-app|awk '{print $2}'| grep node-app)
<html><head><title></title></head><body bgcolor="green"><h1>This application is running in: DEVELOPMENT</h1></body>
4. 用USER-ID-test登录,然后执行以下命令,在USER-ID-test-node-app项目中创建configmap-test和部署node-app应用。注意:由于测试USER-ID-test-node-app项目中还没有node-app镜像,因此即便可通过route访问页面,但也无法显示。
$ oc new-project USER-ID-test-node-app
$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/app-promotion-configmap/master/example2/configmap-test.json
$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/app-promotion-configmap/master/example2/node-app-deployment.json
$ oc policy add-role-to-user view USER-ID-test -n USER-ID-dev-node-app
$ oc policy add-role-to-user system:image-puller USER-ID-test -n USER-ID-dev-node-app
$ oc get project
NAME DISPLAY NAME STATUS
user1-dev-node-app Active
user1-test-node-app Active
$ oc project USER-ID-test-node-app
$ oc get is
NAME IMAGE REPOSITORY TAGS UPDATED
node-app default-route-openshift-image-registry.apps.cluster-beijing-ed5f.beijing-ed5f.example.opentlc.com/user1-test-node-app/node-app
oc tag USER-ID-dev-node-app/node-app USER-ID-test-node-app/node-app:latest
$ curl $(oc get route node-app|awk '{print $2}'| grep node-app)
<html><head><title></title></head><body bgcolor="red"><h1>This application is running in: TEST</h1></body>