doina

一个小菜鸟运维工程师.

kubernetes 特殊存储卷ConfigMap

理解ConfigMap

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

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

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

ConfigMap相当于配置中心,用于Pod读取配置,避免每次只因为修改一个参数而升级镜像

ConfigMap必须在Pod之前被创建,
如果ConfigMap定义了Namespace, 则只有相同的Namespace的Pod才可以使用。

命令行创建configMap语法:

# kubectl create configmap myConfig --from-literal=  --help
  # 用指定的键(而不是磁盘上的文件基名称)创建名为my config的新configmap
  kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt

  # 使用key1=config1和key2=config2创建名为my config的新configMap
  kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2

  # 从文件中的key=value对创建一个名为my config的新configmap
  kubectl create configmap my-config --from-file=path/to/bar

  # 从env文件创建名为my config的新configmap
  kubectl create configmap my-config --from-env-file=path/to/bar.env

创建一个名为nginx-config的configMap,在命令行中给定键和值

[root@master configMap]# kubectl create configmap nginx-config --from-literal=server_port=80 --from-literal=server_name=baiyongjie.com
configmap/nginx-config created

[root@master configMap]# kubectl get configmaps 
NAME           DATA   AGE
nginx-config   2      31s

[root@master configMap]# kubectl describe configmaps nginx-config 
Name:         nginx-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
server_name:
----
baiyongjie.com
server_port:
----
80
Events:  <none>

通过文件创建configmap

[root@master configMap]# ls nginx-blog/www.conf 
nginx-blog/www.conf

[root@master configMap]# cat nginx-blog/www.conf 
server {
        server_name blog.baiyongjie.com;
        listen 80;
        root /usr/local/nginx/html;
}

[root@master configMap]# kubectl create configmap nginx-blog --from-file=nginx-blog/www.conf 
configmap/nginx-blog created

[root@master configMap]# kubectl describe configmaps nginx-blog 
Name:         nginx-blog
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
www.conf:
----
server {
  server_name blog.baiyongjie.com;
  listen 80;
  root /usr/local/nginx/html;
}

Events:  <none>

yaml文件创建configMap

[root@master configmaps]# cat volume-configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap-test
  namespace: default
data:
  name: baiyongjie
  sex: man

在Pod中使用ConfigMap,作为容器的启动参数

# 定义ConfigMap资源
[root@master configmaps]# cat volume-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap-test  #ConfigMap名称,在pod中需要用到
  namespace: default
data:
  name: baiyongjie
  sex: man
  age: "26"

[root@master configmaps]# kubectl apply -f volume-configmap.yaml 
configmap/configmap-test created

[root@master configmaps]# kubectl describe configmaps configmap-test 
Name:         configmap-test
Namespace:    default
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"age":"26","name":"baiyongjie","sex":"man"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"configmap-t...

Data
====
name:
----
baiyongjie
sex:
----
man
age:
----
26
Events:  <none>

# 定义Pod资源
[root@master configmaps]# vim pod-configmap.yml 
apiVersion: v1
kind: Pod
metadata:
  name: configmap-test-pod
spec:
  containers:
    - name: configmap-test-pod
      image: busybox
      imagePullPolicy: IfNotPresent
      command: ["/bin/sh","-c", "while true; do echo $(date) Name: $(name) Sex: $(sex) Age: $(age); sleep 5;done"]  #每五秒输出一次定义的变量, 为了观察方便写成了while循环
      env:
        - name: name
          valueFrom:
            configMapKeyRef:
              name: configmap-test
              key: name
        - name: sex
          valueFrom:
            configMapKeyRef:
              name: configmap-test
              key: sex
        - name: age
          valueFrom:
            configMapKeyRef:
              name: configmap-test
              key: age

[root@master configmaps]# kubectl apply  -f pod-configmap.yml 
pod/configmap-test-pod created

# 可以看到ConfigMap中定义的变量已经被引用了
[root@master configmaps]# kubectl logs -f configmap-test-pod
Wed Mar 27 08:22:54 UTC 2019 Name: baiyongjie Sex: man Age: 26
Wed Mar 27 08:22:59 UTC 2019 Name: baiyongjie Sex: man Age: 26
Wed Mar 27 08:23:04 UTC 2019 Name: baiyongjie Sex: man Age: 26
Wed Mar 27 08:23:09 UTC 2019 Name: baiyongjie Sex: man Age: 26

