全栈工程师开发手册 (作者:栾鹏)
架构系列文章


先准备你自己的文件,比如为config.js
内容如下

(function(global, factory) {typeof exports === "undefined" && typeof module !== "undefined"? (module.exports = factory()): typeof define === "function" && define.amd? define(factory): (global.config = factory());})(this, function() {"use strcit";const config = {$server: "xxxxxxxxx",$tensorboardaddress: "xxxxxxx"};return config;});

将此文件写入到yml文件,形成configmap.yml

kind: ConfigMap
apiVersion: v1
metadata:name: flw-confignamespace: v0930labels:app: flfrontendchart: flfrontend-0.1.0release: flwheritage: Tiller
data:config.js: |(function(global, factory) {typeof exports === "undefined" && typeof module !== "undefined"? (module.exports = factory()): typeof define === "function" && define.amd? define(factory): (global.config = factory());})(this, function() {"use strcit";const config = {$server: "xxxxxxx",$tensorboardaddress: "xxxxxxx"};return config;});

书写格式要符合yml文件的格式。可以在http://www.bejson.com/validators/yaml_editor/网页中编辑后再复制进来

通过kubectl create -f configmap.yml创建configmap

当然我们可以不需要制作这样的yml文件直接通过config.js创建configmap

kubectl create configmap confnginx --from-file=config.js --namespace=cloudai-2

然后在pod中通过将configmap挂载到pod目录下文件,成功将文件添加到pod中

---apiVersion: apps/v1
kind: Deployment
metadata:name: flw-flfrontendnamespace: v0930labels:app: flfrontendchart: flfrontend-0.1.0release: flwheritage: Tiller
spec:selector:matchLabels:app: flfrontendrelease: flwreplicas: 1template:metadata:labels:app: flfrontendrelease: flwspec:volumes:- name: configconfigMap:name: flw-configcontainers:- name: flfrontendimage: xxxxxxximagePullPolicy: IfNotPresentcommand: ["sleep","20000"]ports:- containerPort: 80volumeMounts:- name: configmountPath: /var/www/html/dist/static/config.jssubPath: config.jsreadOnly: False

其中subPath的目的是为了在单一Pod中多次使用同一个volume而设计的. 这样就只是挂载该文件,不要影响挂载目录下的其他文件。

直接将文件、目录、字符串挂载进入

可以使用kubectl create configmap命令,可以根据目录, 文件或 字面值创建ConfigMap。

kubectl create configmap <map-name> <data-source>

<map-name>代表ConfigMap的名字,<data-source>代表目录、文件或者字面值。

数据源对应于ConfigMap中的键值对,

  • key = 文件名或者命令行中提供的key
  • value = 文件内容或命令中提供的字面值

可以使用kubectl describe or kubectl get 获取ConfigMap的信息。前者仅展示ConfigMap的概要,后者将展示ConfigMap的全部内容。

利用目录创建ConfigMap

使用kubectl create configmap命令从同一目录下的一组文件中创建ConfigMap,例如:

kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl

将docs/user-guide/configmap/kubectl目录的内容

ls docs/user-guide/configmap/kubectl/game.properties
ui.properties

组合成下面的ConfigMap:

kubectl describe configmaps game-configName:           game-config
Namespace:      default
Labels:         <none>
Annotations:    <none>Data
====
game.properties:        158 bytes
ui.properties:          83 bytes

docs/user-guide/configmap/kubectl/目录下的game.properties和ui.properties文件代表ConfigMap中的data部分。

kubectl get configmaps game-config-2 -o yaml
apiVersion: v1
data:game.properties: |enemies=alienslives=3enemies.cheat=trueenemies.cheat.level=noGoodRottensecret.code.passphrase=UUDDLRLRBABASsecret.code.allowed=truesecret.code.lives=30ui.properties: |color.good=purplecolor.bad=yellowallow.textmode=truehow.nice.to.look=fairlyNice
kind: ConfigMap
metadata:creationTimestamp: 2016-02-18T18:52:05Zname: game-config-2namespace: defaultresourceVersion: "516"selfLink: /api/v1/namespaces/default/configmaps/game-config-2uid: b4952dc3-d670-11e5-8cd0-68f728db1985

利用文件中创建ConfigMap

使用kubectl create configmap命令从单个文件或一组文件中创建ConfigMap,例如:

kubectl create configmap game-config-2 --from-file=docs/user-guide/configmap/kubectl/game.properties

将产生如下的ConfigMap:

