全栈工程师开发手册 (作者:栾鹏)
架构系列文章
先准备你自己的文件,比如为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-config
namespace: v0930
labels:
app: flfrontend
chart: flfrontend-0.1.0
release: flw
heritage: 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-flfrontend
namespace: v0930
labels:
app: flfrontend
chart: flfrontend-0.1.0
release: flw
heritage: Tiller
spec:
selector:
matchLabels:
app: flfrontend
release: flw
replicas: 1
template:
metadata:
labels:
app: flfrontend
release: flw
spec:
volumes:
- name: config
configMap:
name: flw-config
containers:
- name: flfrontend
image: xxxxxxx
imagePullPolicy: IfNotPresent
command: ["sleep","20000"]
ports:
- containerPort: 80
volumeMounts:
- name: config
mountPath: /var/www/html/dist/static/config.js
subPath: config.js
readOnly: 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-config
Name: 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=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: 2016-02-18T18:52:05Z
name: game-config-2
namespace: default
resourceVersion: "516"
selfLink: /api/v1/namespaces/default/configmaps/game-config-2
uid: 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-2
Name: 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.properties
kubectl get configmaps game-config-3 -o yaml
apiVersion: v1
data:
game-special-key: |
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: 2016-02-18T18:54:22Z
name: game-config-3
namespace: default
resourceVersion: "530"
selfLink: /api/v1/namespaces/default/configmaps/game-config-3
uid: 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: very
special.type: charm
kind: ConfigMap
metadata:
creationTimestamp: 2016-02-18T19:14:38Z
name: special-config
namespace: default
resourceVersion: "651"
selfLink: /api/v1/namespaces/default/configmaps/special-config
uid: dadce046-d673-11e5-8cd0-68f728db1985
对configmap中的不同文件分别处理
apiVersion: v1
kind: ConfigMap
metadata:
name: testmap
data:
test.file: |
this is a test strings
test.file2: |
this is the 2nd strings
---
apiVersion: v1
kind: Pod
metadata:
name: pod-test
spec:
containers:
- name: test
image: busybox
tty: true
command: ["sh"]
volumeMounts:
- name: testdir
mountPath: /root/test.map1
subPath: test.map1
- name: testdir
mountPath: /etc/test.map2
subPath: test.map2
volumes:
- name: testdir
configMap:
name: testmap
items:
- key: test.file
path: test.map1
- key: test.file2
path: 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:38Z
name: example-config
namespace: default
data:
# example of a simple property defined using --from-literal
example.property.1: hello
example.property.2: world
# example of a complex property defined using --from-file
example.property.file: |-
property.1=value-1
property.2=value-2
property.3=value-3