官方中文参考文档:
ConfigMap是API对象,用于存放明文(非机密性)数据保存到键值对中,可以使用环境变量、命令行参数或者存储卷方式应用到Pod中,ConfigMap相当于Pod中程序的配置文件,通过修改ConfigMap内容来修改程序的配置。
ConfigMap在设计上不是用来存储大量数据的,所以在ConfigMap中保存的数据不能超过1MiB。
创建 名为 mysql.conf
的 configmap
kubectl create configmap mysql.conf --from-literal=datadir=/var/lib/mysql --from-literal=socket=/var/lib/mysql/mysql.sock
查看 configmap详细信息:
kubectl describe cm mysql.conf
首先创建nginx.conf文件,针对nginx.conf文件创建configmap
vim nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
创建 名为 nginx.conf
的 configmap,使用--from-file
参数指定文件位置
kubectl create configmap nginx.conf --from-file=www=./nginx.conf
查看详细信息:定义key=www,value=./nginx.conf 中的内容
kubectl describe cm nginx.conf
首先创建config目录,目录中创建nginx、mysql配置文件,使用--from-file
参数指定config目录。
mkdir config && cd config
vim nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
vim my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
指定 config
目录创建 configmap
kubectl create cm config --from-file=./config
查看详细信息:key会以目录中文件名进行命名
kubectl describe cm config
创建 db-config
configmap,定义master.cnf
,slave.cnf
Key
cat db-config.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: db-config
labels:
app: db
data:
master.cnf: |
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
server-id=1
slave.cnf: |
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
server-id=2
执行YAML文件:
kubectl apply -f db-config.yaml
查看db-config
configmap详细信息:
kubectl describe cm db-config
上面是通过key/value方式定义配置文件信息,下面来学习一下,如何引用配置文件。
创建名为nginx-config
的configmap:
cat nginx-config.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
labels:
app: nginx
env: uat
data:
nginx.conf: |
user nginx;
worker_processes auto;
error_log /var/log/nginx/nginx-error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
创建deployment资源,容器中引用nginx-config
configmap的nginx.conf
Key
cat web-nginx.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-nginx
labels:
app: nginx
env: uat
spec:
replicas: 1
selector: # 关联标签
matchLabels:
app: nginx
env: uat
strategy:
rollingUpdate: # 更新策略
maxSurge: 2
maxUnavailable: 0
template:
metadata:
labels:
app: nginx
env: uat
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
env:
- name: nginxConfig # 定义环境变量名称
valueFrom:
configMapKeyRef:
name: nginx-config # 指定configmap名称
key: nginx.conf # 指定configmap中Key
执行YAML文件:
kubectl apply -f nginx-config.yaml
kubectl apply -f web-nginx.yaml
查看创建出来的资源信息:
kubectl get pods -l app=nginx
kubectl get configmap nginx-config
进入 被创建Pod中容器,查看变量信息:
kubectl exec -it web-nginx-84989c5546-6clp4 -- /bin/bash
printenv
我们可以在创建Pod资源时使用command,来引用变量,进行启动操作,比如下面启动MongoDB操作。
command: ["/bin/mongod", "--bind_ip=$(变量名)"]
创建名为mysql-config
的configmap,其中定义了两个KEY
cat mysql-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
data:
master.cnf: |
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
server-id=1
slave.cnf: |
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
server-id=2
创建Deployment,通过envfrom方式指定configmap,这种方式会引用configmap中所有的KEY为系统变量,由于只是演示envFrom方式特性,所有我这边使用 busybox
镜像演示,YAML资源清单如下:
cat envfrom_demo.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: envfrom-demo
namespace: default
spec:
replicas: 1
selector:
matchLabels:
name: envfrom-demo
template:
metadata:
labels:
name: envfrom-demo
spec:
containers:
- name: envfrom-demo
image: busybox:1.28.0
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "-c", "sleep 360000"]
envFrom:
- configMapRef:
name: mysql-config # 指定configmap名称
执行YAML文件:
kubectl apply -f mysql-config.yaml
kubectl apply -f envfrom_demo.yaml
查看创建出来的资源信息:
kubectl get pods -l name=envfrom-demo
kubectl get configmap mysql-config
进入容器查看变量信息:
kubectl exec -it envfrom-demo-6cf79bf4bc-4xvcc -- /bin/sh
printenv
前两种都是使用环境变量方式引用配置的不会经常使用,最常用的还是使用卷挂载方式!
创建名为web
的configmap,定义了两个KEY,分别为:nginx_8080.conf、nginx_9090.conf
cat web-config.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: web
data:
nginx_8080.conf: |
server {
listen 8080;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
nginx_9090.conf: |
server {
listen 9090;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
使用nginx镜像,创建Deployment,并使用挂载configmap
cat web.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-nginx
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
volumes:
- name: web-config
configMap: # 定义卷,使用名为web的configmap
name: web
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
volumeMounts:
- name: web-config # 挂载卷
mountPath: /etc/nginx/conf.d/nginx_9090.conf # 挂载路径
subPath: nginx_9090.conf # 只挂载configmap中的nginx_9090.confkey
执行YAML资源清单:
kubectl apply -f web-config.yaml
kubectl apply -f web.yaml
我将配置文件端口改为9090了,我们请求Nginx的9090端口,作为验证:
kubectl get pods -o wide
curl 10.244.235.234:9090
OK,上面可以成功通过9090端口访问到nginx。
1、编辑ConfigMap
kubectl edit cm web
编辑并保存后,对于卷挂载方式,支持热加载(大约等10s)生效,会更新到容器配置中,但对于变量方式并不支持,需要重新删除创建,更改的数据才会生效。
2、删除ConfigMap
kubectl delete cm web
3、configMapKeyRef和envfrom方式引入变量区别?