kubectl describe configmaps game-config-2
Name:           game-config-2
Namespace:      default
Labels:         <none>
Annotations:    <none>Data
====
game.properties:        158 bytes

您可以多次传递–from-file参数使用不同的数据源来创建ConfigMap。

kubectl create configmap game-config-2 --from-file=docs/user-guide/configmap/kubectl/game.properties --from-file=docs/user-guide/configmap/kubectl/ui.properties kubectl describe configmaps game-config-2Name:           game-config-2
Namespace:      default
Labels:         <none>
Annotations:    <none>Data
====
game.properties:        158 bytes
ui.properties:          83 bytes

利用文件创建ConfigMap是定义key

当您使用–from-file参数时,可以在ConfigMap的data小节内定义key替代默认的文件名:

kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file>

<my-key-name>是ConfigMap中的key,<path-to-file>是key代表的数据源文件位置。

kubectl create configmap game-config-3 --from-file=game-special-key=docs/user-guide/configmap/kubectl/game.propertieskubectl get configmaps game-config-3 -o yamlapiVersion: v1
data:game-special-key: |enemies=alienslives=3enemies.cheat=trueenemies.cheat.level=noGoodRottensecret.code.passphrase=UUDDLRLRBABASsecret.code.allowed=truesecret.code.lives=30
kind: ConfigMap
metadata:creationTimestamp: 2016-02-18T18:54:22Zname: game-config-3namespace: defaultresourceVersion: "530"selfLink: /api/v1/namespaces/default/configmaps/game-config-3uid: 05f8da22-d671-11e5-8cd0-68f728db1985

利用字面值创建ConfigMap

使用kubectl create configmap时使用--from-literal参数在命令中定义字面值:

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

可以传递多个键值对。每个对都代表ConfigMap的data小节中独立的一项。

kubectl get configmaps special-config -o yaml
apiVersion: v1
data:special.how: veryspecial.type: charm
kind: ConfigMap
metadata:creationTimestamp: 2016-02-18T19:14:38Zname: special-confignamespace: defaultresourceVersion: "651"selfLink: /api/v1/namespaces/default/configmaps/special-configuid: dadce046-d673-11e5-8cd0-68f728db1985

对configmap中的不同文件分别处理

apiVersion: v1
kind: ConfigMap
metadata:name: testmap
data:test.file: |this is a test stringstest.file2: |this is the 2nd strings
---
apiVersion: v1
kind: Pod
metadata:name: pod-test
spec:containers:- name: testimage: busyboxtty: truecommand: ["sh"]volumeMounts:- name: testdirmountPath: /root/test.map1subPath: test.map1- name: testdirmountPath: /etc/test.map2subPath: test.map2volumes:- name: testdirconfigMap:name: testmapitems:- key: test.filepath: test.map1- key: test.file2path: test.map2

理解Config Map

ConfigMap允许您将配置文件从容器镜像中解耦,从而增强容器应用的可移植性。

ConfigMap API resource将配置数据以键值对的形式存储。这些数据可以在pod中消费或者为系统组件提供配置,例如controller。ConfigMap与Secret类似,但是通常只保存不包含敏感信息的字符串。用户和系统组件可以以同样的方式在ConfigMap中存储配置数据。

注意:ConfigMap只引用属性文件,而不会替换它们。可以把ConfigMap联想成Linux中的/etc目录和它里面的内容。例如,假如您使用ConfigMap创建了Kubernetes Volume,ConfigMap中的每个数据项都代表该volume中的一个文件。

ConfigMap的data项中包含了配置数据。如下所示,可以是很简单的——如使用 —from-literal 参数定义的每个属性;也可以很复杂——如使用--from-file参数定义的配置文件或者json对象。

kind: ConfigMap
apiVersion: v1
metadata:creationTimestamp: 2016-02-18T19:14:38Zname: example-confignamespace: default
data:# example of a simple property defined using --from-literalexample.property.1: helloexample.property.2: world# example of a complex property defined using --from-fileexample.property.file: |-property.1=value-1property.2=value-2property.3=value-3

