K8S conffigMap
使用目录创建 ConfigMap
cd ~
mkdir ConfigMap
cd ConfigMap/dir -p
cd ConfigMap/dir
vi game.propertis
eneies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
vi ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
# 以目录形式创建 ConfigMap
kubectl create configmap game-config --from-file=../dir/
# 查看 ConfigMap
kubectl get cm
NAME DATA AGE
game-config 2 6s
# 查看 ConfigMap 的详细信息
kubectl get cm -o yaml
apiVersion: v1
items:
- apiVersion: v1
data:
game.propertis: |
eneies=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: "2020-03-21T08:08:52Z"
name: game-config
namespace: default
resourceVersion: "85704"
selfLink: /api/v1/namespaces/default/configmaps/game-config
uid: 83af28e4-b458-4ce0-a7bf-f1f60eff40bd
kind: List
metadata:
resourceVersion: ""
selfLink: ""
# 查看 ConfigMap 的详细信息
kubectl describe cm game-config
Name: game-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
game.propertis:
----
eneies=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>
使用文件创建 ConfigMap
kubectl create configmap game-config-2 --from-file=game.propertis
# 查看 ConfigMap
kubectl get cm
NAME DATA AGE
game-config 2 2m46s
game-config-2 1 3s
# 查看 ConfigMap 的详细信息
kubectl get configmaps game-config-2
NAME DATA AGE
game-config-2 1 27s
# 查看 ConfigMap 的详细信息
kubectl get configmaps game-config-2 -o yaml
apiVersion: v1
data:
game.propertis: |
eneies=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: "2020-03-21T08:11:35Z"
name: game-config-2
namespace: default
resourceVersion: "85959"
selfLink: /api/v1/namespaces/default/configmaps/game-config-2
uid: df1e50e8-70ef-4675-8b05-2316c48bea6d
使用字面值创建 ConfigMap
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
# 查看 ConfigMap 的详细信息
kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
special.how: very
special.type: charm
kind: ConfigMap
metadata:
creationTimestamp: "2020-03-21T08:12:58Z"
name: special-config
namespace: default
resourceVersion: "86096"
selfLink: /api/v1/namespaces/default/configmaps/special-config
uid: cd48e00b-df97-4631-962a-b5404c039f03
使用 ConfigMap 来代替环境变量
kubectl delete cm game-config game-config-2 special-config
# 创建 ConfigMap
vi special-env-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
data:
special.how: very
special.type: charm
---
apiVersion: v1
kind: ConfigMap
metadata:
name: env-config
data:
log_level: INFO
kubectl apply -f special-env-config.yaml
kubectl get cm
NAME DATA AGE
env-config 1 19m
special-config 2 19m
vi dapi-test-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: wangyanglinux/myapp:v1
command: [ "/bin/sh", "-c", "env" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never
kubectl apply -f dapi-test-pod.yaml
kubectl get pod
NAME READY STATUS RESTARTS AGE
dapi-test-pod 0/1 Completed 0 6m
kubectl logs dapi-test-pod|grep SPECIAL
SPECIAL_TYPE_KEY=charm
SPECIAL_LEVEL_KEY=very
使用envFrom会在Pod环境中将ConfigMap中所有定义的key=value自动生成为环境变量
使用 ConfigMap 设置命令行参数
kubectl delete -f dapi-test-pod.yaml
vi dapi-test-pod2.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: wangyanglinux/myapp:v1
command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never
# 查看日志
kubectl logs dapi-test-pod
very charm
在数据卷里面使用 ConfigMap
最基本的就是将文件填入数据卷,在这个文件中,键就是文件名,键值就是文件内容。
kubectl delete -f dapi-test-pod2.yaml
vi dapi-test-pod3.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: wangyanglinux/myapp:v1
command: [ "/bin/sh", "-c", "sleep 600s" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never
kubectl apply -f dapi-test-pod3.yaml
kubectl exec -it dapi-test-pod -- /bin/sh
/ # cd /etc/config/
/etc/config # ls
special.how special.type
ConfigMap 热更新
kubectl delete -f dapi-test-pod3.yaml
vi dapi-test-pod4.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 1
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: wangyanglinux/myapp:v1
ports:
- containerPort: 80
volumeMounts:
- name: env-volume
mountPath: /etc/config
volumes:
- name: env-volume
configMap:
name: env-config
kubectl apply -f dapi-test-pod4.yaml
kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-68c98ffd69-4c2c9 1/1 Running 0 7m33s
kubectl edit configmap env-config
# 把 INFO 改成 DEBUG
#(这里的实验我并没有成功修改,暂时没找到原因)
kubectl exec my-nginx-68c98ffd69-4c2c9 -it -- cat /etc/config/log_level
configmap 挂载到 pod 中
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: echat-business-cm
data:
env.js: |
;(function (win) {
win._env = {
env: 'test',
h5MallHost: 'http://testm.yanpin.cn/',
}
})(window)
deployment 中部分定义
volumes:
- name: env-volume
configMap:
name: echat-business-cm
items:
- key: env.js
path: env.js
volumeMounts:
- mountPath: /usr/share/nginx/html/js/env.js
name: env-volume
subPath: env.js
效果如下: