千家信息网

Kubernetes的Secret与ConfigM是什么

发表于:2024-10-22 作者:千家信息网编辑
千家信息网最后更新 2024年10月22日,ConfigMap和Secret是Kubernetes中两种特殊类型的存储卷,ConfigMap这种资源对象主要用于提供配置数据以定制程序行为,不过一些敏感的配置信息,比如像用户名、密码、密钥等通常都
千家信息网最后更新 2024年10月22日Kubernetes的Secret与ConfigM是什么

ConfigMap和Secret是Kubernetes中两种特殊类型的存储卷,ConfigMap这种资源对象主要用于提供配置数据以定制程序行为,不过一些敏感的配置信息,比如像用户名、密码、密钥等通常都是由Secret这种资源对象来进行配置的,他们将相应的配置信息保存于对象中,而后在Pod资源上以存储卷的形式将其挂载并获取相应配置,以实现配置与镜像文件的解耦。

一、Secret资源对象

1) Secret概述

Secret资源对象存储数据的方式是以键值对的方式进行存储的,在Pod资源进行Secret的方式是通过环境变量或存储卷的方式进行访问数据,解决了密码、token、密钥等敏感数据的配置问题,而不需要将这些敏感数据暴露到镜像或者Pod的spec字段中。另外,Secret对象的数据存储和打印格式为Base64编码的字符串,因此用户在创建Secret对象时,也需要提供该类型的编码格式的数据。在容器中以环境变量或存储卷的方式访问时,会自动解码为明文格式。需要注意的是,如果是在Master节点上,Secret对象以非加密的格式存储在etcd中,所以需要对etcd的管理和权限进行严格控制。

2)Secret资源的类型

Secret有四种类型:
1)Service Account :用来访问Kubernetes API,由Kubernetes自动创建,并且会自动挂载到Pod的/run/secrets/kubernetes.io/serviceaccount目录中;
2)Opaque :base64编码格式的Secret,用来存储密码、密钥、信息、证书等,类型标识符为generic;
3)kubernetes.io/dockerconfigjson :用来存储私有docker registry的认证信息,类型标识为docker-registry;
4)kubernetes.io/tls:用于为SSL通信模式存储证书和私钥文件,命令式创建类型标识为tls;

3)创建Secret的方式

假设存储的数据是:
username:root
password:123.com
以下的存储方式都是存储该信息!

1)使用--from-literal(文字)的方式
[root@master ~]# kubectl create secret generic mysecret01 --from-literal=username=root --from-literal=password=123.com#创建一个secret资源对象,名称为mysecret01,采用的加密方式是generic(通用的、一般的加密方式)#注意:这种方式每一条只能保存一条信息[root@master ~]# kubectl get secrets mysecret01 NAME         TYPE     DATA   AGEmysecret01   Opaque   2      25s[root@master ~]# kubectl describe secrets mysecret01     #查看该资源的详细信息Name:         mysecret01Namespace:    defaultLabels:       Annotations:  Type:  Opaque                         #不透明的,看不到的Data====password:  7 bytes                     #只能查看键的名称,无法查看到键对应的值username:  4 bytes[root@master ~]# kubectl get secrets mysecret01 -o yaml            #将该资源以yaml文件的方式进行显示apiVersion: v1data:  password: MTIzLmNvbQ==                       #键对应的值都是乱码,加密使用的是base64编码格式  username: cm9vdA==kind: Secretmetadata:  creationTimestamp: "2020-02-14T10:08:21Z"  name: mysecret01  namespace: default  resourceVersion: "2474"  selfLink: /api/v1/namespaces/default/secrets/mysecret01  uid: 1aee0635-7bfb-4e8a-a21e-be993e534156type: Opaque[root@master ~]# echo -n cm9vdAo= | base64 --d              #将乱码解码后的结果root[root@master ~]# echo -n MTIzLmNvbQ== | base64 --d123.com
2)使用--from-file(文件)的方式

这种方式更第一种方式差不多,可能稍微显得麻烦一些!

[root@master ~]# echo root > username[root@master ~]# echo 123.com > password#需要先将要存储的键值对写入到文件中,并且每个文件只能写入一个值[root@master ~]# kubectl create secret generic mysecret02 --from-file=username --from-file=password[root@master ~]# rm -rf username password              #即使文件删除之后,该资源键对应的值依然也是存在的[root@master ~]# kubectl get secrets mysecret02NAME         TYPE     DATA   AGEmysecret02   Opaque   2      58s[root@master ~]# kubectl describe secrets mysecret02Name:         mysecret02Namespace:    defaultLabels:       Annotations:  Type:  OpaqueData====password:  8 bytesusername:  5 bytes
3)通过 --from-env-file(环境变量)的方式

