ConfigMap

发布时间: 更新时间: 总字数:641 阅读时间:2m 作者: 分享 复制网址

ConfigMap 的作用是将配置从镜像中解耦,增强应用的可以执行和和复用性。

介绍

查看帮助:kubectl explain configmap

命令行创建

  • from-literal
$ kubectl create configmap nginx-conf --from-literal=nginx_port=80 --from-literal=server_name=hello-app.xiexianbin.cn
configmap/nginx-conf created
$ kubectl get cm nginx-conf
NAME          DATA   AGE
nginx-conf    2      9s
$ kubectl describe cm nginx-conf
Name:         nginx-conf
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
nginx_port:
----
80
server_name:
----
hello-app.xiexianbin.cn

BinaryData
====

Events:  <none>
  • from-file

nginx-hello-app.conf:

server {
  server_name hello-app.xiexianbin.cn;
  listen 8080;
  root /data/html;
}

创建:

$ kubectl create configmap <map-name> --from-file=<data-source>
$ kubectl create configmap nginx-hello-app --from-file=./nginx-hello-app.conf
configmap/nginx-hello-app created
$ kubectl get cm nginx-hello-app
NAME              DATA   AGE
nginx-hello-app   1      15s
$ kubectl describe cm nginx-hello-app
Name:         nginx-hello-app
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
nginx-hello-app.conf:
----
server {
  server_name hello-app.xiexianbin.cn;
  listen 8080;
  root /data/html;
}

BinaryData
====

Events:  <none>

Pod ENV from ConfigMap

帮助:kubectl explain pod.spec.containers.env.valueFrom

  • pod-configmap.yaml
apiVersion: v1
kind: Pod
metadata:
  name: cm-pod
  namespace: default
spec:
  containers:
  - name: nginx
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    env:
    - name: NGINX_SERVER_PORT
      valueFrom:
        configMapKeyRef:
          name: nginx-conf
          key: nginx_port
    - name: NGINX_SERVER_NAME
      valueFrom:
        configMapKeyRef:
          name: nginx-conf
          key: server_name

  • 查看
$ kubectl apply -f pod-configmap.yaml
pod/cm-pod created
$ kubectl exec -it cm-pod -- printenv | grep NGINX_
NGINX_SERVER_PORT=80
NGINX_SERVER_NAME=hello-app.xiexianbin.cn
  • 修改 configmap
kubectl edit cm nginx-conf

该方式的环境变量只在重启才重新注入 Pod

Pod Volume from configMap

  • pod-configmap-vol.yaml
apiVersion: v1
kind: Pod
metadata:
  name: cm-pod-vol
  namespace: default
spec:
  containers:
  - name: nginx
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: nginx-conf
      mountPath: /data
      readOnly: true
  volumes:
  - name: nginx-conf
    configMap:
      name: nginx-conf

  • 查看
$ kubectl apply -f pod-configmap-vol.yaml
pod/cm-pod-vol created
$ kubectl exec -it cm-pod-vol -- sh
# cat /data/nginx_port
80
# cat /data/server_name
hello-app.xiexianbin.cn
#
  • 修改 configmap 端口
# 80 -> 8080
$ kubectl edit cm nginx-conf

# 等一会
$ kubectl exec -it cm-pod-vol -- sh
# cat /data/nginx_port
8080

因此,configMap 通过 volumes 的方式挂载到容器内,等一段时间后,会自动同步到容器里

基于目录创建 ConfigMap

# 创建本地目录
$ mkdir -p configmap

# 将实例文件下载到 configmap/ 目录
$ cd configmap
$ cat game.propertie
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
$ cat ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
$ cd ..

# 创建 configmap
$ kubectl create configmap configmap-game --from-file=configmap/
$ kubectl describe configmaps configmap-game
Name:         configmap-game
Namespace:    xiexianbin
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>
$ kubectl get configmaps configmap-game -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: "2020-12-20T09:21:04Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:data:
        .: {}
        f:game.properties: {}
        f:ui.properties: {}
    manager: kubectl-create
    operation: Update
    time: "2020-12-20T09:21:04Z"
  name: configmap-game
  namespace: xiexianbin
  resourceVersion: "1383191"
  uid: b0b85bc2-562a-4bb6-9ba5-54f3ebdf0448

基于文件创建 ConfigMap

kubectl create configmap configmap-game-2 --from-file=configmap-game/game.properties
$ kubectl describe configmaps configmap-game-2
Name:         configmap-game-2
Namespace:    xiexianbin
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>
$ kubectl create configmap configmap-game-2 --from-file=configmap-game/game.properties  --from-file=configmap-game/ui.properties
configmap/configmap-game-2 created
[xiexianbin@proxy ~]$ kubectl describe configmaps configmap-game-2
Name:         configmap-game-2
Namespace:    xiexianbin
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>
最新评论
加载中...
Home Archives Categories Tags Statistics