0Pricing
Docker & Kubernetes for Developers · 课时

使用 ConfigMaps 与 Secret 管理配置

在 Kubernetes 中使用 ConfigMaps 和 Secret 安全地管理应用配置与敏感数据。

使用 ConfigMaps 与 Secret 管理配置 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Docker & Kubernetes for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Docker & Kubernetes for Developers 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Configuration Challenges

Managing application configuration and sensitive data can be tricky, especially in dynamic environments like Kubernetes.

Hardcoding values into images makes them less reusable. Storing secrets directly in Git is a security risk.

Kubernetes offers two powerful resources to solve these challenges: ConfigMaps and Secrets.

Meet ConfigMaps

A ConfigMap is an API object used to store non-sensitive configuration data in key-value pairs.

Think of it as a central place for settings like database hostnames, logging levels, or API endpoints.

  • Separates configuration from application code.
  • Allows easy updates without rebuilding images.
  • Can be consumed as environment variables or mounted files.

ConfigMap: Literal Values

You can create a ConfigMap directly from literal key-value pairs using YAML. This is great for simple, direct settings.

Here's an example for an application's settings:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-settings
data:
  log_level: INFO
  feature_flag_a: "true"
  api_url: http://backend-service/api

ConfigMap: From Files

For more complex configurations, you can create a ConfigMap from an entire file.

If you have a config.properties file, its content can be directly embedded into the ConfigMap. The resulting ConfigMap would look like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config-from-file
data:
  config.properties: |
    database.host=db-service
    database.port=5432
    application.name=MyWebApp

ConfigMap as Env Vars

The most common way to use a ConfigMap is by injecting its data as environment variables into your Pods.

This allows your application to read configuration values directly.

apiVersion: v1
kind: Pod
metadata:
  name: my-app-pod
spec:
  containers:
  - name: my-container
    image: nginx
    env:
    - name: LOG_LEVEL
      valueFrom:
        configMapKeyRef:
          name: app-settings
          key: log_level
    - name: API_URL
      valueFrom:
        configMapKeyRef:
          name: app-settings
          key: api_url

ConfigMap as Volume Mounts

ConfigMap data can also be mounted as files into a Pod's filesystem. This is ideal for applications that read configuration from files.

Each key in the ConfigMap becomes a file in the specified mountPath. For example, log_level would be at /etc/config/log_level.

apiVersion: v1
kind: Pod
metadata:
  name: my-app-pod-volume
spec:
  containers:
  - name: my-container
    image: nginx
    volumeMounts:
    - name: config-volume
      mountPath: "/etc/config"
  volumes:
  - name: config-volume
    configMap:
      name: app-settings

Introducing Secrets

A Secret is similar to a ConfigMap but is designed for sensitive data like passwords, API keys, or TLS certificates.

Secrets are stored in Kubernetes in base64 encoded format, but this is NOT encryption. It's just encoding for safe transport.

  • Provides a mechanism to distribute sensitive data.
  • Accessed like ConfigMaps (env vars or volume mounts).
  • Should always be managed with care and access controls.

Secret: Literal Values

Secrets are created similarly to ConfigMaps, but their data values are base64 encoded. This encoding is for safe transport, not encryption.

For example, if your password is "my-secure-password", its base64 encoding is "bXktc2VjdXJlLXBhc3N3b3Jk".

apiVersion: v1
kind: Secret
metadata:
  name: db-credentials
type: Opaque
data:
  username: dXNlcg== # 'user' base64 encoded
  password: bXktc2VjdXJlLXBhc3N3b3Jk # 'my-secure-password' base64 encoded

Secret as Env Vars

Secrets can be consumed as environment variables, just like ConfigMaps. Kubernetes automatically decodes the base64 value for the container.

This makes sensitive data available to your application without hardcoding it directly in the image.

apiVersion: v1
kind: Pod
metadata:
  name: my-db-app
spec:
  containers:
  - name: my-container
    image: my-app:1.0
    env:
    - name: DB_USERNAME
      valueFrom:
        secretKeyRef:
          name: db-credentials
          key: username
    - name: DB_PASSWORD
      valueFrom:
        secretKeyRef:
          name: db-credentials
          key: password

Secret as Volume Mounts

Mounting Secrets as files is often preferred for sensitive data, as it limits the exposure of the secret to the application process.

The mounted files will contain the decoded (plain-text) secret values, and you can set them to be read-only.

apiVersion: v1
kind: Pod
metadata:
  name: my-secure-app
spec:
  containers:
  - name: my-container
    image: my-app:1.0
    volumeMounts:
    - name: secret-volume
      mountPath: "/etc/secrets"
      readOnly: true
  volumes:
  - name: secret-volume
    secret:
      secretName: db-credentials

ConfigMaps vs. Secrets

You need to store an API key for a third-party service and a configuration setting for your application's logging level. Which Kubernetes resources would you use for each?

Recap: Config & Secrets

We've learned how ConfigMaps and Secrets help manage configuration and sensitive data in Kubernetes.

  • ConfigMaps store non-sensitive key-value pairs.
  • Secrets store sensitive data, base64 encoded (not encrypted).
  • Both can be consumed as environment variables or mounted files in Pods.
  • Using them separates configuration from application logic, improving flexibility and security.

常见问题解答

「使用 ConfigMaps 与 Secret 管理配置」课时是免费的吗?

是的 — 「使用 ConfigMaps 与 Secret 管理配置」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。

「使用 ConfigMaps 与 Secret 管理配置」这节课中我会学到什么?

在 Kubernetes 中使用 ConfigMaps 和 Secret 安全地管理应用配置与敏感数据。 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Docker & Kubernetes for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「使用 ConfigMaps 与 Secret 管理配置」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?

能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 持久卷与持久卷声明
  2. 使用 StatefulSets 管理有状态应用
  3. 使用 ConfigMaps 与 Secret 管理配置
  4. 存储类与动态供给
← 返回 Docker & Kubernetes for Developers