ConfigMap

12. ConfigMap

可以通过卷的方式挂载传递配置文信息 或者可以通过环境变量来传递信息

  • 概述
一般用ConfigMap去管理一些配置文件、或者一些大量的环境变量信息。

ConfigMap 将配置和Pod分开,有一个nginx、nginx.conf -》 configmap nginx
更易于配置文件的更改和管理

ConfigMap配置信息和镜像解耦,实现方式为将配置信息放到ConfigMap对象中,然后在pod的中作为Volume挂载到pod中,从而实现导入配置的目的
  • 使用场景
通过ConfigMap给pod定义全局环境变量
通过ConfigMap给pod传递命令行参数,如mysql -u -p中的账户密码可以通过ConfigMap传递
通过ConfigMap给pod中的容器提供配置文件,配置文件以挂载到容器的形式使用
  • 注意事项
ConfigMap需要在pod使用它之前创建
pod只能使用位于同一个namespace的ConfigMap,及ConfigMap不能夸ConfigMap使用
通常用于非安全加密的配置场景
ConfigMap通常是小于1MB的配置

12.1 使用

#下载官方模板
wget https://kubernetes.io/examples/configmap/ui.properties -O ../configmap/ui.properties

wget --no-check-certificate https://kubernetes.io/examples/configmap/game.properties -O ../configmap/game.properties

#创建cm
[root@master01 configmap]# kubectl create configmap game-config --from-file=../configmap/
configmap/game-config created

  • 查看文件内容
[root@master01 configmap]# kubectl describe cm
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@master01 configmap]# kubectl get  cm 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
kind: ConfigMap
metadata:
  creationTimestamp: "2022-11-08T14:27:20Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:data:
        .: {}
        f:game.properties: {}
    manager: kubectl-create
    operation: Update
    time: "2022-11-08T14:27:20Z"
  name: game-config-2
  namespace: default
  resourceVersion: "211077"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-2
  uid: 36b3a9f3-8427-4eb9-aedf-bf95abe645e1

12.2 数据卷挂载

  • 数据卷的方式挂载
root@deploy-harbor:~/20220731/k8s-Resource-N70/case10-configmap# cat 1-deploy_configmap.yml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
 default: |
    server {
       listen       80;
       server_name  www.mysite.com;
       index        index.html index.php index.htm;

       location / {
           root /data/nginx/html;
           if (!-e $request_filename) {
               rewrite ^/(.*) /index.html last;
           }
       }
    }


---
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ng-deploy-80
  template:
    metadata:
      labels:
        app: ng-deploy-80
    spec:
      containers:
      - name: ng-deploy-80
        image: nginx:1.20.0
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-config
          mountPath:  /etc/nginx/conf.d
        - name: nginx-static-dir
          mountPath: /data/nginx/html
      volumes:
      - name: nginx-static-dir
        hostPath:
          path: /data/nginx
      - name: nginx-config
        configMap:
          name: nginx-config
          items:
             - key: default
               path: mysite.conf

---
apiVersion: v1
kind: Service
metadata:
  name: ng-deploy-80
spec:
  ports:
  - name: http
    port: 81
    targetPort: 80
    nodePort: 30019
    protocol: TCP
  type: NodePort
  selector:
    app: ng-deploy-80
  • 显示
root@deploy-harbor:~/20220731/k8s-Resource-N70/case10-configmap# kubectl describe configmaps nginx-config 
Name:         nginx-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
default:
----
server {
   listen       80;
   server_name  www.mysite.com;
   index        index.html index.php index.htm;

   location / {
       root /data/nginx/html;
       if (!-e $request_filename) {
           rewrite ^/(.*) /index.html last;
       }
   }
}


BinaryData
====

Events:  <none>

12.3 环境变量传递

  • 通过环境变量传递 容器里直接查看env即可
root@deploy-harbor:~/20220731/k8s-Resource-N70/case10-configmap# cat 2-deploy_configmap_env.yml 
apiVersion: v1
kind: ConfigMap

metadata:
  name: nginx-config
data:
  username: "user1"
  password: "12345678"


---
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ng-deploy-80
  template:
    metadata:
      labels:
        app: ng-deploy-80
    spec:
      containers:
      - name: ng-deploy-80
        image: nginx 
        env:
        - name: MY_USERNAME
          valueFrom:
            configMapKeyRef:
              name: nginx-config
              key: username
        - name: MY_PASSWORD
          valueFrom:
            configMapKeyRef:
              name: nginx-config
              key: password
        ######是可以选择其中一个即可 上面是通过变量传递值 下面是直接使用变量传递值###
        - name: "password"
          value: "123456"
        ports:
        - containerPort: 80
posted @ 2022-12-06 21:37  YIDADA-SRE  阅读(69)  评论(0编辑  收藏  举报