目录贴:Kubernetes学习系列

  其他容器编排调度工具会大谈特谈“轻应用”、“十二要素应用”,这样就势必会对企业级复杂应用做很大的改动。Kubernetes是为了解决“如何合理使用容器支撑企业级复杂应用”这个问题而诞生的,所以它的设计理念是要支持绝大多数应用的原生形态。例如,很多应用程序的配置需要通过配置文件,命令行参数和环境变量的组合配置来完成(“十二要素应用”等均要求去配置)。这些配置应该从image内容中解耦,以此来保持容器化应用程序的可移植性。ConfigMap API资源提供了将配置数据注入容器的方式,同时保证该机制对容器来说是透明的。ConfigMap可以被用来保存单个属性,也可以用来保存整个配置文件或者JSON二进制大对象。

  ConfigMap API资源存储键/值对配置数据,这些数据可以在pods里使用。ConfigMap跟Secrets类似,但是ConfigMap可以更方便的处理不包含敏感信息的字符串。示例如下:

kind: ConfigMap
apiVersion: v1
metadata:
creationTimestamp: --18T19::38Z
name: example-config
namespace: default
data:
example.property.: hello
example.property.: world
example.property.file: |-
property.=value-
property.=value-
property.=value-

  通过示例代码可以看到:ConfigMap可以包含细粒度的配置项,如:example.property.1;也可以包含粗粒度的配置文件,如:example.property.file。

1、创建ConfigMap

1.1 从文件夹创建

[root@k8s-master propertirs]# cat /home/yaml/propertirs/game.properties
enemies=aliens
lives=
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=
[root@k8s-master propertirs]# cat /home/yaml/propertirs/ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
[root@k8s-master propertirs]# kubectl create configmap game-config --from-file=/home/yaml/propertirs/
configmap "game-config" created
[root@k8s-master propertirs]# kubectl describe configmaps game-config
#该方法只能得到ConfigMap的Key和size
Name: game-config
Namespace: default
Labels: <none>
Annotations: <none> Data
====
ui.properties: bytes
game.properties: bytes
#若想得到详细信息,可通过以下命令:
[root@k8s-master propertirs]# kubectl get configmaps game-config -o yaml
apiVersion: v1
data:
game.properties: |
enemies=aliens
lives=
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
creationTimestamp: --21T03::34Z
name: game-config
namespace: default
resourceVersion: ""
selfLink: /api/v1/namespaces/default/configmaps/game-config
uid: a04f90f0-0de5-11e7-b3d5-fa163ebba51b

1.2 从文件创建

[root@k8s-master propertirs]# kubectl create configmap game-config- --from-file=/home/yaml/propertirs/game.properties --from-file=/home/yaml/propertirs/ui.properties
configmap "game-config-2" created
[root@k8s-master propertirs]# kubectl get configmaps game-config- -o yaml
apiVersion: v1
data:
game.properties: |
enemies=aliens
lives=
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
creationTimestamp: --21T03::15Z
name: game-config-
namespace: default
resourceVersion: ""
selfLink: /api/v1/namespaces/default/configmaps/game-config-
uid: b2e4dfab-0de6-11e7-b3d5-fa163ebba51b

1.3  指定data中的key

[root@k8s-master propertirs]# kubectl create configmap game-config- --from-file=game-special-key=/home/yaml/propertirs/game.properties
configmap "game-config-3" created
[root@k8s-master propertirs]# kubectl get configmaps game-config- -o yaml
apiVersion: v1
data:
game-special-key: |
enemies=aliens
lives=
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=
kind: ConfigMap
metadata:
creationTimestamp: --21T03::23Z
name: game-config-
namespace: default
resourceVersion: ""
selfLink: /api/v1/namespaces/default/configmaps/game-config-
uid: 2345dad3-0de7-11e7-b3d5-fa163ebba51b

1.4  指定具体的值

[root@k8s-master propertirs]# kubectl create configmap game-config- --from-literal=special.user=zhenyu --from-literal=special.passwd=yaodidiao
configmap "game-config-4" created
[root@k8s-master propertirs]# kubectl get configmaps game-config- -o yaml
apiVersion: v1
data:
special.passwd: yaodidiao
special.user: zhenyu
kind: ConfigMap
metadata:
creationTimestamp: --21T03::12Z
name: game-config-
namespace: default
resourceVersion: ""
selfLink: /api/v1/namespaces/default/configmaps/game-config-
uid: 8802f6d2-0de7-11e7-b3d5-fa163ebba51b

