尝试进行Kubernetes 1.10教程(ConfigMap)的操作

首先

在Kubernetes的官方网页上,有许多教程可供参考。
作为学习Kubernetes的一部分,我们将深入研究教程。
这次的目标是ConfigMap。
https://kubernetes.io/docs/tutorials/configuration/configure-redis-using-configmap/

另外,我们使用了截至2018年4月时最新的Kubernetes版本1.10。

ConfigMap是什么

为了提高容器的可移植性,ConfigMap可以将配置信息从容器镜像中分离出来。在本教程中,我们将创建ConfigMap并探讨如何在Pod中使用它。

创建ConfigMap

创建 ConfigMap 有三种方法可供选择:

kubectl create -f で作成。YAMLマニフェストファイルを作成する必要がある。

kubectl create configmap で作成。key-valueを定義したファイルのみで作成が出来る。

kubectl create configmap –from-literalで作成。コマンドの引数で直接 key-value を与える

1-1. 使用YAML清单文件创建

我将创建以下的清单文件。

cat <<'EOF' > /root/kube_yaml/config_files/game-config.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:
  name: game-config
EOF

创建ConfigMap。

[root@sugi-kubernetes110-master01 config_files]# kubectl create -f /root/kube_yaml/config_files/game-config.yaml
configmap "game-config" created

查看ConfigMap列表

[root@sugi-kubernetes110-master01 config_files]# kubectl get configmap 
NAME          DATA      AGE
game-config   2         13s

查看创建的ConfigMap的详细信息

[root@sugi-kubernetes110-master01 config_files]# kubectl describe configmap game-config 
Name:         game-config
Namespace:    default
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>

为了进行下一次确认,将其删除。

[root@sugi-kubernetes110-master01 config_files]# kubectl delete configmap game-config 
configmap "game-config" deleted

从目录中创建

创建一个目录

/root/kube_yaml/config_files/temp_dir

在已创建的目录中创建2个文件。

cat <<'EOF' > /root/kube_yaml/config_files/temp_dir/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
EOF
cat <<'EOF' > /root/kube_yaml/config_files/temp_dir/ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
EOF

在temp_dir目录下创建了两个文件。

[root@sugi-kubernetes110-master01 temp_dir]# pwd
/root/kube_yaml/config_files/temp_dir
[root@sugi-kubernetes110-master01 temp_dir]# 
[root@sugi-kubernetes110-master01 temp_dir]# ls -la
total 8
drwxr-xr-x 2 root root  50 Apr 30 15:42 .
drwxr-xr-x 3 root root  46 Apr 30 15:40 ..
-rw-r--r-- 1 root root 158 Apr 30 15:42 game.properties
-rw-r--r-- 1 root root  83 Apr 30 15:42 ui.properties

请指定目录并创建ConfigMap。

[root@sugi-kubernetes110-master01 temp_dir]# kubectl create configmap game-config --from-file=/root/kube_yaml/config_files/temp_dir
configmap "game-config" created

查看 ConfigMap 的列表

[root@sugi-kubernetes110-master01 temp_dir]# kubectl get configmap 
NAME          DATA      AGE
game-config   2         12s

查看已创建的ConfigMap的详细信息。

[root@sugi-kubernetes110-master01 temp_dir]# kubectl describe configmap game-config 
Name:         game-config
Namespace:    default
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>

为了进行下一轮的确认, 我们将删除它

[root@sugi-kubernetes110-master01 temp_dir]# kubectl delete configmap game-config 
configmap "game-config" deleted

从文件中创建

选择一个在2-1中创建的文件,并进行创建。

kubectl create configmap game-config-1 --from-file=/root/kube_yaml/config_files/temp_dir/game.properties

查看ConfigMap的列表。

[root@sugi-kubernetes110-master01 temp_dir]# kubectl get configmap 
NAME            DATA      AGE
game-config-1   1         12s

查看创建的ConfigMap的详细信息。

[root@sugi-kubernetes110-master01 temp_dir]# kubectl describe configmap game-config-1 
Name:         game-config-1
Namespace:    default
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

Events:  <none>

3-1. 通过命令的参数进行创建

kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm

查看创建的ConfigMap的详细信息。

