K8s - Kubernetes使用详解9(使用Secret管理敏感信息)

作者: hangge 发布时间: 2019-09-02 浏览: 2425 次 编辑

    应用启动过程中可能需要一些敏感信息,比如访问数据库的用户名密码或者秘钥。将这些信息如果直接保存在容器镜像中显然不妥,Kubernetes 提供的解决方案是 Secret

九、使用 Secret 管理敏感信息

1,Secret 的功能介绍

  • Secret 会以密文的方式存储数据,避免了直接在配置文件中保存敏感信息。

  • Secret 会以 Volume 的形式被 mount 到 Pod,容器可通过文件的方式使用 Secret 中的敏感数据。

  • 此外,容器也可以环境变量的方式使用 Secret 中的数据。


2,创建 Secret

一共有有四种方法创建 Secret,这里假设我们需要创建一个包含如下信息的 Secret

  • username:hangge

  • password:123456

(1)使用 --from-literal 方式来创建(每个 --from-literal 对应一个信息条目):

1
kubectl create secret generic mysecret --from-literal=username=hangge --from-literal=password=123456


(2)使用 --from-file 方式来创建(每个文件内容对应一个信息条目):

1
2
3
echo -n hangge > ./username
echo -n 123456 > ./password
kubectl create secret generic mysecret --from-file=./username --from-file=./password


(3)使用 --from-env-file 方式来创建(文件 env.txt 中每行 Key=Value 对应一个信息条目)

1
2
3
4
5
6
cat << EOF > env.txt
username=admin
password=123456
EOF
 
kubectl create secret generic mysecret --from-env-file=env.txt


(4)使用 YAML 配置文件方式来创建:

  • 首先创建一个名为 mysecret.yml 的配置文件,内容如下:

1
2
3
4
5
6
7
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
data:
  username: LSBuIGhhbmdnZQo=
  password: LSBuIDEyMzQ1Ngo=


  • 其中文件中的敏感数据必须是通过 base64 编码后的结果,我们可以通过如下命令进行编码:

原文:K8s - Kubernetes使用详解9(使用Secret管理敏感信息)


  • 最后执行 kubectl apply 命令创建 Secret 即可:

1
kubectl apply -f mysecret.yml


3,查看 Secret

(1)执行如下命令可以查看存在的 secret,这里可以看到 mysecret 里有两个数据条目:

1
kubectl get secret mysecret

原文:K8s - Kubernetes使用详解9(使用Secret管理敏感信息)


(2)执行如下命令可以查看条目的 key

1
kubectl describe secret mysecret

原文:K8s - Kubernetes使用详解9(使用Secret管理敏感信息)

(3)如果想连同 Value 一起查看,可以执行如下代码:

1
kubectl edit secret mysecret

原文:K8s - Kubernetes使用详解9(使用Secret管理敏感信息)


(4)然后通过 base64 将 Value 反编码即可:

原文:K8s - Kubernetes使用详解9(使用Secret管理敏感信息)


4,Secret 的使用方式一:通过 Volume 方式

注意:以 Volume 方式使用的 Secret 支持动态更新。也就是说 Secret 更新后,容器中的数据也会更新。

(1)首先我们创建一个 Pod 配置文件 mypod.yml 内容如下:

  • 我们定义了一个名为 foo 的 Volume,来源为 secret mysecret

  • 并且将 foo mount 到容器路径 /etc/foo(指定读写权限为 readOnly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - image: busybox
    name: app
    volumeMounts:
    - mountPath: /etc/foo
      name: foo
      readOnly: true
    args:
    - /bin/sh
    - -c
    - sleep 10; touch /tmp/healthy; sleep 30000
  volumes:
  - name: foo
    secret:
      secretName: mysecret


(2)Pod 创建以后可以看到 Kubernetes 会在指定的路径 /etc/foo 下为每条敏感数据创建一个文件,文件名就是数据条目的 Key(这里是 /etc/foo/username 和 /etc/foo/password)。而 Value 则以明文存放在文件中。

1
2
3
4
5
kubectl apply -f mypod.yml
kubectl exec -it mypod sh
ls /etc/foo
cat /etc/foo/username
cat /etc/foo/password

原文:K8s - Kubernetes使用详解9(使用Secret管理敏感信息)

(3)我们也可以自定义存放数据的文件名,比如将配置文件进行如下修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - image: busybox
    name: app
    volumeMounts:
    - mountPath: /etc/foo
      name: foo
      readOnly: true
    args:
    - /bin/sh
    - -c
    - sleep 10; touch /tmp/healthy; sleep 30000
  volumes:
  - name: foo
    secret:
      secretName: mysecret
      items:
      - key: username
        path: my-group/my-username
      - key: password
        path: my-group/my-password


(4)重新创建 Pod 可以发现这时数据将分别存放在 /etc/foo/my-group/my-username 和 /etc/foo/my-group/my-password 中。

原文:K8s - Kubernetes使用详解9(使用Secret管理敏感信息)


5,Secret 的使用方式二:通过环境变量方式

注意:虽然环境变量读取 Secret 很方便,但无法支撑 Secret 动态更新。

(1)我们对配置文件内容进行修改,将 Secret 读取到环境变量中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - image: busybox
    name: app
    args:
    - /bin/sh
    - -c
    - sleep 10; touch /tmp/healthy; sleep 30000
    env:
      - name: SECRET_USERNAME
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: username
      - name: SECRET_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: password


(2)容器创建后,我们通过环境变量 SECRET_USERNAME  SECRET_PASSWORD 成功读取到 Secret 的数据。

原文:K8s - Kubernetes使用详解9(使用Secret管理敏感信息)


原文链接:https://www.hangge.com/blog/cache/detail_2438.html