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

成都创新互联是专业的图木舒克网站建设公司,图木舒克接单;提供成都网站设计、网站制作、外贸营销网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行图木舒克网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
Secret 会以密文的方式存储数据,避免了直接在配置文件中保存敏感信息。Secret 会以 Volume 的形式被 mount 到 Pod,容器可通过文件的方式使用 Secret 中的敏感数据;此外,容器也可以环境变量的方式使用这些数据。
Secret 可通过命令行或 YAML 创建。比如希望 Secret 中包含如下信息:
- 用户名 admin
- 密码 123456
有四种方法创建 Secret:
- 通过 --from-literal: - kubectl create secret generic mysecret --from-literal=username=admin --from-literal=password=123456- 每个 --from-literal 对应一个信息条目。 
- 通过 --from-file: - echo -n admin > ./username echo -n 123456 > ./password kubectl create secret generic mysecret --from-file=./username --from-file=./password- 每个文件内容对应一个信息条目。 
- 通过 --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 对应一个信息条目。 
- 通过 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:       
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 # 通过 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。
Secret 可以为 Pod 提供密码、Token、私钥等敏感数据;对于一些非敏感数据,比如应用的配置信息,则可以用 ConfigMap。
ConfigMap 的创建和使用方式与 Secret 非常类似,主要的不同是数据以明文的形式存放。
与 Secret 一样,ConfigMap 也支持四种创建方式:
- 通过 --from-literal: - kubectl create configmap myconfigmap --from-literal=config1=xxx --from-literal=config2=yyy- 每个 --from-literal 对应一个信息条目。 
- 通过 --from-file: - echo -n xxx > ./config1 echo -n yyy > ./config2 kubectl create configmap myconfigmap --from-file=./config1 --from-file=./config2- 每个文件内容对应一个信息条目。 
- 通过 --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 对应一个信息条目。 
- 通过 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:       
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:  
[root@k8s-master ~]#   在 Pod 中使用此 ConfigMap,配置文件为:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-configmap
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-configmap
    spec:
      containers:
      - name: nginx-configmap
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:     
        - name: config-volume4
          mountPath: /tmp/config4
      volumes:
      - name: config-volume4
        configMap:
          name: myconfigmap① 在 volume 中指定存放配置信息的文件相对路径为 myapp/logging.conf。
② 将 volume mount 到容器的 /etc 目录。
创建 Pod 并读取配置信息:
配置信息已经保存到 /etc/myapp/logging.conf 文件中。与 Secret 一样,Volume 形式的 ConfigMap 也支持动态更新,留给大家自己实践。
小结
向 Pod 传递配置信息。如果信息需要加密,可使用 Secret;如果是一般的配置信息,则可使用 ConfigMap。
Secret 和 ConfigMap 支持四种定义方法。Pod 在使用它们时,可以选择 Volume 方式或环境变量方式,不过只有 Volume 方式支持动态更新。
当前标题:k8s之安全信息(secret)及配置信息(configmap)管理
链接分享:http://www.scyingshan.cn/article/gcpooh.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 