[root@sugi-kubernetes110-master01 temp_dir]# kubectl describe configmap special-config 
Name:         special-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
special.how:
----
very
special.type:
----
charm
Events:  <none>

由于使用”describe”命令不够直观,如果使用”get -o yaml”命令,就可以更清晰地查看。

[root@sugi-kubernetes110-master01 temp_dir]# kubectl get configmap special-config -o yaml
apiVersion: v1
data:
  special.how: very
  special.type: charm
kind: ConfigMap
metadata:
  creationTimestamp: 2018-04-30T06:52:28Z
  name: special-config
  namespace: default
  resourceVersion: "194751"
  selfLink: /api/v1/namespaces/default/configmaps/special-config
  uid: 0c220c18-4c43-11e8-baee-0050569817ee

为了进行下一次确认,我会删除。

kubectl delete configmap special-config

在Pod的环境变量中使用ConfigMap。

在不使用ConfigMap的情况下,查看容器内的环境变量。

首先,创建一个不附加 ConfigMap 的 BusyBox,将容器内的环境变量输出到标准输出,并通过 `kubectl logs` 命令进行确认。

cat <<'EOF' > /root/kube_yaml/config_files/noenv_pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
  restartPolicy: Never
EOF

创建Pod

kubectl create -f /root/kube_yaml/config_files/noenv_pod.yaml

确认已完成

[root@sugi-kubernetes110-master01 ~]# kubectl get pod -o wide
NAME            READY     STATUS      RESTARTS   AGE       IP            NODE
dapi-test-pod   0/1       Completed   0          2m        10.244.1.13   sugi-kubernetes110-node01.localdomain

使用kubectl logs命令,检查容器内的环境变量。

[root@sugi-kubernetes110-master01 ~]# kubectl logs dapi-test-pod 
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
HOSTNAME=dapi-test-pod
SHLVL=1
HOME=/root
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
[root@sugi-kubernetes110-master01 ~]# 

删除pod

kubectl delete pod dapi-test-pod

使用一个ConfigMap来验证容器内的环境变量。

创建以下的ConfigMap
我认为将环境变量构成多层结构的方式在应用到Pod上时是很不方便的。

cat <<'EOF' > /root/kube_yaml/config_files/game-config.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:
  name: game-config
EOF

创建 ConfigMap

kubectl create -f /root/kube_yaml/config_files/game-config.yaml

创建一个用于将ConfigMap定义为环境变量(env)的Pod的清单文件。

cat <<'EOF' > /root/kube_yaml/config_files/env_pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        # Define the environment variable
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
              name: game-config
              # Specify the key associated with the value
              key: game.properties
  restartPolicy: Never
EOF

创建Pod

kubectl create -f /root/kube_yaml/config_files/env_pod.yaml

查看Pod列表,并确认状态为“已完成”。

[root@sugi-kubernetes110-master01 ~]# kubectl get pod -o wide
NAME            READY     STATUS      RESTARTS   AGE       IP            NODE
dapi-test-pod   0/1       Completed   0          19s       10.244.2.12   sugi-kubernetes110-node02.localdomain

我会检查日志。

[root@sugi-kubernetes110-master01 ~]# kubectl logs dapi-test-pod 
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=dapi-test-pod
SHLVL=1
HOME=/root
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
SPECIAL_LEVEL_KEY=enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
[root@sugi-kubernetes110-master01 ~]# 

通过指定ConfigMap,可以看到以下的值已经被添加到环境变量中。
由于在pod的manifest文件中指定了SPECIAL_LEVEL_KEY,并与ConfigMap进行了混合,导致输出的环境变量呈现出微妙的状态。只有第一个环境变量以”SPECIAL_LEVEL_KEY=enemies=aliens”的形式存在。

SPECIAL_LEVEL_KEY=enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

我要删除Pod。

kubectl delete pod dapi-test-pod

调整env_pod的环境变量指定方法,通过将spec.containers.env改为spec.containers.envFrom。
在”spec.containers.env”中,需要逐个指定每个环境变量,非常冗长,但使用envFrom可以一次读取多个环境变量。

