k8s之安全信息(secret)及配置信息(configmap)管理

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

Secret 会以密文的方式存储数据,避免了直接在配置文件中保存敏感信息。Secret 会以 Volume 的形式被 mount 到 Pod,容器可通过文件的方式使用 Secret 中的敏感数据;此外,容器也可以环境变量的方式使用这些数据。

Secret 可通过命令行或 YAML 创建。比如希望 Secret 中包含如下信息:

  1. 用户名 admin
  2. 密码 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 配置文件:
    apiVersion: v1
    kind: Secret
    metadata:
    name: mysecret
    data:
    username: YWRtaW4=
    password: MTIzNDU2

文件中的敏感数据必须是通过 base64 编码后的结果。

[root@k8s-master ~]# echo -n admin |base64
YWRtaW4=
[root@k8s-master ~]# echo -n 123456 | base64
MTIzNDU2

执行 kubectl apply 创建 Secret:

# kubectl apply -f mysecrete.yml
secret/mysecret created
使用这些创建好的 Secret。

查看 Secre
可以通过 kubectl get secret 查看存在的 secret。

[root@k8s-master ~]# kubectl get secrets 
NAME                  TYPE                                  DATA   AGE
default-token-5l66h   kubernetes.io/service-account-token   3      14d
mysecret              Opaque                                2      20s

显示有两个数据条目,kubectl describe secret 查看条目的 Key:

[root@k8s-master ~]# kubectl describe secrets mysecret 
Name:         mysecret
Namespace:    default
Labels:       <none>
Annotations:  
Type:         Opaque

Data
====
password:  6 bytes
username:  5 bytes
[root@k8s-master ~]# 

如果还想查看 Value,可以用 kubectl edit secret mysecret:

apiVersion: v1
data:
  password: MTIzNDU2
  username: YWRtaW4=
kind: Secret
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"password":"MTIzNDU2","username":"YWRtaW4="},"kind":"Secret","metadata":{"annotations":{},"name":"mysecret","namespace":"default"}}
  creationTimestamp: "2019-10-14T08:26:43Z"
  name: mysecret
  namespace: default
  resourceVersion: "13845"
  selfLink: /api/v1/namespaces/default/secrets/mysecret
  uid: a713292c-6fea-4065-b5ae-239f8fe9a76f
type: Opaque
~              

然后通过 base64 将 Value 反编码:

[root@k8s-master ~]# echo -n MTIzNDU2 |base64 --decode 
123456

# echo -n YWRtaW4=  |base64 --decode                  
admin[root@k8s-master ~]# 

如何在 Pod 中使用 Secret。

volume 方式使用 Secret

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

Pod 的配置文件如下所示:

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

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

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

创建 Pod 并在容器中读取 Secret:

[root@k8s-master ~]# kubectl apply -f mypod.yml
pod/mypod created
[root@k8s-master ~]# kubectl exec -it mypod sh 
/ # ls /etc/foo/
password  username
/ # cat /etc/foo/username 
admin/ # 
/ # cat /etc/foo/password 
123456/ # 
/ # 
/ # exit

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

我们也可以自定义存放数据的文件名,比如将配置文件改为:


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
      items:
      - key: username
        path: my-group/my-username
      - key: password
        path: my-group/my-password

这时数据将分别存放在 /etc/foo/my-group/my-username 和 /etc/foo/my-group/my-password 中。

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

将 password 更新为 abcdef,base64 编码为 YWJjZGVm

[root@k8s-master ~]# cat mysecrete.yml 
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
data:
  username: YWRtaW4=
  password: YWJjZGVm

更新 Secret。

[root@k8s-master ~]# kubectl apply -f mysecrete.yml
secret/mysecret configured

等待,新的 password 会同步到容器。

/etc/foo/..2019_10_14_09_42_09.863448745/my-group # cat my-password 
abcdef/etc/foo/..2019_10_14_09_42_09.863448745/my-group # 
环境变量方式使用 Secret

通过 Volume 使用 Secret,容器必须从文件读取数据,会稍显麻烦,Kubernetes 还支持通过环境变量使用 Secret。

