Kubernetes-ConfigMap详解

目录

简介:

一、ComfigMap的创建

1.使用目录创建

2.使用文件创建

3.使用命令行创建

二、Pod中使用ConfigMap

1.使用ConfigMap代替环境变量

2.使用ConfigMap设置命令行参数

3.使用ConfigMap用做数据卷插件

三、ConfigMap的热更新


简介:

       ConfigMap 功能在 Kubernetes1.2 版本中引入,许多应用程序会从配置文件、命令行参数或环境变量中读取配置信息。ConfigMap API 给我们提供了向容器中注入配置信息的机制,ConfigMap 可以被用来保存单个属性,也可以用来保存整个配置文件或者 JSON 二进制等对象。

一、ComfigMap的创建

1.使用目录创建

mkdir configmap
    #创建configmap的目录,目录下创建文件,基于这个目录创建configmap

vim game.file
version=1.17
name=dave
age=18

vim ui.properties
level=2
color=yellow
 kubectl create configmap game-config --from-file=../configmap/
    #基于configmap目录创建名为game-config的configmap。
    #--from-file指定在目录下的所有文件都会被用在 ConfigMap 里面创建一个键值对,键的名字就是文件名,值就是文件的内容

2.使用文件创建

kubectl create configmap game-config-2 --from-file=./game.file
    #基于文件创建
    #--from-file这个参数可以使用多次,你可以使用两次分别指定上个实例中的那两个配置文件,效果就跟指定整个目录是一样的。
kubectl get cm	
    #configmap的简称是cm

kubectl get cm game-config -o yaml
    #以yaml格式查看名为game-config的cm

kubectl describe cm game-config
    #查看cm的详细信息

3.使用命令行创建

kubectl create configmap literal-config --from-literal=name=dave --from-literal=password=pass
	#创建的类型为configmap,configmap的名字为literal-config,名字为name,值是dave,名字为password,值为pass

kubectl get configmaps literal-config -o yaml
    #查看创建的cm

二、Pod中使用ConfigMap

1.使用ConfigMap代替环境变量

vim configmap-literal.yaml
    #创建configmap的yaml文件

apiVersion: v1
kind: ConfigMap
metadata:
  name: literal-config
  namespace: default
data:
  name: dave
  password: pass

kubectl apply -f configmap-literal.yaml
    #基于yaml文件运行configmap
vim configmap-env.yaml
    #创建yaml文件

apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  og_level: INFO

kubectl apply -f configmap-env.yaml
    #基于yaml文件运行configmap
vim configmap-pod.yaml
    #创建pod使用cm的yaml文件

apiVersion: v1
kind: Pod
metadata:
  name: cm-env-test-pod
spec:
  containers:
    - name: test-container
      image: centos:7.9.2009
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: USERNAME                #创建一个环境变量的名字
          valueFrom:                    #来源
            configMapKeyRef:            #来源于configMapKeyRef
              name: literal-config      #来自于上面创建的literal-config
              key: name                 #来自于name字段
        - name: PASSWORD
          valueFrom:
            configMapKeyRef:
              name: literal-config
              key: password
      envFrom:                          #使用envFrom字段可以将env-config中的所有键值对作为环境变量导入到Pod中
        - configMapRef:
            name: env-config
  restartPolicy: Never
kubectl create -f configmap-pod.yaml
    #根据yaml文件创建pod

kubectl get pod

kubectl logs cm-env-test-pod
    #查看pod的日志

2.使用ConfigMap设置命令行参数

vim cm-command-dapi-test-pod

apiVersion: v1
kind: Pod
metadata:
  name: cm-command-dapi-test-pod
spec:
  containers:
    - name: test-container
      image: centos:7.9.2009
      command: [ "/bin/sh", "-c", "echo $(USERNAME) $(PASSWORD)" ]
      env:
        - name: USERNAME
          valueFrom:
            configMapKeyRef:
              name: literal-config
              key: name
        - name: PASSWORD
          valueFrom:
            configMapKeyRef:
              name: literal-config
              key: password
  restartPolicy: Never

kubectl create -f cm-command-dapi-test-pod

kubectl get pod

kubectl logs cm-command-dapi-test-pod

3.使用ConfigMap用做数据卷插件

vim cm-volume-test-pod

apiVersion: v1
kind: Pod
metadata:
  name: cm-volume-test-pod
spec:
  containers:
    - name: test-container
      image: centos:7.9.2009
      command: [ "/bin/sh","-c","tailf /var/log/yum.log" ]    #持久化运行,使容器保持运行,不会退出。
      volumeMounts:              #挂载volume卷
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume        #卷的名字叫config-volume
      configMap:                 #基于configMap提供的
        name: literal-config     #基于名字为literal-config的configMap提供的
  restartPolicy: Never


kubectl apply -f cm-volume-test-pod

kubectl get pod