cat <<'EOF' > /root/kube_yaml/config_files/env_pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
        # Define the environment variable
        - configMapRef:
            # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
            name: game-config
            # Specify the key associated with the value
            # key: game.properties
  restartPolicy: Never
EOF

创建Pod

kubectl create -f /root/kube_yaml/config_files/env_pod.yaml

查看日志

[root@sugi-kubernetes110-master01 ~]# kubectl logs dapi-test-pod 
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
ui.properties=color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
HOSTNAME=dapi-test-pod
SHLVL=1
HOME=/root
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
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1

我只摘录了ConfigMap中定义的部分。
然而,多段配置的部分内容确实变得微妙。

ui.properties=color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
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

停止使用ConfigMap的多层构建,改为以Flat方式定义。

cat <<'EOF' > /root/kube_yaml/config_files/game-config.yaml
apiVersion: v1
data:
  game.properties.enemies: 'aliens'
  game.properties.lives: '3'
  game.properties.enemies.cheat: 'true'
  game.properties.enemies.cheat.level: 'noGoodRotten'
  game.properties.secret.code.passphrase: 'UUDDLRLRBABAS'
  game.properties.secret.code.allowed: 'true'
  game.properties.secret.code.lives: '30'
  ui.properties.color.good: 'purple'
  ui.properties.color.bad: 'yellow'
  ui.properties.allow.textmode: 'true'
  ui.properties.how.nice.to.look: 'fairlyNice'
kind: ConfigMap
metadata:
  name: game-config
EOF

重新创建ConfigMap。

kubectl create -f /root/kube_yaml/config_files/game-config.yaml

重新创建Pod。

kubectl create -f /root/kube_yaml/config_files/env_pod.yaml

我会检查日志。您可以确认它们以良好的方式同时加载。

[root@sugi-kubernetes110-master01 ~]# kubectl logs dapi-test-pod 
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
ui.properties.how.nice.to.look=fairlyNice
HOSTNAME=dapi-test-pod
SHLVL=1
HOME=/root
ui.properties.color.good=purple
game.properties.enemies=aliens
game.properties.lives=3
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
game.properties.secret.code.passphrase=UUDDLRLRBABAS
KUBERNETES_PORT_443_TCP_PROTO=tcp
game.properties.enemies.cheat.level=noGoodRotten
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
ui.properties.allow.textmode=true
game.properties.enemies.cheat=true
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
game.properties.secret.code.lives=30
game.properties.secret.code.allowed=true
ui.properties.color.bad=yellow

删除 Pod

kubectl delete pod dapi-test-pod

使用多个ConfigMap来检查容器内的环境变量。

创建第二个ConfigMap

cat <<'EOF' > /root/kube_yaml/config_files/game-config-2.yaml
apiVersion: v1
data:
  test.env: 'ceder'
kind: ConfigMap
metadata:
  name: game-config-2
EOF

创建 ConfigMap

kubectl create -f /root/kube_yaml/config_files/game-config-2.yaml

修改Pod的清单文件

cat <<'EOF' > /root/kube_yaml/config_files/env_pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
        # Define the environment variable
        - configMapRef:
            name: game-config
        - configMapRef:
            name: game-config-2
  restartPolicy: Never
EOF

我会创建一个Pod。

kubectl create -f /root/kube_yaml/config_files/env_pod.yaml

查看日志
可以看到多个ConfigMap正常加载

[root@sugi-kubernetes110-master01 ~]# kubectl logs dapi-test-pod 
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
ui.properties.how.nice.to.look=fairlyNice
HOSTNAME=dapi-test-pod
SHLVL=1
HOME=/root
ui.properties.color.good=purple
game.properties.lives=3
game.properties.enemies=aliens
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
game.properties.secret.code.passphrase=UUDDLRLRBABAS
KUBERNETES_PORT_443_TCP_PROTO=tcp
test.env=ceder
game.properties.enemies.cheat.level=noGoodRotten
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
ui.properties.allow.textmode=true
game.properties.enemies.cheat=true
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
game.properties.secret.code.lives=30
game.properties.secret.code.allowed=true
ui.properties.color.bad=yellow

更新 ConfigMap

创建Pod的清单文件

