(转)Kubernetes 配置Pod和容器(十七) 使用Secrets管理安全证书

转:https://www.jianshu.com/p/530b3642c642

本章节展示了如何把密码秘钥等敏感数据安全的注入到Pod里面。

转换安全数据成base-64表示

假设你有两个秘密数据:一个用户名my-app和一个密码39528$vdg7Jb。首先使用Base64-encoding转换用户名和密码用base-64来表示。下面是一个用Linux转换的例子:

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

输出展示了用户名转换以后的字符串是bXktYXBw,密码转换以后是Mzk1MjgkdmRnN0pi。

创建一个Secret

下面是一个配置文件创建一个Secret来保存用户名和密码:

apiVersion: v1
kind: Secret
metadata:
  name: test-secret
data:
  username: bXktYXBwCg==
  password: Mzk1MjgkdmRnN0piCg==

1.创建Secret

kubectl create -f secret.yaml 

注意:如果你想跳过Base64转码的步骤,可以使用kubectl create secret命令创建Secret:

 kubectl create secret generic test-secret --from-literal=username='my-app',password='39528$vdg7Jb'

2.查看Secret的详细信息:

kubectl get secret test-secret

输出:

NAME          TYPE      DATA      AGE
 test-secret   Opaque    2         1m

3.查看更多详细信息:

 kubectl describe secret test-secret

输出:

扫描二维码关注公众号,回复: 3416162 查看本文章
 Name:       test-secret
 Namespace:  default
 Labels:     <none>
 Annotations:    <none>

 Type:   Opaque

 Data
 ====
 password:   13 bytes
 username:   7 bytes

创建一个Pod通过卷访问秘密数据

下面是一个配置文件可以用来创建一个Pod:

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.使用shell进入到pod运行的容器里面:

kubectl exec -it secret-test-pod -- /bin/bash

4.这个秘密数据公开在容器/etc/secret-volume目录里面通过卷挂载的方式。进入这个目录,并查看这个数据:

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

5.在shell里面查看/etc/secret-volume目录下的文件:

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

输出展示了两个文件,每一个都对应相应的秘密数据:

password username

输出是用户名和密码:

 my-app
 39528$vdg7Jb

创建Pod通过环境变量访问秘密数据

下面是一个创建Pod的配置文件:

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 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.用shell进入Pod运行的容器里面:

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


作者:YiQinGuo
链接:https://www.jianshu.com/p/530b3642c642
來源:简书

猜你喜欢

转载自www.cnblogs.com/wangle1001986/p/9726894.html