Kubernetes ConfigMap 与 Secret 管理

小飞兽 Linux 219 次阅读 2026-07-23

Kubernetes ConfigMap 与 Secret 管理

ConfigMap 和 Secret 是 Kubernetes 中用于管理配置数据的两种资源对象。ConfigMap 用于存储非敏感的配置文件、环境变量、命令行参数等配置数据;Secret 用于存储敏感信息如密码、OAuth 令牌、SSH 密钥等。两者都可以在 Pod 中以环境变量或文件挂载的方式使用。

ConfigMap 用法

创建 ConfigMap

# 从文件创建
kubectl create configmap app-config --from-file=config.properties

从环境变量文件创建

kubectl create configmap app-config --from-env-file=env.properties

直接指定键值对

kubectl create configmap app-config --from-literal=DB_HOST=localhost --from-literal=DB_PORT=5432

从目录创建(目录中每个文件对应一个键)

kubectl create configmap app-config --from-file=config/

YAML 定义

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  DB_HOST: "localhost"
  DB_PORT: "5432"
  app.properties: |
    log.level=info
    cache.enabled=true
    max_connections=100

在 Pod 中使用 ConfigMap

apiVersion: v1
kind: Pod
metadata:
  name: app-pod
spec:
  containers:
  • name: app
image: myapp:latest env:
  • name: DB_HOST
valueFrom: configMapKeyRef: name: app-config key: DB_HOST
  • name: DB_PORT
valueFrom: configMapKeyRef: name: app-config key: DB_PORT volumeMounts:
  • name: config
mountPath: /etc/config volumes:
  • name: config
configMap: name: app-config items:
  • key: app.properties
path: app.properties

Secret 用法

Secret 与 ConfigMap 语法类似,但内容经过 Base64 编码(注意:Base64 不是加密,Secret 默认也不加密存储,除非启用加密配置)。

# 创建 Secret
kubectl create secret generic db-credentials 
  --from-literal=username=admin 
  --from-literal=password=secret123

从文件创建

kubectl create secret generic tls-cert --from-file=tls.crt=cert.pem --from-file=tls.key=key.pem

YAML 定义(值需要 Base64 编码)

apiVersion: v1 kind: Secret metadata: name: db-credentials type: Opaque data: username: YWRtaW4= password: c2VjcmV0MTIz

在 Pod 中使用 Secret

apiVersion: v1
kind: Pod
metadata:
  name: app-pod
spec:
  containers:
  • name: app
image: myapp:latest env:
  • name: DB_USER
valueFrom: secretKeyRef: name: db-credentials key: username
  • name: DB_PASS
valueFrom: secretKeyRef: name: db-credentials key: password volumeMounts:
  • name: certs
mountPath: /etc/certs readOnly: true volumes:
  • name: certs
secret: secretName: tls-cert

使用场景对比

    • ConfigMap:数据库连接配置、日志级别、功能开关、不含敏感信息的配置文件
    • Secret:数据库密码、API 密钥、TLS 证书、OAuth Token

    Secret 类型

    # Opaque(通用 Secret,默认类型)
    kubectl create secret generic my-secret --from-literal=key=value
    

    kubernetes.io/tls(TLS 证书)

    kubectl create secret tls my-tls --cert=cert.pem --key=key.pem

    kubernetes.io/dockerconfigjson(Docker 镜像仓库认证)

    kubectl create secret docker-registry my-registry --docker-server=registry.example.com --docker-username=user --docker-password=pass

    Pod 使用私有镜像仓库认证

    kubectl create secret docker-registry my-registry 
      --docker-server=registry.example.com 
      --docker-username=user 
      --docker-password=pass 
      --docker-email=user@example.com
    

    在 Pod spec 中引用

    apiVersion: v1 kind: Pod spec: imagePullSecrets:
  • name: my-registry
containers:
  • name: app
image: registry.example.com/myapp:latest

配置热更新

ConfigMap 或 Secret 更新后,挂载到 Pod 的配置不会自动更新(默认行为)。

启用配置热更新(通过 volume 挂载的方式):
1. 更新 ConfigMap/Secret
kubectl apply -f updated-configmap.yaml

2. Pod 中的文件会周期性更新(默认 1 分钟)
3. 应用检测到配置变化并重新加载

如果需要立即生效,可以重启 Pod:
kubectl rollout restart deployment/app-deployment

注意事项

    • Secret 的数据只是 Base64 编码,不是加密。生产环境应启用 Kubernetes 的加密配置,对 Secret 数据进行加密存储
    • 不要在 Git 仓库中存储 Secret YAML 文件,应使用外部密钥管理工具(如 Vault、AWS Secrets Manager)
    • Pod 引用的 Secret 必须存在,否则 Pod 启动会失败
  • 使用 volume 挂载 Secret 时,每个 key 对应一个文件,文件内容是原始值(未解码)

ConfigMap 和 Secret 是 Kubernetes 应用配置管理的核心,合理使用能让应用配置与代码分离,支持配置的动态更新和安全管理。