screct
作者:互联网
一 对于一些保密度比较高的文件,k8s又是如何存储的呢?
针对那些保密度比较高的配置文件,例如证书以及一些认证配置不能直接存储在configmap中,而是需要存储在另外一种资源中,需要对存储在里面的数据进行加密,并且只会调度到需要的pod上面,在pod上面所在的节点上,secerts卷并不会存储在硬盘等设备上,而是需要存储在node上面的内存中,这样就不需要擦拭来提高保密性,同样的secrets和configmap类似,支持两种数据渲染到pod中的容器
- 通过将配置渲染到容器的环境变量中
- 通过将secerts暴露为卷中的文件
另外一点需要注意的是,对于主节点本身,secerts是按照明文的形式进行存储,这就需要保障主节点安全从而确保存储在secerts的数据安全性,同样需要保障的一点是禁止未被授权的用户访问主节点服务器,因为在没有确实授权之前,任何用户都可以通过pod拉去secerts数据并可能造成数据暴露因此是将数据存储到configmap或者secert是需要认真考虑的一件事情
- 采用configmap存储非敏感的数据文件
- 采用secerts存储敏感的数据文件
- 当存储的数据里面同时包含敏感数据或者非敏感数据,也得将数据存储在secerts中
二 secrets的相关应用和创建
2.1 来看一个pod中默认的令牌
[root@node01 ~]# k get secrets NAME TYPE DATA AGE default-token-tzwwt kubernetes.io/service-account-token 3 15d [root@node01 ~]# k describe secrets Name: default-token-tzwwt Namespace: default Labels: <none> Annotations: kubernetes.io/service-account.name: default kubernetes.io/service-account.uid: 61b7be2c-433e-11eb-90ae-5254002a5691 Type: kubernetes.io/service-account-token Data ==== namespace: 7 bytes token: eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6ImRlZmF1bHQtdG9rZW4tdHp3d3QiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGVmYXVsdCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjYxYjdiZTJjLTQzM2UtMTFlYi05MGFlLTUyNTQwMDJhNTY5MSIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDpkZWZhdWx0OmRlZmF1bHQifQ.zwEvc13whMJW7MaCcDpXkpLmQsi2qfih-OUiNAVt8gwqZ9KWmFOj7Qye_vtzeYhc1m3YyM6YkCTjCtKqTUX28P0Y50-_Pl_0fQKrm2BTb7fGIskRwB4egFiwMZmcgzqUr4CHuvINdb339mYp8eYqpf1Io51fFKmLRwE3U1zWLRFod_QP-eBxlSCq5O76BtPjkH0egWvEgScao5tYJLw-tFMdkhyeFpJCoVdjf4plxi5yFYdxP86kc0jjtW9x9Z6LBKVhTC18_cQawG24UgOAjwZW4TF4v5y25Me4G3itTTVx_IKx8M6ndK5MrerFGCZoppFeSJdFr_TFyuWgcznuTw ca.crt: 1025 bytes
- 系统中每个pod都会挂载一个secrets卷,存储的内容是可以直接于kubernets的api服务器进行通信的相关认证,这里也可以通过设置pod中的automountServiceAccountToken字段为false来关闭这种默认行为
- 通过describe命令可以看到更过的关于这个secrets的信息,可以发现这个secrets数据包含了ca.crt,token以及namespace等访问kubernets的api服务器的全部信息
- 再者通过describe pod可以看到该secrets挂载在pod上面的相关信息地址为/var/run/secrets/kubernetes.io/serviceaccount
2.2 查看该默认的secrets挂载pod中的位置
[root@node01 ~]# k exec test-for-all-env-from-configmap -- ls -Alrt /var/run/secrets/kubernetes.io/serviceaccount total 0 lrwxrwxrwx. 1 root root 12 Jan 4 13:51 token -> ..data/token lrwxrwxrwx. 1 root root 16 Jan 4 13:51 namespace -> ..data/namespace lrwxrwxrwx. 1 root root 13 Jan 4 13:51 ca.crt -> ..data/ca.crt lrwxrwxrwx. 1 root root 31 Jan 4 13:51 ..data -> ..2021_01_04_13_51_29.048076121 drwxr-xr-x. 2 root root 100 Jan 4 13:51 ..2021_01_04_13_51_29.048076121
- 可以看出secrets也是和configmap一样,通过创建data的软连接到指定的目录,然后data也是其中一个挂载的目录,通过对整个文件进行替换使得文件来更新
- 该文件夹下面包含了访问kubernetsapi服务器的所有的必要文件
2.3 创建一个secrets
[root@node01 Chapter07]# openssl genrsa -out https.key 2048 Generating RSA private key, 2048 bit long modulus [root@node01 Chapter07]# openssl req -new -x509 -key https.key -out https.cert -days 3650 -subj /CN=www.kubia-example.com [root@node01 Chapter07]# echo wxm-test>wxm-test [root@node01 Chapter07]# k create secret generic fortune-https --from-file=https.cert --from-file=https.key --from-file=wxm-test secret/fortune-https created [root@node01 Chapter07]# k get secret NAME TYPE DATA AGE default-token-tzwwt kubernetes.io/service-account-token 3 15d fortune-https Opaque 3 24s
- 我们首先使用openssl等相关命令创建了证书以及认证以及一个附加的文件
- 之后用这个三个文件创建了一个secret
- 至此secret创建完成,但是有点奇怪的是,type类型内容为opaque不知道什么意思?
2.4 对比configmap以及secret
2.4.1 通过yaml的形式查看刚才创建的secret
[root@node01 Chapter07]# k get secret fortune-https -o yaml apiVersion: v1 data: https.cert: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURFekNDQWZ1Z0F3SUJBZ0lKQU5jQk83ZW0wYk5pTUEwR0NTcUdTSWIzRFFFQkN3VUFNQ0F4SGpBY0JnTlYKQkFNTUZYZDNkeTVyZFdKcFlTMWxlR0Z0Y0d4bExtTnZiVEFlRncweU1UQXhNRFV3TkRBMk1EUmFGdzB6TVRBeApNRE13TkRBMk1EUmFNQ0F4SGpBY0JnTlZCQU1NRlhkM2R5NXJkV0pwWVMxbGVHRnRjR3hsTG1OdmJUQ0NBU0l3CkRRWUpLb1pJaHZjTkFRRUJCUUFEZ2dFUEFEQ0NBUW9DZ2dFQkFLdkNkUFFHRWlJaElwZ0ZDRkU2U21oZFJ5ODMKaldoRDZPZUtLcndNcms1cWMweWUrSmg5N3lqRHY4Q3NtbGFpZWJPZjZXTUN0L1Z2ajhPW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5L1ZyckpvMi9QalJ0RGMyaXdMdzFlaU5sQmZ1dDdXOHZWeHhrY21EaApNb21GRjdBUklqQ1VHc1ZnVmJWRElNZkE5VmpMSTNhTitGWGhsdVI1U1dtUWZydzloQ2Q4dWc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo= wxm-test: d3htLXRlc3QK kind: Secret metadata: creationTimestamp: "2021-01-05T04:08:43Z" name: fortune-https namespace: default resourceVersion: "2145910" selfLink: /api/v1/namespaces/default/secrets/fortune-https uid: b3435209-4f0b-11eb-ae9a-5254002a5691 type: Opaque
- 对比之前创建的configmap可以发现secret里面的数据都是通过采用base64格式编码
- 这么做的好处很明显,可以对数据起到很好的保护
- 但是对于开发者而言,就会多了一个编解码的步骤,但是不需要应用自己去编解码
标签:screct,secrets,存储,token,https,pod,root 来源: https://www.cnblogs.com/wxm-pythoncoder/p/14235048.html