任务

任务
管理集群
Debug DNS 方案
Enabling Service Topology (EN)
IP Masquerade Agent 用户指南
Kubernetes 云管理控制器
Safely Drain a Node while Respecting the PodDisruptionBudget (EN)
为 Kubernetes 运行 etcd 集群
为系统守护进程预留计算资源
为节点发布扩展资源
使用 CoreDNS 进行服务发现
使用 KMS 提供商进行数据加密
使用 Kubernetes API 访问集群
关键插件 Pod 的调度保证
启用端点切片
命名空间演练
在 Kubernetes 集群中使用 NodeLocal DNSCache
在 Kubernetes 集群中使用 sysctl
在实时集群上重新配置节点的 Kubelet
声明网络策略
开发云控制器管理器
控制节点上的 CPU 管理策略
控制节点上的拓扑管理策略
搭建高可用的 Kubernetes Masters
改变默认 StorageClass
更改 PersistentVolume 的回收策略
自定义 DNS 服务
访问集群上运行的服务
通过命名空间共享集群
通过配置文件设置 Kubelet 参数
配置 API 对象配额
配置多个调度器
配置资源不足时的处理方式
限制存储消耗
集群 DNS 服务自动伸缩
集群安全
集群管理
静态加密 Secret 数据
用插件扩展 kubectl
管理巨页(HugePages)
调度 GPUs

Edit This Page

使用 Secret 安全地分发凭证

本文展示如何安全地将敏感数据(如密码和加密密钥)注入到 Pods 中。

准备开始

你必须拥有一个 Kubernetes 的集群,同时你的 Kubernetes 集群必须带有 kubectl 命令行工具。 如果你还没有集群,你可以通过 Minikube 构建一 个你自己的集群,或者你可以使用下面任意一个 Kubernetes 工具构建:

要获知版本信息,请输入 kubectl version.

将 secret 数据转换为 base-64 形式

假设用户想要有两条 secret 数据:用户名 my-app 和密码 39528$vdg7Jb。 首先使用 Base64 编码 将用户名和密码转化为 base-64 形式。 这里是一个 Linux 示例:

```shell
echo -n 'my-app' | base64
echo -n '39528$vdg7Jb' | base64
```

结果显示 base-64 形式的用户名为 bXktYXBw, base-64 形式的密码为 Mzk1MjgkdmRnN0pi

创建 Secret

这里是一个配置文件,可以用来创建存有用户名和密码的 Secret:

pods/inject/secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: test-secret
data:
  username: bXktYXBw
  password: Mzk1MjgkdmRnN0pi
  1. 创建 Secret

    kubectl create -f https://k8s.io/examples/pods/inject/secret.yaml
    注意: 如果想要跳过 Base64 编码的步骤,可以使用 kubectl create secret 命令来创建 Secret:
    kubectl create secret generic test-secret --from-literal=username='my-app' --from-literal=password='39528$vdg7Jb'
  2. 查看 Secret 相关信息:

    kubectl get secret test-secret

    输出:

    NAME          TYPE      DATA      AGE
    test-secret   Opaque    2         1m
  3. 查看 Secret 相关的更多详细信息:

    kubectl describe secret test-secret

    输出:

    Name:       test-secret
    Namespace:  default
    Labels:     <none>
    Annotations:    <none>
    
    Type:   Opaque
    
    Data
    ====
    password:   13 bytes
    username:   7  bytes

创建可以通过卷访问 secret 数据的 Pod

这里是一个可以用来创建 pod 的配置文件:

pods/inject/secret-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: secret-test-pod
spec:
  containers:
    - name: test-container
      image: nginx
      volumeMounts:
        # name must match the volume name below
        - name: secret-volume
          mountPath: /etc/secret-volume
  # The secret data is exposed to Containers in the Pod through a Volume.
  volumes:
    - name: secret-volume
      secret:
        secretName: test-secret
  1. 创建 Pod:

    kubectl create -f secret-pod.yaml
  2. 确认 Pod 正在运行:

    kubectl get pod secret-test-pod

    输出:

    NAME              READY     STATUS    RESTARTS   AGE
    secret-test-pod   1/1       Running   0          42m
  3. 在 Pod 中运行的容器中获取一个 shell:

    kubectl exec -it secret-test-pod -- /bin/bash
  4. secret 数据通过挂载在 /etc/secret-volume 目录下的卷暴露在容器中。 在 shell 中,进入 secret 数据被暴露的目录:

    root@secret-test-pod:/# cd /etc/secret-volume
  5. 在 shell 中,列出 /etc/secret-volume 目录的文件:

    root@secret-test-pod:/etc/secret-volume# ls

    输出显示了两个文件,每个对应一条 secret 数据:

    password username
  6. 在 shell 中,显示 usernamepassword 文件的内容:

    root@secret-test-pod:/etc/secret-volume# cat username; echo; cat password; echo

    输出为用户名和密码:

    my-app
    39528$vdg7Jb

创建通过环境变量访问 secret 数据的 Pod

这里是一个可以用来创建 pod 的配置文件:

pods/inject/secret-envars-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: secret-envars-test-pod
spec:
  containers:
  - name: envars-test-container
    image: nginx
    env:
    - name: SECRET_USERNAME
      valueFrom:
        secretKeyRef:
          name: test-secret
          key: username
    - name: SECRET_PASSWORD
      valueFrom:
        secretKeyRef:
          name: test-secret
          key: password
  1. 创建 Pod:

    kubectl create -f https://k8s.io/examples/pods/inject/secret-envars-pod.yaml
  2. 确认 Pod 正在运行:

    kubectl get pod secret-envars-test-pod

    输出:

    NAME                     READY     STATUS    RESTARTS   AGE
    secret-envars-test-pod   1/1       Running   0          4m
  3. 在 Pod 中运行的容器中获取一个 shell:

    kubectl exec -it secret-envars-test-pod -- /bin/bash
  4. 在 shell 中,显示环境变量:

    root@secret-envars-test-pod:/# printenv

    输出包括用户名和密码:

    ...
    SECRET_USERNAME=my-app
    ...
    SECRET_PASSWORD=39528$vdg7Jb

接下来

参考

反馈