kubectl exec -it cm-volume-test-pod -- /bin/bash
    #进入pod内的容器,这里pod内只有一个容器,如果pod内有多个容器,需要-c指定容器名。
    #进入容器后,执行下列命令,查看挂载的volume卷的信息。

#为什么这里这么设计,因为这里支持热更新

#什么叫热更新呢,就是我现在把configmap的对象改变的话,这里的内容也会变化,

#注意:这里不是共享,这是注入

          如果共享过多的话,会造成过多的资源损耗

          注入的话就不会有这个问题,先注入10个pod,这10个pod注入以后,再注入下10pod

#什么叫注入,cat这个文件,重定向到你的目录下,这叫注入。

三、ConfigMap的热更新

vim hot-update.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: log-config
  namespace: default
data:
  log_level: INFO
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: hot-update
spec:
  replicas: 2
  selector:
    matchLabels:
      run: nginx
  template:
    metadata:
      labels:
        run: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
        - name: config-volume
          mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            name: log-config

kubectl create -f hot-update.yaml

kubectl get cm
    #查看刚刚创建的名为log-config的cm。

开第二个终端

while 2>1;do kubectl exec hot-update-5f9f99b44f-sm7kf -- cat /etc/config/log_level;sleep 1s;date;done
    #观察信息

终端1:

kubectl edit cm log-config
	#修改data里的键值INFO为log_level:ERROR
	#annotations:里面的INFO为ERROR

终端2查看变化 

kubectl get deployment hot-update -o yaml
    #查看yaml文件

kubectl patch deployment hot-update --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "20190411" }}}}}'
    #更新版本

kubectl get deployment hot-update -o yaml
    #再次查看yaml文件

kubectl get pod
    #pod已更新

kubectl patch deployment hot-update --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "20190412" }}}}}'	
    #修改版本号的会再一次触发更新

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# Copyright 2017 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ------------------- Dashboard Secret ------------------- # apiVersion: v1 kind: Secret metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard-certs namespace: kube-system type: Opaque --- # ------------------- Dashboard Service Account ------------------- # apiVersion: v1 kind: ServiceAccount metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system --- # ------------------- Dashboard Role & Role Binding ------------------- # kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: name: kubernetes-dashboard-minimal namespace: kube-system rules: # Allow Dashboard to create 'kubernetes-dashboard-key-holder' secret. - apiGroups: [""] resources: ["secrets"] verbs: ["create"] # Allow Dashboard to create 'kubernetes-dashboard-settings' config map. - apiGroups: [""] resources: ["configmaps"] verbs: ["create"] # Allow Dashboard to get, update and delete Dashboard exclusive secrets. - apiGroups: [""] resources: ["secrets"] resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs"] verbs: ["get", "update", "delete"] # Allow Dashboard to get and update 'kubernetes-dashboard-settings' config map. - apiGroups: [""] resources: ["configmaps"] resourceNames: ["kubernetes-dashboard-settings"] verbs: ["get", "update"] # Allow Dashboard to get metrics from heapster. - apiGroups: [""] resources: ["services"] resourceNames: ["heapster"] verbs: ["proxy"] - apiGroups: [""] resources: ["services/proxy"] resourceNames: ["heapster", "http:heapster:", "https:heapster:"] verbs: ["get"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: kubernetes-dashboard-minimal namespace: kube-system roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: kubernetes-dashboard-minimal subjects: - kind: ServiceAccount name: kubernetes-dashboard namespace: kube-system --- # ------------------- Dashboard Deployment ------------------- # kind: Deployment apiVersion: apps/v1 metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system spec: replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: k8s-app: kubernetes-dashboard template: metadata: labels: k8s-app: kubernetes-dashboard spec: containers: - name: kubernetes-dashboard image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1 imagePullPolicy: IfNotPresent ports: - containerPort: 8443 protocol: TCP args: - --auto-generate-certificates # Uncomment the following line to manually specify Kubernetes API server Host # If not specified, Dashboard will attempt to auto discover the API server and connect # to it. Uncomment only if the default does not work. #- --apiserver-host=http://192.168.140.129:8080 volumeMounts: - name: kubernetes-dashboard-certs mountPath: /certs # Create on-disk volume to store exec logs - mountPath: /tmp name: tmp-volume livenessProbe: httpGet: scheme: HTTPS path: / port: 8443 initialDelaySeconds: 30 timeoutSeconds: 30 volumes: - name: kubernetes-dashboard-certs secret: secretName: kubernetes-dashboard-certs - name: tmp-volume emptyDir: {} serviceAccountName: kubernetes-dashboard # Comment the following tolerations if Dashboard must not be deployed on master tolerations: - key: node-role.kubernetes.io/master effect: NoSchedule --- # ------------------- Dashboard Service ------------------- # kind: Service apiVersion: v1 metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system spec: type: NodePort ports: - port: 443 targetPort: 8443 selector: k8s-app: kubernetes-dashboard

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值