这种方式可以在同一个文件中写入多个键值对,推荐使用!

[root@master ~]# tee  env.txt <Annotations:  Type:  OpaqueData====username:  4 bytespassword:  7 bytes
4)通过yaml文件的方式
 [root@master ~]# echo root | base64                  #需要将键对应的值进行加密cm9vdAo=[root@master ~]# echo 123.com | base64MTIzLmNvbQo=[root@master ~]# vim secret.yaml  apiVersion: v1kind: Secretmetadata:  name: mysecret04data:  username: cm9vdAo=                            #将加密后的值写到配置文件中  password: MTIzLmNvbQo=[root@master ~]# kubectl apply -f secret.yaml[root@master ~]# kubectl get secrets mysecret04NAME         TYPE     DATA   AGEmysecret04   Opaque   2      118s[root@master ~]# kubectl describe secrets mysecret04Name:         mysecret04Namespace:    defaultLabels:       Annotations:  Type:         OpaqueData====password:  8 bytesusername:  5 bytes

4)Secret资源的使用方式

1)以volume挂载的方式使用
[root@master ~]# vim secret-pod01.yamlapiVersion: v1kind: Podmetadata:  name: mypodspec:  containers:  - name: mypod    image: busybox    args:      - /bin/sh      - -c      - sleep 3000000                  #以上字段仅仅是创建一个容器    volumeMounts:    - name: secret-test      mountPath: "/etc/secret-test"                  #指定容器中的目录      readOnly: true                                         #以只读的方式挂载  volumes:  - name: secret-test    secret:      secretName: mysecret04          #指定的是已有的secret资源的名称[root@master ~]# kubectl apply -f secret-pod01.yaml [root@master ~]# kubectl exec -it mypod /bin/sh        #进入容器/ # cat -n /etc/secret-test/username /etc/secret-test/password     #查看对应的目录是否存在数据     1  root     2  123.com#而且是已经解密后的数据         / # echo 12324235532 > /etc/secret-test/username /bin/sh: can't create /etc/secret-test/username: Read-only file system

现在,我们可以验证一下,如果此时更改secret04的内容,那么容器内对应的挂载目录下的内容是否更改?

[root@master ~]# echo zhangsan | base64emhhbmdzYW4K[root@master ~]# echo 123456 | base64MTIzNDU2Cg==[root@master ~]# vim secret.yaml apiVersion: v1kind: Secretmetadata:  name: mysecret04data:  username: emhhbmdzYW4K  password: MTIzNDU2Cg==[root@master ~]# kubectl apply -f secret.yaml [root@master ~]# kubectl exec -it mypod /bin/sh/ # cat -n /etc/secret-test/username /etc/secret-test/password      1  zhangsan     2  123456#再次查看容器中数据,发现已经发生了变化!    

注意:如果采用volume挂载的方式调用secert存储的值,容器内的值会随着secert存储的值发生改变而变化!

2)用环境变量的方式运行
[root@master ~]# vim secret-pod02.yaml apiVersion: v1kind: Podmetadata:  name: mypod2spec:  containers:  - name: mypod    image: busybox    args:      - /bin/sh      - -c      - sleep 3000000    env:                                   #设置环境变量      - name: SECRET_USERNAME               #指容器中的变量名称        valueFrom:          secretKeyRef:            name: mysecret02                 #调用的是mysecret02             key: username                       #对应的是mysecret02中username对应的值      - name: SECRET_PASSWORD     #同上        valueFrom:          secretKeyRef:            name: mysecret02            key: password[root@master ~]# kubectl apply -f secret-pod02.yaml[root@master ~]# kubectl exec -it mypod2 /bin/sh/ # echo ${SECRET_USERNAME}root/ # echo ${SECRET_PASSWORD}123.com#进入容器之后,查看变量对应的值

注意:如果采用变量的方式调用secert存储的值,容器内的变量值并不会随着secert存储的值发生改变,除非重新生成pod。

二、ConfigMap资源对象

1)ConfigMap概述

我们知道,在几乎所有的应用开发中,都会涉及到配置文件的变更,比如说在web的程序中,需要连接数据库,缓存甚至是队列等等。而我们的一个应用程序从写第一行代码开始,要经历开发环境、测试环境、预发布环境只到最终的线上环境。而每一个环境都要定义其独立的各种配置。如果我们不能很好的管理这些配置文件,你的运维工作将顿时变的无比的繁琐。为此业内的一些大公司专门开发了自己的一套配置管理中心,如360的Qcon,百度的disconf等。kubernetes也提供了自己的一套方案,即ConfigMap。kubernetes通过ConfigMap来实现对容器中应用的配置管理。

