Kubernetes之Secret

Secret

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

Secret 会以密文的方式存储数据,避免了直接在配置文件中保存敏感信息。Secret 会以 Volume 的形式被 mount 到 Pod,容器可通过文件的方式使用 Secret 中的敏感数据;此外,容器也可以环境变量的方式使用这些数据。
Secret 可通过命令行或 YAML 创建。比如希望 Secret 中包含如下信息:
用户名 admin 密码 123456

创建 Secret
有四种方法创建 Secret:

  1. 通过 --from-literal:
    kubectl create secret generic mysecret --from-literal=username=admin --from-literal=password=123456
    每个 --from-literal 对应一个信息条目。

  2. 通过 --from-file:
    echo -n admin > ./username
    echo -n 123456 > ./password
    kubectl create secret generic mysecret --from-file=./username --from-file=./password
    每个文件内容对应一个信息条目。

  3. 通过 --from-env-file:
    cat << EOF > env.txt
    username=admin
    password=123456
    EOF
    kubectl create secret generic mysecret --from-env-file=env.txt
    文件 env.txt 中每行 Key=Value 对应一个信息条目。

  4. 通过 YAML 配置文件:
    文件中的敏感数据必须是通过 base64 编码后的结果。
    [root@jack ~]# echo -n admin | base64
    YWRtaW4=
    [root@jack ~]# echo -n 123456 | base64
    MTIzNDU2
    执行 kubectl apply 创建 Secret:
    [root@jack ~]# kubectl apply -f mysecret.yml
    secret/mysecret created

查看Secret

kubectl edit secret mysecret
注意里面的密码和用户名是base64 加密过的

echo YWRtaW4= | base64 --decode //123456

在Pod中使用Secret

volume 方式使用 Secret

Pod 可以通过 Volume 或者环境变量的方式使用 Secret

第一步:Pod 的配置文件如下所示:

① 定义 volume foo,来源为 secret mysecret。

② 将 foo mount 到容器路径 /etc/foo,可指定读写权限为 readOnly

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 10; touch /tmp/healthy; sleep 30000
    volumeMounts:
    - name: foo
     mountPath: "/etc/foo"
     readOnly: true
 volumes:
 - name: foo
  secret:
    secretName: mysecret

执行 kubectl apply -f mypod.yml
kubectl exec -it mypod sh
ls /etc/foo 会看到password 和 username 文件里面记录的是明文账密

看起来是不是很酷?但是如果是想改变密码就得进入到pod里面?如果多台机器用一份密码,那岂不是很不方便?
恰好以Volume方式使用Secret 支持动态更新,上述文件稍作修改

 volumes:
 - name: foo
  secret:
    secretName: mysecret
    items:
    - key: username
     path: my-group/my-username
    - key: password
     path: my-group/my-password

这样 当Secret更新之后,容器中的数据也会更新。

环境变量方式

k8s支持通过环境变量使用Secret

spec:
  containers:
  ... 省略若干
    env:
    - name: SECRET_USERNAME
     valueFrom:
       secretKeyRef:
         name: mysecret
         key: username
    - name: SECRET_PASSWORD
     valueFrom:
       secretKeyRef:
         name: mysecret
         key: password

这种无法支撑Secret 动态更新

ConfigMap

ConfigMap 管理配置

Secret 可以为 Pod 提供密码、Token、私钥等敏感数据;对于一些非敏感数据,比如应用的配置信息,则可以用 ConfigMap。

ConfigMap 的创建和使用方式与 Secret 非常类似,主要的不同是数据以明文的形式存放。

与 Secret 一样,ConfigMap 也支持四种创建方式:

  1. 通过 --from-literal:
    kubectl create configmap myconfigmap --from-literal=config1=xxx --from-literal=config2=yyy
    每个 --from-literal 对应一个信息条目。

  2. 通过 --from-file:
    echo -n xxx > ./config1
    echo -n yyy > ./config2
    kubectl create configmap myconfigmap --from-file=./config1 --from-file=./config2
    每个文件内容对应一个信息条目。

  3. 通过 --from-env-file:
    cat << EOF > env.txt
    config1=xxx
    config2=yyy
    EOF
    kubectl create configmap myconfigmap --from-env-file=env.txt
    文件 env.txt 中每行 Key=Value 对应一个信息条目。

  4. 通过 YAML 配置文件:
    文件中的数据直接以明文输入。
    与 Secret 一样,Pod 也可以通过 Volume 或者环境变量的方式使用 Secret。
    Volume 方式:
    大多数情况下,配置信息都以文件形式提供,所以在创建 ConfigMap 时通常采用 --from-file 或 YAML 方式,读取 ConfigMap 时通常采用 Volume 方式。
    与 Secret 一样,Volume 形式的 ConfigMap 也支持动态更新。

apiVersion: v1
kind: ConfigMap
metadata:
  name: myconfigmap
data:
  config1: xxx
  config2: yyy
--- 

使用方法
```yaml
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 10; touch /tmp/healthy; sleep 30000
    volumeMounts:
    - name: foo
     mountPath: "/etc/foo"
     readOnly: true
 volumes:
 - name: foo
  configmap:
    name: myconfigmap

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注