ConfigMap
kubernetes中非常重要的资源对象:ConfigMap ,在实际工作中许多应用经常会有从配置文件、命令行参数或者环境变量中读取一些配置信息,这些配置信息我们肯定不会直接写死到应用程序中去的,比如你一个应用连接一个redis 服务,下一次想更换一个,还得重新去修改代码,重新制作一个镜像,这肯定是不可取的,而ConfigMap 就给我们提供了向容器中注入配置信息的能力,不仅可以用来保存单个属性,也可以用来保存整个配置文件,比如我们可以用来配置一个redis服务的访问地址,也可以用来保存整个redis 的配置文件。
创建
ConfigMap 资源对象使用key-value 形式的键值对来配置数据,这些数据可以在 Pod 里面使用,ConfigMap 可以比较方便的处理一些非敏感的数据,比如密码之类的还是需要使用Secrets来进行管理。下面举个例子说明下ConfigMap 的使用方法:
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-demo1
data:
db.host: 11.11.11.11
db.port: 123
使用
ConfigMap 的配置数据可以通过很多种方式在Pod 里使用,主要有以下几种方式:
- 设置环境变量的值
- 在容器里设置命令行参数
- 在数据卷里面创建config文件
使用ConfigMap 来填充我们的环境变量:
apiVersion: v1
kind: Pod
metadata:
name: testcm1-pod
spec:
containers:
- name: testcm1
image: busybox
command: [ "/bin/sh", "-c", "env" ]
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: cm-demo1
key: db.host
- name: DB_PORT
valueFrom:
configMapKeyRef:
name: cm-demo1
key: db.port
envFrom:
- configMapRef:
name: cm-demo1
[root@master ~]# kubectl logs testcm1-pod
PORT=123
HOST=111.111.111.111
db.host=111.111.111.111
db.port=123
卷挂载
非常常见的使用ConfigMap 的方式:通过数据卷使用,在数据卷里面使用ConfigMap ,就是将文件填入数据卷,在这个文件中,键就是文件名,键值就是文件内容:
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-demo1
data:
db.host: 11.11.11.11
db.port: 123
---
apiVersion: v1
kind: Pod
metadata:
name: testcm1-pod
spec:
containers:
- name: testcm1
image: busybox
command: [ "/bin/sh", "-c", "cat /root/config/db.host; cat /root/config/db.port" ]
volumMounts:
- name: abc
mountPath: /root/config
volumes:
- name: abc
configMap:
name: cm-demo1
[root@master ~]# kubectl logs testcm1-pod
111.111.111.111 123[root@master ~]#
可以在 ConfigMap 值被映射的数据卷里去控制路径以及配置,如下Pod 定义:
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-demo1
data:
mysql.conf: |
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
# include all files from the config directory
#
!includedir /etc/my.cnf.d
nginx.conf: |
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
fastcgi_buffer_size 128k;
fastcgi_buffers 32 32k;
fastcgi_pass 127.0.0.1:9000;
root html;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
}
}
---
apiVersion: v1
kind: Pod
metadata:
name: testcm1-pod
spec:
containers:
- name: testcm1
image: busybox
command: [ "/bin/sh", "-c", "cat /root/config/nginx/nginx.conf" ]
volumeMounts:
- name: abc
mountPath: /root/config
volumes:
- name: abc
configMap:
name: cm-demo1
items:
- key: nginx.conf
path: nginx/nginx.conf
[root@master ~]# kubectl logs testcm1-pod
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
fastcgi_buffer_size 128k;
fastcgi_buffers 32 32k;
fastcgi_pass 127.0.0.1: 9000;
root html;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
}
}
另外需要注意的是,当ConfigMap 以数据卷的形式挂载进Pod 的时,这时更新ConfigMap (或删掉重建ConfigMap ),Pod 内挂载的配置信息会热更新。这时可以增加一些监测配置文件变更的脚本,然后reload 对应服务。