Pod 配置文件示例如下:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: busybox
    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

创建 Pod 并读取 Secret。

[root@k8s-master ~]# kubectl apply -f mysql-env.yml
pod/mypod created
[root@k8s-master ~]# kubectl exec -it mypod sh
/ # echo $SECRET_USERNAME
admin
/ # echo $SECRET_PASSWORD
123456
/ # 

通过环境变量 SECRET_USERNAME 和 SECRET_PASSWORD 成功读取到 Secret 的数据。

需要注意的是,环境变量读取 Secret 很方便,但无法支撑 Secret 动态更新。

Secret 可以为 Pod 提供密码、Token、私钥等敏感数据;对于一些非敏感数据,比如应用的配置信息,则可以用 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 配置文件:
    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: myconfigmap1
    data:
    config1: xxx
    config2: yyy

    文件中的数据直接以明文输入。

与 Secret 一样,Pod 也可以通过 Volume 或者环境变量的方式使用 Secret。

Volume 方式:

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

环境变量方式:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: busybox
    args:
      - /bin/sh
      - -c
      - sleep 10; touch /tmp/healthy; sleep 30000
    env:
      - name: CONFIG_1
        valueFrom:
          configMapKeyRef:
            name: myconfigmap
            key: config1
      - name: CONFIG_2
        valueFrom:
          configMapKeyRef:
            name: myconfigmap
            key: config2

大多数情况下,配置信息都以文件形式提供,所以在创建 ConfigMap 时通常采用 --from-file 或 YAML 方式,读取 ConfigMap 时通常采用 Volume 方式。

比如给 Pod 传递如何记录日志的配置信息:

class: logging.handlers.RotatingFileHandler
formatter: precise
level: INFO
filename: %hostname-%timestamp.log

可以采用 --from-file 形式,则将其保存在文件 logging.conf 中,然后执行命令:

# kubectl create configmap myconfigmap2 --from-file=./logging.conf 

kubectl create configmap myconfigmap --from-file=./logging.conf
如果采用 YAML 配置文件,其内容则为:

apiVersion: v1
kind: ConfigMap
metadata:
  name: myconfigmap3
data:
  logging.conf: |
    class: logging.handlers.RotatingFileHandler
    formatter: precise
    level: INFO
    filename: %hostname-%timestamp.log

注意别漏写了 Key logging.conf 后面的 | 符号。

创建并查看 ConfigMap:

[root@k8s-master ~]# kubectl apply -f myconfigmap2.yml
configmap/myconfigmap3 created
[root@k8s-master ~]# kubectl get configmaps myconfigmap3 
NAME           DATA   AGE
myconfigmap3   1      2m39s
[root@k8s-master ~]# kubectl describe configmaps myconfigmap3
Name:         myconfigmap3
Namespace:    default
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"logging.conf":"class: logging.handlers.RotatingFileHandler\nformatter: precise\nlevel: INFO\nfilename: %hostna...

Data
====
logging.conf:
----
class: logging.handlers.RotatingFileHandler
formatter: precise
level: INFO
filename: %hostname-%timestamp.log

Events:  <none>
[root@k8s-master ~]# 

在 Pod 中使用此 ConfigMap,配置文件为:

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/
  volumes:
  - name: foo
    configMap:
      name: myconfigmap3
      items:
        - key: logging.conf
          path: myapp/logging.conf

① 在 volume 中指定存放配置信息的文件相对路径为 myapp/logging.conf。

② 将 volume mount 到容器的 /etc 目录。

创建 Pod 并读取配置信息:

配置信息已经保存到 /etc/myapp/logging.conf 文件中。与 Secret 一样,Volume 形式的 ConfigMap 也支持动态更新,留给大家自己实践。

小结
向 Pod 传递配置信息。如果信息需要加密,可使用 Secret;如果是一般的配置信息,则可使用 ConfigMap。

Secret 和 ConfigMap 支持四种定义方法。Pod 在使用它们时,可以选择 Volume 方式或环境变量方式,不过只有 Volume 方式支持动态更新。

猜你喜欢

转载自blog.51cto.com/5157495/2442447