创建一个ConfigMap
yaml文件如下
# config-test.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test-map
namespace: default
data:
env: |
token = 'xxxxxx'
应用yaml
[root@node1 config]# kubectl apply -f config-test.yaml
configmap/test-map created
在Pod中挂载configMap
apiVersion: v1
kind: Pod
metadata:
name: busybox
spec:
containers:
- name: busybox
image: busybox
command: ['sleep', 'infinity']
volumeMounts:
- name: config-test
mountPath: /test # 将ConfigMap内容挂载到/test目录
volumes:
- name: config-test
configMap:
name: test-map # 使用前面创建的configMap
进入pod查看文件
运行下面的命令可以看到busybox中多了一个/test目录,目录里面有一个env文件,文件内容也是和先前定义的一样
[root@node1 config]# kubectl exec -it busybox -- sh
/ #
/ # ls /test
env
/ # cat /test/env
token = 'xxxxxx'
/ #
创建ConfigMap的几种形式
形式一: 使用目录创建ConfigMap
# 使用目录创建(--fromfile 指定在目录下的所有文件都会被用在ConfigMap里面创建一个键值对,键的名字就是文件名,值就是文件的内容)
kubectl create configmap [configmap名称] --from-file={目录}
创建文件夹
# 创建了一个dir文件夹,里面放了6个文件
[root@node1 dir]# echo "我是文件1" > file1.txt
[root@node1 dir]# echo "我是文件2" > file2.txt
[root@node1 dir]# echo "我是文件3" > file3.txt
[root@node1 dir]# echo "我是文件4" > file4.txt
[root@node1 dir]# echo "我是文件5" > file5.txt
[root@node1 dir]# echo "我是文件6" > file6.txt
[root@node1 dir]# cd ..
[root@node1 config]# tree
.
└── dir
├── file1.txt
├── file2.txt
├── file3.txt
├── file4.txt
├── file5.txt
└── file6.txt
创建ConfigMap
[root@node1 config]# kubectl create configmap test-dir --from-file=./dir
configmap/test-dir created
[root@node1 config]# kubectl describe configmap test-dir
Name: test-dir
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
file1.txt:
----
我是文件1
file2.txt:
----
我是文件2
file3.txt:
----
我是文件3
file4.txt:
----
我是文件4
file5.txt:
----
我是文件5
file6.txt:
----
我是文件6
BinaryData
====
Events: <none>
挂载使用上面创建的configMap
# busybox-dir.yaml
apiVersion: v1
kind: Pod
metadata:
name: busybox-dir
spec:
containers:
- name: busybox-dir
image: busybox
command: ['sleep', 'infinity']
volumeMounts:
- name: config-test
mountPath: /var
volumes:
- name: config-test
configMap:
name: test-dir # 使用前面创建的configMap
```https://blog.csdn.net/u010755471/article/details/125782198
### 应用yaml文件并查看Pod里面的内容
```sh
[root@node1 config]# kubectl exec -it busybox-dir -- sh
/ #
/ # ls /var/
file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt run
/ # cat /var/file1.txt
我是文件1
/ # cat /var/file2.txt
我是文件2
形式二: 使用文件创建ConfigMap
# 使用文件创建(--fromfile 这个参数可以使用多次,你可以使用两次分别指定上个实例中的那两个配置文件,效果就跟指定整个目录是一样的)
kubectl create configmap [configmap名称] --from-file=[文件]
准备nginx配置文件
# nginx.conf
# 默认为 1,表示开启一个业务进程
worker_processes 1;
events {
# 单个业务进程可接受连接数
worker_connections 1024;
}
http {
# 引入 http mime 类型
include mime.types;
# 如果 mime 类型没匹配上,默认使用二进制流的方式传输。
default_type application/octet-stream;
# sendfile() 高效网络传输,也就是数据 0 拷贝。
sendfile on;
keepalive_timeout 65;
server {
# 监听端口号
listen 80;
# 虚拟主机名
server_name localhost;
# 匹配路径
location / {
# 文件根目录
root html;
# 默认页
index index.html index.htm;
}
# 错误码对应页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
将nginx.conf创建成为一个ConfigMap
[root@node1 config]# kubectl create configmap nginx-conf --from-file=./nginx.conf
configmap/nginx-conf created
[root@node1 config]# kubectl describe configmap nginx-conf
Name: nginx-conf
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
nginx.conf:
----
# 默认为 1,表示开启一个业务进程
worker_processes 1;
events {
# 单个业务进程可接受连接数
worker_connections 1024;
}
http {
# 如果 mime 类型没匹配上,默认使用二进制流的方式传输。
default_type application/octet-stream;
# sendfile() 高效网络传输,也就是数据 0 拷贝。
sendfile on;
keepalive_timeout 65;
server {
# 监听端口号
listen 80;
# 虚拟主机名
server_name localhost;
# 匹配路径
location / {
# 文件根目录
root html;
# 默认页
index index.html index.htm;
}
# 错误码对应页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
BinaryData
====
Events: <none>
将上面的configmap应用到运行nginx的Pod里面
# nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: nginx-test
mountPath: /etc/nginx
volumes:
- name: nginx-test
configMap:
name: nginx-conf # 引用前面创建的nginx-conf
应用yaml文件并查看Pod中文件内容
运行下面的命令可以看到,nginx启动的时候使用了我们指定的配置文件
[root@node1 config]# kubectl exec -it nginx -- sh
# cat /etc/nginx/nginx.conf
# 默认为 1,表示开启一个业务进程
worker_processes 1;
events {
# 单个业务进程可接受连接数
worker_connections 1024;
}
http {
# 如果 mime 类型没匹配上,默认使用二进制流的方式传输。
default_type application/octet-stream;
# sendfile() 高效网络传输,也就是数据 0 拷贝。
sendfile on;
keepalive_timeout 65;
server {
# 监听端口号
listen 80;
# 虚拟主机名
server_name localhost;
# 匹配路径
location / {
# 文件根目录
root html;
# 默认页
index index.html index.htm;
}
# 错误码对应页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
#
形式三: 从字面值创建configmap
# 从字面值创建
kubectl create configmap [configmap名称] --from-literal=[键值对]
示例
#使用key1 = config1和key2 = config2创建一个名为my-config的configmap
kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
形式四: 使用指定的key创建
与前面的形式二类似,只是这里指定了键的名字
# 使用指定的keys创建一个名为my-config的configmap
kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
将ConfigMap中的内容映射到Pod的环境变量里面
创建configmap的yaml文件
# config-env.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very # 键为special.how, 值为very
---
apiVersion: v1
kind: ConfigMap
metadata:
name: env-config
namespace: default
data:
log-level: INFO # 键为log-level,值为INFO
应用文件
[root@node1 config]# kubectl apply -f config-env.yaml
configmap/special-config created
configmap/env-config created
将configmap中的内容映射为pod的环境变量
# config-env-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: busybox-env
spec:
containers:
- name: busybox-env
image: busybox
command: ["sleep", "infinity"]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: env-config # 使用前面创建的configmap
key: log-level # 使用该configmap中键为log-level的值
应用pod的yaml文件,并查看env
[root@node1 config]# kubectl apply -f config-env-pod.yaml
[root@node1 config]# kubectl exec -it busybox-env -- sh
/ #
/ # env
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
LOG_LEVEL=INFO # 这里是注入的环境变量
HOSTNAME=busybox-env
SHLVL=1
HOME=/root
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
SPECIAL_LEVEL_KEY=very # 这里是注入的环境变量
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/
将ConfigMap中的内容映射到Pod的环境变量里面方式二
如上一步所示的一个一个引入到环境变量很麻烦,接下来将一个configmap中所有的键值对一次性注入进环境变量
# nginx-env.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-conf
namespace: default
data:
ENV_1: "111.111.111.111" # 创建多个键值对
ENV_2: "2.2.2.2"
ENV_3: "3.3.3.3"
ENV_NAME: "test"
应用configmap的yaml文件
[root@node1 config]# kubectl apply -f nginx-env.yaml
configmap/nginx-conf created
向Pod中注入变量
# nginx-env-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-env-pod
spec:
containers:
- name: nginx-env-pod
image: nginx
envFrom: # 使用envFrom可以将整个confimap中的键值对引入
- configMapRef:
name: nginx-conf # 前面创建的configmap的名字
应用Pod的yaml文件
[root@node1 config]# kubectl apply -f nginx-env-pod.yaml
pod/nginx-env-pod created
进入Pod查看env
[root@node1 config]# kubectl exec -it nginx-env-pod -- bash
root@nginx-env-pod:/# env
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_PORT=443
ENV_NAME=test
HOSTNAME=nginx-env-pod
PWD=/
PKG_RELEASE=1~bookworm
HOME=/root
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
NJS_VERSION=0.8.0
TERM=xterm
ENV_3=3.3.3.3
ENV_2=2.2.2.2
ENV_1=111.111.111.111 # 可以看到这些注入的环境变量
SHLVL=1
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PORT=443
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NGINX_VERSION=1.25.2
_=/usr/bin/env