cat <<'EOF' > /root/kube_yaml/config_files/env_pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "sleep", "3600" ]
      envFrom:
        # Define the environment variable
        - configMapRef:
            name: game-config
        - configMapRef:
            name: game-config-2
  restartPolicy: Never
EOF

创建Pod

kubectl create -f /root/kube_yaml/config_files/env_pod.yaml

使用bash进行远程登录

kubectl exec -it dapi-test-pod sh

确认环境变量

/ # env
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
ui.properties.how.nice.to.look=fairlyNice
HOSTNAME=dapi-test-pod
SHLVL=1
HOME=/root
ui.properties.color.good=purple
game.properties.enemies=aliens
game.properties.lives=3
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
game.properties.secret.code.passphrase=UUDDLRLRBABAS
KUBERNETES_PORT_443_TCP_PROTO=tcp
test.env=ceder
game.properties.enemies.cheat.level=noGoodRotten
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
ui.properties.allow.textmode=true
game.properties.enemies.cheat=true
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
game.properties.secret.code.lives=30
game.properties.secret.code.allowed=true
ui.properties.color.bad=yellow

修改清单文件
将test.env从ceder更改为sugi

cat <<'EOF' > /root/kube_yaml/config_files/game-config-2.yaml
apiVersion: v1
data:
  test.env: 'sugi'
kind: ConfigMap
metadata:
  name: game-config-2
EOF

应用更改

[root@sugi-kubernetes110-master01 ~]# kubectl apply -f /root/kube_yaml/config_files/game-config-2.yaml
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
configmap "game-config-2" configured

确认

[root@sugi-kubernetes110-master01 ~]# kubectl get configmap game-config-2 -o yaml
apiVersion: v1
data:
  test.env: sugi
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"test.env":"sugi"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"game-config-2","namespace":"default"}}
  creationTimestamp: 2018-04-30T16:47:42Z
  name: game-config-2
  namespace: default
  resourceVersion: "243453"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-2
  uid: 331bb48c-4c96-11e8-baee-0050569817ee

已经创建的Pod的环境变量不会被更改。它仍然保持为ceder的状态。

/ # env
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
ui.properties.how.nice.to.look=fairlyNice
HOSTNAME=dapi-test-pod
SHLVL=1
HOME=/root
ui.properties.color.good=purple
game.properties.enemies=aliens
game.properties.lives=3
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
game.properties.secret.code.passphrase=UUDDLRLRBABAS
KUBERNETES_PORT_443_TCP_PROTO=tcp
test.env=ceder
game.properties.enemies.cheat.level=noGoodRotten
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
ui.properties.allow.textmode=true
game.properties.enemies.cheat=true
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
game.properties.secret.code.lives=30
game.properties.secret.code.allowed=true
ui.properties.color.bad=yellow

将ConfigMap作为Volume挂载到Pod中。

使用一个ConfigMap将文件作为卷挂载到容器内部。

创建Pod的清单文件。
在spec.volumes下定义了一个名为config-volume的configmap卷。
在spec.container.volumeMounts下定义了将其挂载到/etc/config目录下。

cat <<'EOF' > /root/kube_yaml/config_files/env_pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "sleep", "3600" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: game-config
  restartPolicy: Never
EOF

创建Pod

kubectl create -f /root/kube_yaml/config_files/env_pod.yaml

我会执行Busybox的shell,并通过远程登录。

kubectl exec -it dapi-test-pod sh

您可以确认该目录上是否存在文件。

