转: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

输出:

 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
來源:简书

最新文章

  1. WPF与WinForm开发有什么区别?
  2. linux命令连接远程服务器
  3. node.js整理 06异步编程
  4. Kafka集群部署
  5. [AaronYang]C#人爱学不学[4]
  6. 用wcf实现带有“秒传”功能的网盘
  7. codeforces 624B Making a String
  8. erlang mnesia 数据库实现SQL查询
  9. 【制作镜像Win*】系统配置
  10. SVN常用命令积累
  11. NOIP2017 列队
  12. 学习笔记7-Android短信发送器
  13. C++ 文件流的详解
  14. 跟我一起学opencv 第五课之调整图像亮度和对比度
  15. 小米 OJ 编程比赛 01 月常规赛_灯_找规律
  16. logback日志配置
  17. CSS中的px与物理像素、逻辑像素、1px边框问题
  18. spring Boot 出现:org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
  19. 17秋 SDN课程 第二次上机作业
  20. Ubuntu16.04怎么将桌面左侧的启动器移动到屏幕底部

热门文章

  1. [CERC2016]Hangar Hurdles
  2. 003--PowerDesigner创建索引与外键
  3. 浅谈WebService开发三(动态调用WebService)转
  4. checkbox radio 多次操作失效
  5. 自己挖的坑自己填--docker创建实例出现Waiting for SSH to be available…
  6. DB2创建库 数据恢复
  7. Linux-SSH免密登陆原理
  8. HDU 6538 Neko and quadrilateral(极角排序+旋转坐标系)
  9. 2019 Multi-University Training Contest 3 - 1006 - Fansblog - 打表 - 暴力
  10. 将图片地址转为blob格式的例子