2、使用ConfigMap

2.1 环境变量或参数

  创建一个Pod,并将一个已经创建好的ConfigMap作为环境变量,注入到Pod中。

[root@k8s-master propertirs]# kubectl get configmaps game-config- -o yaml
apiVersion: v1
data:
special.passwd: yaodidiao
special.user: zhenyu
kind: ConfigMap
metadata:
creationTimestamp: --21T03::12Z
name: game-config-
namespace: default
resourceVersion: ""
selfLink: /api/v1/namespaces/default/configmaps/game-config-
uid: 8802f6d2-0de7-11e7-b3d5-fa163ebba51b
[root@k8s-master propertirs]# cat testEnv.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
name: testenv
role: master
name: testenv
spec:
containers:
- name: testenv
image: busybox
imagePullPolicy: IfNotPresent
env:
- name: SPECIAL_USER
valueFrom:
configMapKeyRef:
name: game-config-
key: special.user
command:
- sleep
- ""
[root@k8s-master propertirs]# kubectl create -f testEnv.yaml
pod "testenv" created
[root@k8s-master propertirs]# kubectl exec -ti testenv sh
/ # echo $SPECIAL_USER
zhenyu
/ #

2.2 挂载文件数据卷

[root@k8s-master propertirs]# kubectl get configmaps game-config -o yaml
apiVersion: v1
data:
game.properties: |
enemies=aliens
lives=
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
creationTimestamp: --21T03::34Z
name: game-config
namespace: default
resourceVersion: ""
selfLink: /api/v1/namespaces/default/configmaps/game-config
uid: a04f90f0-0de5-11e7-b3d5-fa163ebba51b
[root@k8s-master propertirs]# cat testVolume.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
name: testvolume
role: master
name: testvolume
spec:
containers:
- name: testvolume
image: busybox
imagePullPolicy: IfNotPresent
volumeMounts:
- name: config-volume
mountPath: /etc/config
command:
- sleep
- ""
volumes:
- name: config-volume
configMap:
name: game-config
[root@k8s-master propertirs]# kubectl create -f testVolume.yaml
pod "testvolume" created
[root@k8s-master propertirs]# kubectl exec -ti testvolume sh
/ # cd /etc/config/
/etc/config # ls
game.properties ui.properties
/etc/config # cat game.properties
enemies=aliens
lives=
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=
/etc/config # cat ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
/etc/config #

2.3 挂载信息数据卷

[root@k8s-master propertirs]# cat testVolume.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
name: testvolume
role: master
name: testvolume
spec:
containers:
- name: testvolume
image: busybox
imagePullPolicy: IfNotPresent
volumeMounts:
- name: config-volume
mountPath: /etc/config
command:
- sleep
- ""
volumes:
- name: config-volume
configMap:
name: game-config-
[root@k8s-master propertirs]# kubectl get configmaps game-config- -o yaml
apiVersion: v1
data:
special.passwd: yaodidiao
special.user: zhenyuyaodidiao
kind: ConfigMap
metadata:
creationTimestamp: --21T06::29Z
name: game-config-
namespace: default
resourceVersion: ""
selfLink: /api/v1/namespaces/default/configmaps/game-config-
uid: bd086dca-0dff-11e7-b3d5-fa163ebba51b
[root@k8s-master propertirs]# kubectl create -f testVolume.yaml
pod "testvolume" created
[root@k8s-master propertirs]# kubectl exec -ti testvolume sh
/ # cd /etc/config/
/etc/config # ls
special.passwd special.user
/etc/config # cat special.user
zhenyuyaodidiao/etc/config #
/etc/config # exit

2.4 热更新

  当ConfigMap以数据卷的形式挂载进Pod的时,这时更新ConfigMap(或删掉重建ConfigMap),Pod内挂载的配置信息会热更新。这时可以增加一些监测配置文件变更的脚本,然后reload对应服务。

