OpenShift 4 Hands-on Lab (8) 使用配置参数和环境变量

文章目录

  • 使用ConfigMap配置参数
  • 使用Env环境变量
  • 通过Volume使用ConfigMap配置参数
  • 结合应用发布,在不同的应用运行环境中使用不同的参数配置

使用ConfigMap配置参数

  1. 将github中的资源下载到本地。然后查看两个properties配置文件的内容。
$ 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
  1. 执行以下命令,创建名为config1和config2的ConfigMap,其中config1采用“–from-file”方法(可以带多个文件参数),而config2采用“-from-env-file”方法(只能是一个文件参数)。
$ 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
  1. 执行以下命令查看config1和config2在“data”区域的区别。
$ 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
  1. 查看configmap-test.yaml文件,它定义了一个访问配置参数并打印所有环境变量的的Pod。然后根据它创建一个Pod。
$ 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
  1. 查看configmap-test的log。注意打印的环境变量中url.google、url.redhat和REDHAT_URL的内容。
$ 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环境变量

本实验使用Env环境变量向DeploymentConfig中传递参数。

  1. 部署测试应用,该应用会将环境变量全部打印出来。
$ oc new-app https://github.com/liuxiaoyu-git/PrintEnv
$ oc expose svc printenv
  1. 等到Pod运行起来,然后查看当前为dc/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
  1. 查看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",
。。。。
  1. 执行命令,为dc/printenv设置2个环境变量:APP_VAR_1和APP_VAR_2
$ oc set env dc/printenv APP_VAR_1=Value1 APP_VAR_2=Value2
deploymentconfig.apps.openshift.io/printenv updated
  1. 查看pod的状态,可以看到OpenShift会启动一个新的部署printenv-2-deploy,它部署将运行的pod从printenv-1-8sb4n更新为printenv-2-mn9np。
$ 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
  1. 再次查看dc/printenv的环境变量,可以看到已经有APP_VAR_1和APP_VAR_2变量了。
$ oc set env dc/printenv --list
# deploymentconfigs/printenv, container printenv
APP_VAR_1=Value1
APP_VAR_2=Value2
  1. 再次查看应用页面输出,可以看到已经有APP_VAR_1和APP_VAR_2变量了。
$ 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",
。。。。
  1. 执行命令,删除APP_VAR_2环境参数,然后再执行上一步验证。
$ oc set env dc/printenv APP_VAR_2-

通过Volume使用ConfigMap配置参数

本实验将ConfigMap配置参数放在容器能访问的目录中。

  1. 创建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
  1. 创建Node-App应用相关资源 。
$ 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
  1. 查看node-app应用返回,其中页面的“bgcolor”设为“blue”颜色。
$ 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>
  1. 查看部署的应用页面的NodeJS源文件,https://raw.githubusercontent.com/liuxiaoyu-git/app-promotion-configmap/master/node-app/server.js,其中以下第一行是读取配置,最后二行是根据node-app.config中的内容设置背景,最后一行是根据env显示“Hello World”。
      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 + '

'
);
  1. 在OpenShift控制台的Developer视图中进入到Search菜单,在右侧Search下方选出DeploymentConfig类型,然后点击nodeapp一行右侧的图标,在菜单中进入“Edit Deployment Config”。
    OpenShift 4 Hands-on Lab (8) 使用配置参数和环境变量_第1张图片
  2. 在node-app的DeploymentConfig找到以下部分,其中下图中三部分分别定义了configmap(颜色)、env(文字)、volumemount。
    OpenShift 4 Hands-on Lab (8) 使用配置参数和环境变量_第2张图片
  3. 修改node-app的DeploymentConfig的items配置,修改完为下图。最后保存修改。
    OpenShift 4 Hands-on Lab (8) 使用配置参数和环境变量_第3张图片
  4. OpenShift会自动根据新的DeploymentConfig配置重新部署应用。可以查看pod状态,直到node-app-2-z58mx的pod运行起来。
$ 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
  1. 进入node-app-2-z58mx的pod内部,然后查看容器中的“/etc/node-app”目录中已经有2个文件,最后查看“node-app1.config”文件中的内容。
$ 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
  1. 按照上述(4)的方法进入node-app的DeploymentConifg的Overview,可以在下方可以看到下图的Volumes配置。
    OpenShift 4 Hands-on Lab (8) 使用配置参数和环境变量_第4张图片
  2. 点击上图中的“config”,然后可以在ConfigMap的Overview的下方可以看到名为config的ConfigMap中的内容。
    OpenShift 4 Hands-on Lab (8) 使用配置参数和环境变量_第5张图片
  3. 回到以上(9)的界面,然后进入node-app的DeploymentConifg的Environment。可以看到“BACKGROUND_MSG”环境变量被设为名为config的ConfigMap中的message参数。
    OpenShift 4 Hands-on Lab (8) 使用配置参数和环境变量_第6张图片

结合应用发布,在不同的应用运行环境中使用不同的参数配置

  1. 根据《OpenShift 4 之增加管理员用户》新建两个用户USER-ID-dev和USER-ID-test。
  2. 用USER-ID-dev登录,然后执行以下命令,在USER-ID-dev-node-app项目中创建configmap-dev和部署node-app应用。
$ 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
  1. 访问node-app应用,确认bgcolor为green,页面内容“This application is running in: DEVELOPMENT”。
$ 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
  1. 使用管理员用户执行以下命令,让tester-USER-ID用户能够看到node-app-dev-USER-ID项目并能从中“拉镜像”
$ 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
  1. 用USER-ID-test用户登录,然后查看项目列表。可以发现该用户能同时看到USER-ID-dev-node-app和USER-ID-test-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
  1. 执行命令,查看ImageStream。确认可以看到名为node-app的ImageStream.
$ 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
  1. 用USER-ID-test用户执行以下命令,将USER-ID-dev-node-app-项目中的镜像拉到USER-ID-test-node-app项目中。
oc tag USER-ID-dev-node-app/node-app USER-ID-test-node-app/node-app:latest
  1. 访问USER-ID-test-node-app项目node-app应用的Route对应的页面,将显示下图,说明该环境的应用使用了红色背景配置文件。
$ 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>

在这里插入图片描述

你可能感兴趣的:(OpenShift,4,Dev,openshift,configmap,env)