在Pod中使用ConfigMap,当做卷挂载

# cat pod-configmap-volume.yml
apiVersion: v1
kind: Pod
metadata:
  name: configmap-test-pod
spec:
  containers:
  - name: configmap-test-pod
    image: nginx
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config/configmap
      readOnly: true
  volumes:
    - name: config-volume
      configMap:
        name: configmap-test

# kubectl apply -f pod-configmap.yml 
pod/configmap-test-pod created

# kubectl exec -it configmap-test-pod  -- /bin/bash
root@configmap-test-pod:/# cd /etc/config/configmap/     


root@configmap-test-pod:/etc/config/configmap# ls -l
total 0
lrwxrwxrwx. 1 root root 10 Mar 28 02:01 age -> ..data/age
lrwxrwxrwx. 1 root root 11 Mar 28 02:01 name -> ..data/name
lrwxrwxrwx. 1 root root 10 Mar 28 02:01 sex -> ..data/sex

root@configmap-test-pod:/etc/config/configmap# for i in `ls /etc/config/configmap*`;do cat $i;echo ; done
26
baiyongjie
man

将ConfigMap中的键对挂载到Pod当文件使用

# 准备html文件
# cat index.html 
<html>
<meta http-equiv="refresh" content="0;url=https://baiyongjie.com">
</html>

# 创建ConfigMap,从文件中导入,key为test.html
# kubectl create configmap configmap-nginx --from-file=test.html=index.html
configmap/configmap-nginx created

# 查看ConfigMap详细信息
# kubectl describe configmaps configmap-nginx 
Name:         configmap-nginx
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
test.html:
----
<html>
<meta http-equiv="refresh" content="0;url=https://baiyongjie.com">
</html>

# 定义Pod资源
# cat pod-configmap-volume-file.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: configmap-test-pod-file
spec:
  containers:
  - name: configmap-test-pod-file
    image: nginx
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: config-volume-nginx
      mountPath: /usr/share/nginx/html/
      readOnly: true
  volumes:
    - name: config-volume-nginx
      configMap:
        name: configmap-nginx

# 启动pod
# kubectl apply -f pod-configmap-volume-file.yaml
pod/configmap-test-pod-file created

# 查看启动后pod的IP地址
[root@master nginx-configmaps]# kubectl get pods -o wide
NAME                      READY   STATUS    RESTARTS   AGE   IP             NODE     NOMINATED NODE   READINESS GATES
configmap-test-pod-file   1/1     Running   0          30s   10.244.0.245   master   <none>           <none>

# 进入pod查看文件是否被挂载
# kubectl exec -it configmap-test-pod-file  -- /bin/bash
root@configmap-test-pod-file:/# ls -l /usr/share/nginx/html/
total 0
lrwxrwxrwx. 1 root root 16 Mar 28 02:57 test.html -> ..data/test.html

# 查看挂载的文件内容
root@configmap-test-pod-file:/# cat /usr/share/nginx/html/test.html
<html>
<meta http-equiv="refresh" content="0;url=https://baiyongjie.com">
</html>

# 请求pod中挂载的test.html,查看效果
# curl 10.244.0.245/test.html
<html>
<meta http-equiv="refresh" content="0;url=https://baiyongjie.com">
</html>

将ConfigMap中的所有键值对配置为Pod环境变量

# vim pod-configmap-env.yml
apiVersion: v1
kind: Pod
metadata:
  name: configmap-test-pod-env
spec:
  containers:
    - name: configmap-test-pod-env
      image: busybox
      imagePullPolicy: IfNotPresent
      command: ["/bin/sh","-c","env"]
      envFrom:
      - configMapRef:
          name: configmap-test

# kubectl apply -f pod-configmap-env.yml
pod/configmap-test-pod-env created

# kubectl logs configmap-test-pod-env |grep -E "name|sex|age"
age=26
sex=man
name=baiyongjie
点赞

发表评论

电子邮件地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据