/ # cd /etc/config/
/etc/config # 
/etc/config # ls -la
total 8
drwxrwxrwx    3 root     root          4096 Apr 30 17:04 .
drwxr-xr-x    1 root     root            20 Apr 30 17:04 ..
drwxr-xr-x    2 root     root          4096 Apr 30 17:04 ..2018_04_30_17_04_33.366860462
lrwxrwxrwx    1 root     root            31 Apr 30 17:04 ..data -> ..2018_04_30_17_04_33.366860462
lrwxrwxrwx    1 root     root            30 Apr 30 17:04 game.properties.enemies -> ..data/game.properties.enemies
lrwxrwxrwx    1 root     root            36 Apr 30 17:04 game.properties.enemies.cheat -> ..data/game.properties.enemies.cheat
lrwxrwxrwx    1 root     root            42 Apr 30 17:04 game.properties.enemies.cheat.level -> ..data/game.properties.enemies.cheat.level
lrwxrwxrwx    1 root     root            28 Apr 30 17:04 game.properties.lives -> ..data/game.properties.lives
lrwxrwxrwx    1 root     root            42 Apr 30 17:04 game.properties.secret.code.allowed -> ..data/game.properties.secret.code.allowed
lrwxrwxrwx    1 root     root            40 Apr 30 17:04 game.properties.secret.code.lives -> ..data/game.properties.secret.code.lives
lrwxrwxrwx    1 root     root            45 Apr 30 17:04 game.properties.secret.code.passphrase -> ..data/game.properties.secret.code.passphrase
lrwxrwxrwx    1 root     root            35 Apr 30 17:04 ui.properties.allow.textmode -> ..data/ui.properties.allow.textmode
lrwxrwxrwx    1 root     root            30 Apr 30 17:04 ui.properties.color.bad -> ..data/ui.properties.color.bad
lrwxrwxrwx    1 root     root            31 Apr 30 17:04 ui.properties.color.good -> ..data/ui.properties.color.good
lrwxrwxrwx    1 root     root            37 Apr 30 17:04 ui.properties.how.nice.to.look -> ..data/ui.properties.how.nice.to.look

文件的内容也已经被正确定义

/etc/config # cat game.properties.enemies
aliens/etc/config # 
/etc/config # 
/etc/config # cat game.properties.lives
3/etc/config # 

删除Pod

kubectl delete pod dapi-test-pod

在容器内以文件的形式挂载多个ConfigMap。

修改Pod的清单文件。

cat <<'EOF' > /root/kube_yaml/config_files/env_pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "sleep", "3600" ]
      volumeMounts:
      - name: config-volume1
        mountPath: /etc/config-volume1
      - name: config-volume2
        mountPath: /etc/config-volume2
  volumes:
    - name: config-volume1
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: game-config
    - name: config-volume2
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: game-config-2
  restartPolicy: Never
EOF

创建Pod

kubectl create -f /root/kube_yaml/config_files/env_pod.yaml

使用BusyBox的shell启动一个新的实例,并进行远程登录。

kubectl exec -it dapi-test-pod sh

在/etc目录下,已经创建了config-volume1和config-volume2两个目录。

/etc # pwd
/etc
/etc # 
/etc # ls -la
total 80
drwxr-xr-x    1 root     root            50 Apr 30 17:14 .
drwxr-xr-x    1 root     root            40 Apr 30 17:14 ..
drwxrwxrwx    3 root     root          4096 Apr 30 17:14 config-volume1
drwxrwxrwx    3 root     root            75 Apr 30 17:14 config-volume2
-rw-rw-r--    1 root     root           466 Feb 27  2014 fstab
-rw-rw-r--    1 root     root           344 Feb 27  2014 group
-rw-r--r--    1 root     root            14 Apr 30 17:14 hostname
-rw-r--r--    1 root     root           209 Apr 30 17:14 hosts
drwxrwxr-x    2 root     root            82 May 22  2014 init.d
-rw-rw-r--    1 root     root          1086 May 22  2014 inittab
-rw-rw-r--    1 root     root          1180 Feb 27  2014 inputrc
drwxr-xr-x    2 root     root           123 May 22  2014 iproute2
-rw-rw-r--    1 root     root            21 May 22  2014 issue
-rw-r--r--    1 root     root             0 May 22  2014 ld.so.conf
drwxrwxr-x    2 root     root             6 Feb 27  2014 ld.so.conf.d
lrwxrwxrwx    1 root     root            12 Apr 30 17:14 mtab -> /proc/mounts
drwxrwxr-x    8 root     root           138 May 22  2014 network
-rw-rw-r--    1 root     root           439 Feb 27  2014 nsswitch.conf
-rw-r--r--    1 root     root            95 May 22  2014 os-release
-rw-r--r--    1 root     root           596 Feb 27  2014 passwd
-rw-rw-r--    1 root     root          1836 Feb 27  2014 profile
-rw-rw-r--    1 root     root          2744 Feb 27  2014 protocols
-rw-rw-r--    1 root     root           512 Feb 27  2014 random-seed
-rw-r--r--    1 root     root           115 Apr 30 17:14 resolv.conf
-rw-rw-r--    1 root     root           386 Feb 27  2014 securetty
-rw-rw-r--    1 root     root         10873 Feb 27  2014 services
-rw-------    1 root     root           346 May 22  2014 shadow
/etc # 

我会确认每个目录。

/etc/config-volume1 # pwd
/etc/config-volume1
/etc/config-volume1 # 
/etc/config-volume1 # ls -la
total 8
drwxrwxrwx    3 root     root          4096 Apr 30 17:14 .
drwxr-xr-x    1 root     root            50 Apr 30 17:14 ..
drwxr-xr-x    2 root     root          4096 Apr 30 17:14 ..2018_04_30_17_14_44.399765545
lrwxrwxrwx    1 root     root            31 Apr 30 17:14 ..data -> ..2018_04_30_17_14_44.399765545
lrwxrwxrwx    1 root     root            30 Apr 30 17:14 game.properties.enemies -> ..data/game.properties.enemies
lrwxrwxrwx    1 root     root            36 Apr 30 17:14 game.properties.enemies.cheat -> ..data/game.properties.enemies.cheat
lrwxrwxrwx    1 root     root            42 Apr 30 17:14 game.properties.enemies.cheat.level -> ..data/game.properties.enemies.cheat.level
lrwxrwxrwx    1 root     root            28 Apr 30 17:14 game.properties.lives -> ..data/game.properties.lives
lrwxrwxrwx    1 root     root            42 Apr 30 17:14 game.properties.secret.code.allowed -> ..data/game.properties.secret.code.allowed
lrwxrwxrwx    1 root     root            40 Apr 30 17:14 game.properties.secret.code.lives -> ..data/game.properties.secret.code.lives
lrwxrwxrwx    1 root     root            45 Apr 30 17:14 game.properties.secret.code.passphrase -> ..data/game.properties.secret.code.passphrase
lrwxrwxrwx    1 root     root            35 Apr 30 17:14 ui.properties.allow.textmode -> ..data/ui.properties.allow.textmode
lrwxrwxrwx    1 root     root            30 Apr 30 17:14 ui.properties.color.bad -> ..data/ui.properties.color.bad
lrwxrwxrwx    1 root     root            31 Apr 30 17:14 ui.properties.color.good -> ..data/ui.properties.color.good
lrwxrwxrwx    1 root     root            37 Apr 30 17:14 ui.properties.how.nice.to.look -> ..data/ui.properties.how.nice.to.look
/etc/config-volume2 # pwd
/etc/config-volume2
/etc/config-volume2 # 
/etc/config-volume2 # ls -la
total 0
drwxrwxrwx    3 root     root            75 Apr 30 17:14 .
drwxr-xr-x    1 root     root            50 Apr 30 17:14 ..
drwxr-xr-x    2 root     root            22 Apr 30 17:14 ..2018_04_30_17_14_44.848559988
lrwxrwxrwx    1 root     root            31 Apr 30 17:14 ..data -> ..2018_04_30_17_14_44.848559988
lrwxrwxrwx    1 root     root            15 Apr 30 17:14 test.env -> ..data/test.env

更新配置映射中的 ConfigMap。

修改清单文件
将test.env从ceder更改为sugi

cat <<'EOF' > /root/kube_yaml/config_files/game-config-2.yaml
apiVersion: v1
data:
  test.env: 'sugi'
kind: ConfigMap
metadata:
  name: game-config-2
EOF

应用更改

[root@sugi-kubernetes110-master01 ~]# kubectl apply -f /root/kube_yaml/config_files/game-config-2.yaml
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
configmap "game-config-2" configured

我会确认

[root@sugi-kubernetes110-master01 ~]# kubectl get configmap game-config-2 -o yaml
apiVersion: v1
data:
  test.env: sugi
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"test.env":"sugi"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"game-config-2","namespace":"default"}}
  creationTimestamp: 2018-04-30T16:47:42Z
  name: game-config-2
  namespace: default
  resourceVersion: "243453"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-2
  uid: 331bb48c-4c96-11e8-baee-0050569817ee