Kubernetes外挂配置管理—ConfigMap介绍的更多相关文章

  1. K8S学习笔记之Kubernetes 配置管理 ConfigMap

    0x00 概述 很多情况下我们为某一应用做好镜像,当我们想修改其中的一些参数的时候,就变得比较麻烦,又要重新制作镜像,我们是不是有一种方式,让镜像根据不同的场景调用我们不同的配置文件呢,那我们就需要用 ...

  2. Kubernetes 配置管理 ConfigMap(十二)

    目录 一.背景 二.创建 ConfigMap 2.1.通过 --from-literal 2.2.通过 --from-file 2.3.通过--from-env-file 2.4.YAML 配置文件 ...

  3. 【k8s】kubernetes(k8s)介绍

    转自 http://blog.csdn.net/Real_Myth/article/details/78719244 一.Kubernetes系列之介绍篇   •Kubernetes介绍 1.背景介绍 ...

  4. Docker集群编排工具之Kubernetes(K8s)介绍、安装及使用

    K8s基础原理 k8s中文社区:https://www.kubernetes.org.cn/ 简介 Kubernetes与较早的集群管理系统Mesos和YARN相比,对容器尤其是 Docker的支持更 ...

  5. kubernetes系列之ConfigMap使用方式

    作用理解 核心用途就是容器和配置的分离解耦. 如启用一个mysql容器,mysql容器重要的文件有两部分,一部分为存储数据文件,一部分为配置文件my.cnf,存储数据可以用持久存储实现和容器的分离解耦 ...

  6. (转帖)开源容器集群管理系统Kubernetes架构及组件介绍

    最近在搞Docker还有她的管理工具,选型Kuberetes后,被她的术语和概念搞得晕头转向...看了一篇文章还不错,放到这里分享出来. 地址:http://www.linuxidc.com/Linu ...

  7. Kubernetes系列之Helm介绍篇

    本次系列使用的所需部署包版本都使用的目前最新的或最新稳定版,安装包地址请到公众号内回复[K8s实战]获取 介绍 Helm 是 Deis 开发的一个用于 Kubernetes 应用的包管理工具,主要用来 ...

  8. Kubernetes中的Configmap和Secret

    本文的试验环境为CentOS 7.3,Kubernetes集群为1.11.2,安装步骤参见kubeadm安装kubernetes V1.11.1 集群 应用场景:镜像往往是一个应用的基础,还有很多需要 ...

  9. Kubernetes里的ConfigMap的用途

    顾名思义,ConfigMap用于保存配置数据的键值对,可以用来保存单个属性,也可以用来保存配置文件. ConfigMap同Kubernetes的另一个概念secret类似,区别是ConfigMap主要 ...

随机推荐

  1. javassist实例

    我们常用到的动态特性主要是反射,在运行时查找对象属性.方法,修改作用域,通过方法名称调用方法等.在线的应用不会频繁使用反射,因为反射的性能开销较大.其实还有一种和反射一样强大的特性,但是开销却很低,它 ...

  2. PowerDesigner 15进行逆向工程生成数据库图表时,注释的comment的生成,解决PowerDesigner逆向工程没有列注释

    使用PowerDesigner默认配置逆向工程是没有注释(name列为英文,comment列是空的),这样的不方便查看字段具体是什么意义,将注释一同导出,方便查看字段具体的意义,如下图 注释列导出步骤 ...

  3. 30、git 使用

    Git 一. 目标 (一) Git的操作 二. Git和SVN的区别 (一) SVN优缺点 1. 优点: (1) 管理方便,逻辑明确 (2) 易于管理,集中式服务器更能保证安全性 (3) 代码一致性非 ...

  4. python os module

    os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号                                                              ...

  5. phjp(ajax)作用知识点

    1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...

  6. URL传值乱码问题。(已解决)

    1. 问题描述 今天,我在写我的记账本的主界面,想在右上角加一个用户名提示,需要我把登陆界面的用户名传递给主界面,输入英文可以,输入汉字,发现显示在右上角的是乱码. 2. 解决办法 看这个乱码眼熟,我 ...

  7. Python学习之旅(二十七)

    Python基础知识(26):常用内建模块(Ⅱ) 1.hashlib Python的hashlib提供了常见的摘要算法,如MD5,SHA1等 摘要算法又称哈希算法.散列算法. (1)它通过一个函数,把 ...

  8. ORACLE导入梗

    1.Oracle版数据库的安装及初始化 1.1安装oracle数据库(10g或11g) 1.2以用户system账号登陆oralcle数据库的sqlplus,执行以下语句 1.3创建表空间语句: cr ...

  9. python全栈开发 * 33 知识点汇总 * 180718

    33 udp协议编码 显示客户端名字,输出带颜色的内容 udp协议的时间同步机制 #一.udp 协议编码 一个服务器,多个客户端#服务器:# import socket# sk=socket.sock ...

  10. 51nod图论题解(4级,5级算法题)

    51nod图论题解(4级,5级算法题) 1805 小树 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 她发现她的树的点上都有一个标号(从1到n),这些树都在空 ...