2)ConfigMap的创建方式

其实configMap和secert资源对象的创建方式完全一样!

1)通过--from-literal的方式
[root@master ~]# kubectl create configmap configmap01 --from-literal=username=root --from-literal=password=123.com#创建一个configmap资源,名称为configmap01[root@master ~]# kubectl describe configmaps configmap01 Name:         configmap01Namespace:    defaultLabels:       Annotations:  Data      #可以很明确的看出存储的键对应的值,所以一般用于存储配置文件信息====password:----123.comusername:----rootEvents:  
2)通过--from-file的方式
[root@master ~]# echo root > username[root@master ~]# echo 123.com > password[root@master ~]# kubectl create configmap configmap02 --from-file=username --from-file=passwordconfigmap/configmap02 created[root@master ~]# kubectl describe configmaps configmap02Name:         configmap02Namespace:    defaultLabels:       Annotations:  Data====password:----123.comusername:----rootEvents:  
3)通过--from-env-file的方式
[root@master ~]# tee 123.txt < username=root> password=123.com> EOF[root@master ~]# kubectl create configmap configmap03 --from-env-file=123.txtconfigmap/configmap03 created[root@master ~]# kubectl describe configmaps configmap03Name:         configmap03Namespace:    defaultLabels:       Annotations:  Data====username:----rootpassword:----123.comEvents:  
4)通过yaml文件的方式
[root@master ~]# vim configmap.yamlapiVersion: v1kind: ConfigMapmetadata:  name: configmap04data:   username: root                       #configmap使用yaml文件进行创建时,键对应的值无需事先加密  password: 123.com                     #对应的值如果是数字,则需要单引号引起[root@master ~]# kubectl apply -f configmap.yaml[root@master ~]# kubectl describe configmaps configmap04Name:         configmap04Namespace:    defaultLabels:       Annotations:  kubectl.kubernetes.io/last-applied-configuration:                {"apiVersion":"v1","data":{"password":"123.com","username":"root"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"configmap04","n...Data====password:----123.comusername:----rootEvents:  

3)ConfigMap的使用方式

1)以volume挂载的方式使用
[root@master ~]# vim configmap-pod01.yamlapiVersion: v1kind: Podmetadata:  name: configmap-pod01spec:  containers:  - name: configmap-pod01    image: busybox    args:      - /bin/sh      - -c      - sleep 3000000    volumeMounts:    - name: configmap-test      mountPath: "/etc/configmap-test"      readOnly: true  volumes:  - name: configmap-test    configMap:      name: configmap01[root@master ~]# kubectl apply -f configmap-pod01.yaml[root@master ~]# kubectl exec -it configmap-pod01 /bin/sh/ # cat -n /etc/configmap-test/username /etc/configmap-test/password     1  root     2  123.com

ConfigMap资源采用volume挂载的方式,与Secret资源采用volume挂载的方式几乎是一样的,会随着源数据的变化而变化!

2)用环境变量的方式运行
[root@master ~]# vim configmap-pod02.yamlapiVersion: v1kind: Podmetadata:  name: configmap-pod02spec:  containers:  - name: configmap-pod02    image: busybox    args:      - /bin/sh      - -c      - sleep 3000000    env:      - name: CONFIGMAP_USERNAME        valueFrom:          configMapKeyRef:            name: configmap04            key: username                   #调用的是configmap04中username的值      - name: CONFIGMAP_PASSWORD        valueFrom:          configMapKeyRef:            name: configmap04            key: password[root@master ~]# kubectl apply -f configmap-pod02.yaml                      [root@master ~]# kubectl exec -it configmap-pod02 /bin/sh/ # echo ${CONFIGMAP_USERNAME}root/ # echo ${CONFIGMAP_PASSWORD}123.com

ConfigMap资源采用环境变量的方式,与Secret资源采用变量变量的方式几乎是一样的,不会随着源数据的变化!

三、Secret与ConfigMap的异同点

1)相同点

都是用来保存轻量级信息的,可以供其他资源对象(Deployment、RC、RS和POd)进行挂载使用。
这两种资源对象的创建方法(4种)及引用方法(2种)都是一样的,都是以键值对的方式进行存储的。

2)不同点

Secret是用来保存敏感信息的,而configMap是用来保存一些不太重要的数据的,具体表现在当我们执行"kubectl describe ...."命令时,Secret这种类型的资源对象时查看不到其具体的信息的,而configMap是可以查看到其保存的具体内容的。

3)注意事项

1)Secret、ConfigMap必须在Pod之前创建;
2)只有与当前Secret、ConfigMap在同一个namespace内的pod才能使用这个Secret、ConfigMap,换句话说,Secret、ConfigMap不能跨命名空间调用。

0