在k8s中将文件通过configmap添加为pod的文件相关推荐

  1. 图文:windows7文件夹权限添加_解决目标文件夹访问被拒绝

    随着windows7操作系统的普及推广,现在已经有很多人的电脑都在只用这个系统了.使用比较多或者细心的人应该会发现在win7的操作系统中,在文件夹下的c:\windows\winsxs的文件和文件夹受 ...

  2. windows7文件夹权限添加,解决目标文件夹访问被拒绝

    随着windows7操作系统的普及推广,现在已经有很多人的电脑都在只用这个系统了.使用比较多或者细心的人应该会发现在win7的操作系统中,在文件夹下的c:\windows\winsxs的文件和文件夹受 ...

  3. c语言在文件指定地方添加字符,C语言文件操作在指定行插入数据

    C语言文件操作有覆盖和追加两种模式,但不提供插入模式.所以要在文件中指定行进行删除或者插入,需要按照如下流程操作: 1.以只读打开文件: 2.将文件逐行读入到内存中: 3.关闭文件: 4.在内存中对指 ...

  4. 【Keil】Keil5添加源程序和头文件

    xxx.c就是源程序 xxx.h就是头文件 [源程序添加方法] 双击文件夹,例如图片上的Source,跳出弹窗,选择需要添加的源程序即可 [添加头文件的方法] 1.首先点击图片红框处,或是在文件夹te ...

  5. BootStrap使用方法为BootStrap3添加jquery.min.js文件(简单易懂)_☆往事随風☆的博客

    为BootStrap3添加jquery.min.js文件 一.前言 二.BootStrap简介 三.BootStrap3的使用方法 (1)先去官网找到BootStrap3这个版本的文档. (2)配置B ...

  6. k8s使用volume将ConfigMap作为文件或目录直接挂载_Kubernetes in Action 06. 卷:将磁盘挂载到容器...

    简介 P161 pod 中的每个容器都有自己独立的文件系统,因为文件系统来自容器镜像. P161 Kubernetes 中的卷是 pod 的一部分,并和 pod 共享相同的生命周期.这意味着 pod ...

  7. k8s使用volume将ConfigMap作为文件或目录直接挂载_NET Core + Kubernetes:Volume

    和 Docker 类似,Kubernetes 中也提供了 Volume 来实现数据卷挂载,但 Kubernetes 中 Volume 是基于 Pod,而不是容器,它可被 Pod 中多个容器共享,另外 ...

  8. @kubernetes(k8s) 应用配置管理(ConfigMap、subPath、Secret)

    k8s 应用配置管理 ConfigMap 文章目录 k8s 应用配置管理 ConfigMap 一. ConfigMap 1.configmap的介绍 2.congfigmap的创建方式(四种) [指定 ...

  9. k8s资源secret和ConfigMap的相关详解

    secret 一.secret相关简介 二.secret资源的使用 三.Secret实践k8s连接Harbor 四.ConfigMap相关介绍 五.ConfigMap资源的使用 一.secret相关简 ...

最新文章

  1. 普通卷积armv7-neon指令集实现—XNNPACK
  2. 公差基本偏差代号_508/f7:基本偏差怎么查,标准公差又怎么查?
  3. WPF如何给窗口设置透明png的图片背景
  4. linux是不是显示不了中文版,Linux为什么OpenOffice下不能显示中文
  5. MySQL(四)InnoDB中一棵B+树能存多少行数据
  6. C语言 fread 函数 - C语言零基础入门教程
  7. redis-live监控安装与测试
  8. 大学生搜题软件哪个好?2020搜题软件排行榜
  9. 数学建模上课(一)推导万有引力定律
  10. 虹科新品 | 需要进行高功率,大规模的测试控制?这款5A功率高密度继电器模块你一定不能错过!
  11. 【实用算法教学】——教你如何用转换器抽取电影,音乐甚至是比赛等特征
  12. PyCharm下载安装以及使用教程
  13. 对基层技术管理者的一些建议
  14. Linux学习-MySQL变量学习
  15. [附源码]java毕业设计电影影评网
  16. 微型计算机阅读,CHP1微型计算机基础(阅读).ppt
  17. 快速画正弦波、方波、三角波——Visio制图总结(六)
  18. MSP432库函数学习笔记-CS
  19. Pytorch LR scheduler
  20. python下载网易云MV

热门文章

  1. 全球最大语音识别公司Nuance的衰落与自我救赎
  2. 10.一文了解JAVA反射超详尽知乎
  3. mysql如何字符编码,如何修改MYSQL的字符编码
  4. 刷新按钮_不能忍:用户求微软为Win10 Wi-Fi窗口添加刷新按钮
  5. win102004优化_windows 10新版2004和1909该如何选择
  6. 【linux笔记】vi和vim编辑器,重定向输出,系统命令,进程,管道
  7. 【java笔记】list接口
  8. 递归处理汉诺塔问题(c++/python)
  9. 剑指 Offer II 006. 排序数组中两个数字之和
  10. ps打开闪退_PS教程派 | 水花四溅的草莓切块效果,如此简单!