我在 挂载为卷 的 busybox 容器上检查 ConfigMap。据说更新需要一定时间。在我的情况下,更改立即生效。

kubectl exec -it dapi-test-pod sh
/etc/config-volume2 # pwd
/etc/config-volume2
/etc/config-volume2 # 
/etc/config-volume2 # cat test.env 
sugi/etc/config-volume2 # 

RealWorld示例:Redis与ConfigMap。

请使用以下命令创建ConfiMap。

cat <<'EOF' > /root/kube_yaml/config_files/redis-config
maxmemory 2mb
maxmemory-policy allkeys-lru
EOF

创建ConfigMap

kubectl create configmap example-redis-config --from-file=/root/kube_yaml/config_files/redis-config

确认

[root@sugi-kubernetes110-master01 config_files]# kubectl get configmap example-redis-config -o yaml
apiVersion: v1
data:
  redis-config: |
    maxmemory 2mb
    maxmemory-policy allkeys-lru
kind: ConfigMap
metadata:
  creationTimestamp: 2018-04-30T17:41:36Z
  name: example-redis-config
  namespace: default
  resourceVersion: "244715"
  selfLink: /api/v1/namespaces/default/configmaps/example-redis-config
  uid: baacb60b-4c9d-11e8-baee-0050569817ee

制作Pod的清单文件

cat <<'EOF' > /root/kube_yaml/config_files/redis-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: kubernetes/redis:v1
    env:
    - name: MASTER
      value: "true"
    ports:
    - containerPort: 6379
    resources:
      limits:
        cpu: "0.1"
    volumeMounts:
    - mountPath: /redis-master-data
      name: data
    - mountPath: /redis-master
      name: config
  volumes:
    - name: data
      emptyDir: {}
    - name: config
      configMap:
        name: example-redis-config
        items:
        - key: redis-config
          path: redis.conf
EOF

创建Pod

kubectl create -f /root/kube_yaml/config_files/redis-pod.yaml

我将远程登录到Pod。

kubectl exec -it redis bash

首先,我们会检查环境变量。我们已经确认”Master = true”被正确指定并成功地生效。

root@redis:/redis-master# env
MASTER=true
HOSTNAME=redis
REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-2.8.19.tar.gz
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
TERM=xterm
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_HOST=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/redis-master
SHLVL=1
HOME=/root
KUBERNETES_PORT_443_TCP_PROTO=tcp
REDIS_DOWNLOAD_SHA1=3e362f4770ac2fdbdce58a5aa951c1967e0facc8
REDIS_VERSION=2.8.19
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
OLDPWD=/redis-master-data
_=/usr/bin/env

将ConfigMap挂载到/redis-master,并查看该目录。

root@redis:/redis-master# pwd
/redis-master
root@redis:/redis-master# 
root@redis:/redis-master# ls -la
total 0
drwxrwxrwx 3 root root 77 Apr 30 17:42 .
drwxr-xr-x 1 root root 42 Apr 30 17:45 ..
drwxr-xr-x 2 root root 24 Apr 30 17:42 ..2018_04_30_17_42_54.142107158
lrwxrwxrwx 1 root root 31 Apr 30 17:42 ..data -> ..2018_04_30_17_42_54.142107158
lrwxrwxrwx 1 root root 17 Apr 30 17:42 redis.conf -> ..data/redis.conf

查看文件内容

root@redis:/redis-master# cat redis.conf 
maxmemory 2mb
maxmemory-policy allkeys-lru

Redis容器镜像被设计成可以加载”/redis-master/redis.conf”文件,并通过与ConfigFile配合,动态地指定Redis进程的内存相关配置。

打开 Redis 的 CLI 并进行确认。

root@redis:/etc# redis-cli
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "2097152"
127.0.0.1:6379> 
127.0.0.1:6379> CONFIG GET maxmemory-policy
1) "maxmemory-policy"
2) "allkeys-lru"

请提供网页链接

关于 envFrom:

请点击以下链接了解更多信息:

https://qiita.com/tkusumi/items/cf7b096972bfa2810800

广告
将在 10 秒后关闭
bannerAds