configmap配置的几种方式
# deployment的配置
- 针对单个文件
#只展示部分内容
spec:
containers:
- name: php
image: docker-images/pic:v1
ports:
- containerPort: 80
volumeMounts:
- name: database-config
mountPath: /var/www/html/config/database.php
subPath: database.php
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
volumes:
- name: database-config
configMap:
name: app-config-files
items:
- key: database.php
path: database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
针对目录的配置
spec: containers: - name: php image: docker-images/pic:v1 ports: - containerPort: 80 volumeMounts: - name: app-config mountPath: /var/www/html/config readOnly: true resources: requests: memory: "128Mi" cpu: "100m" limits: memory: "256Mi" cpu: "200m" volumes: - name: app-config configMap: name: app-config-files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21配置敏感数据
apiVersion: v1
kind: Secret
metadata:
name: app-secret-config
type: Opaque
data:
database.php: |
<?php
return [
'mysql' => [
'host' => 'mysql-service',
'port' => '3306',
'database' => 'myapp',
'username' => base64_encode('username'),
'password' => base64_encode('password'),
'charset' => 'utf8mb4',
],
];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 创建configmap的几种方式
# 1. 从单个文件创建 ConfigMap
假设你有一个名为 config.properties
的文件,其内容如下:
key1=value1
key2=value2
1
2
2
你可以使用以下命令快速创建一个 ConfigMap
,将该文件的内容作为一个键值对:
kubectl create configmap my-config --from-file=config.properties
1
执行后,my-config
将包含一个名为 config.properties
的键,文件内容将作为值:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
config.properties: |
key1=value1
key2=value2
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 2. 从目录创建 ConfigMap
假设你有一个目录 /path/to/config/
,该目录中包含多个配置文件,下面是该目录的文件列表:
/path/to/config/
├── config1.properties
├── config2.properties
1
2
3
2
3
config1.properties
内容:
key1=value1
key2=value2
1
2
2
config2.properties
内容:
key3=value3
key4=value4
1
2
2
使用以下命令将整个目录的内容创建为 ConfigMap
:
kubectl create configmap my-config --from-file=/path/to/config/
1
执行后,my-config
将包含以下内容:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
config1.properties: |
key1=value1
key2=value2
config2.properties: |
key3=value3
key4=value4
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 3. 从多个文件创建 ConfigMap
假设你有两个独立的配置文件 config1.properties
和 config2.properties
,分别位于 /path/to/config1/
和 /path/to/config2/
:
/path/to/config1/config1.properties
内容:
key1=value1
key2=value2
1
2
2
/path/to/config2/config2.properties
内容:
key3=value3
key4=value4
1
2
2
你可以通过以下命令将它们分别添加到 ConfigMap
中:
kubectl create configmap my-config --from-file=/path/to/config1/config1.properties --from-file=/path/to/config2/config2.properties
1
执行后,my-config
将包含:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
config1.properties: |
key1=value1
key2=value2
config2.properties: |
key3=value3
key4=value4
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 总结
- 从单个文件创建:将一个文件的内容作为
ConfigMap
键值对。 - 从目录创建:将目录中的所有文件(以文件名作为键,文件内容作为值)创建为
ConfigMap
。 - 从多个文件创建:将多个文件分别作为独立的键值对添加到
ConfigMap
中。
这些是快速根据不同场景创建 ConfigMap
的方式。


上次更新: 2025/02/18, 09:32:44