Kubernetes 中使用 ConfigMap 挂载的方式主要有以下几种:

  1. 环境变量方式挂载:可以将 ConfigMap 中的值以环境变量的形式注入到容器中,例如:
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: nginx
      env:
        - name: MY_VAR
          valueFrom:
            configMapKeyRef:
              name: my-configmap
              key: my-key
  1. 卷方式挂载:可以将 ConfigMap 中的值以文件的形式挂载到容器中,例如:
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  volumes:
    - name: my-volume
      configMap:
        name: my-configmap
  containers:
    - name: my-container
      image: nginx
      volumeMounts:
        - name: my-volume
          mountPath: /etc/my-config
  1. 配置文件方式挂载:可以将 ConfigMap 中的值以配置文件的形式挂载到容器中,例如:
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  my-config.properties: |
    key1=value1
    key2=value2
---
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: nginx
      volumeMounts:
        - name: my-volume
          mountPath: /etc/my-config
      command: ['/bin/sh']
      args: ['-c', 'cat /etc/my-config/my-config.properties']
  volumes:
    - name: my-volume
      configMap:
        name: my-configmap
        items:
          - key: my-config.properties
            path: my-config.properties

以上是三种常见的 ConfigMap 挂载方式的示例。

Kubernetes ConfigMap 挂载方式详解及实例

原文地址: https://www.cveoy.top/t/topic/nlMe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录