Docker学习第十二天——ConfigureMap(明文)与Secret(密文)

目录

一、ConfigureMap

1、ConfigureMap介绍

2、ConfigureMap的创建

3、ConfigureMap的使用方式

4、ConfigureMap使用实例

5、注意

二、Secret

1、Secret介绍

2、Secret类型

3、Secret与ConfigMap对比

4、Secret的创建

5、Secret的使用


一、ConfigureMap

1、ConfigureMap介绍

• configureMap是一种API对象,用于将非加密数据保存到键值对中,可以用作环境变量、命令行参数或者存储卷中的配置文件。

• ConfigMap可以将环境变量配置信息和容器镜像解耦,便于应用配置的修改。如果需要存储加密信息时可以使用Secret对象。

2、ConfigureMap的创建

1、命令行创建

使用 kubectl create configmap 从文件、目录或者 key-value 字符串创建等创建 ConfigMap

1)文件创建

[root@hd1 ~]# echo hello > test1.txt
[root@hd1 ~]# echo hello > test2.txt
[root@hd1 ~]# echo world > test3.txt
[root@hd1 ~]# echo goodbye > test4.txt
[root@hd1 ~]# kubectl create configmap my-config --from-file=key1=test1.txt --from-file=key2=test2.txt --from-file=key3=test3.txt --from-file=key4=test4.txt
configmap/my-config created
[root@hd1 ~]# kubectl describe configmap my-config
Name:         my-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
key3:
----
world

key4:
----
goodbye

key1:
----
hello

key2:
----
hello

Events:  <none>

2)文件夹创建

[root@hd1 ~]# mkdir config
[root@hd1 ~]# echo hello >config/test1
[root@hd1 ~]# echo segai >config/test2
[root@hd1 ~]# kubectl create configmap dir-config --from-file=config/
configmap/dir-config created
[root@hd1 ~]# kubectl describe configmap dir-config
Name:         dir-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
test1:
----
hello

test2:
----
segai

Events:  <none>

3)键值对创建

[root@hd1 ~]# kubectl create configmap literal-config --from-literal=key1=hello --from-literal=key2=world
configmap/literal-config created
[root@hd1 ~]# kubectl describe configmap literal-config
Name:         literal-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
key1:
----
hello
key2:
----
world
Events:  <none>

2、yaml文件创建

[root@hd1 ~]# vim config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: new-config
data:
  key1: nihao
  key2: shijie
  fruit: lemon
  like: pool
  
[root@hd1 ~]# kubectl apply -f config.yaml
[root@hd1 ~]# kubectl describe configmap new-config
Name:         new-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
fruit:
----
lemon
key1:
----
nihao
key2:
----
shijie
like:
----
pool
Events:  <none>

3、ConfigureMap的使用方式

1. 将ConfigMap中的数据设置为容器的环境变量

2. 将ConfigMap中的数据设置为命令行参数

3. 使用Volume将ConfigMap作为文件或目录挂载

4. 编写代码在 Pod 中运行,使用 Kubernetes API 来读取 ConfigMap

4、ConfigureMap使用实例

1.配置到容器的环境变量

[root@hd1 ~]# vim test-pod-configmap.yaml
apiVersion: v1
kind: Pod
metadata:
 name: test-pod-configmap
spec:
 containers:
  - name: test-busybox
    image: busybox:1.28
    imagePullPolicy: IfNotPresent
    args:
    - sleep
    - "86400"
    env:
    - name: KEY1
      valueFrom:
       configMapKeyRef:        #指定调用configMap
        name: my-config        #对应configMap名称
        key: key1              #对应其中键值对
    - name: KEY2
      valueFrom:
       configMapKeyRef:
        name: my-config
        key: key2

2.设置为命令行参数

# test-pod-configmap-cmd
apiVersion: v1
kind: Pod
metadata:
 name: test-pod-configmap-cmd
spec:
 containers:
  - name: test-busybox
    image: busybox
    imagePullPolicy: IfNotPresent
    command: [ "/bin/sh","-c","echo $(KEY1) echo $(KEY2)"]
    env:
    - name: KEY1
      valueFrom:
       configMapKeyRef:
        name: my-config
        key: key1
    - name: KEY2
      valueFrom:
       configMapKeyRef:
        name: my-config
        key: key2
 restartPolicy: Never
 
# 创建pod,该pod成功启动后会输出环境变量KEY1和KEY2的值
[root@hd1 ~]# kubectl create -f test-pod-configmap-cmd.yaml 

#查看pod的日志
[root@hd1 ~]# kubectl logs test-pod-configmap-cmd
hello
world

#进入到容器中查看环境变量
[root@hd1 ~]# kubectl exec -it test-pod-configmap -- /bin/sh
/ # env |grep KEY
KEY1=hello
KEY2=hello

3.将configMap挂载到容器中

# test-pod-projected-configmap-volume.yaml
apiVersion: v1
kind: Pod
metadata:
 name: test-pod-projected-configmap-volume
spec:
 containers:
 - name: test-pod-busybox
   image: busybox
   imagePullPolicy: IfNotPresent
   args:
   - sleep
   - "86400"
   volumeMounts:
   - name: config-volume
     mountPath: "/projected-volume"
     readOnly: true
 volumes:
 - name: config-volume
   projected:
    sources:
    - configMap:

#创建pod
[root@hd1 ~]# kubectl create -f test-pod-projected-configmap-volume.yaml
#进入容器
[root@hd1 ~]# kubectl exec -it test-pod-projected-configmap-volume -- /bin/sh

• 通过volume挂载和环境变量的区别:通过Volume挂载到容器内部时,当该configmap的值发生变化时,容器内部具备自动更新的能力,但是通过环境变量设置到容器内部该值不具备自动更新的能力。

5、注意

• ConfigMap必须在Pod使用它之前创建

• 使用envFrom时,将会自动忽略无效的键

• Pod只能使用同一个命名空间的ConfigMap

二、Secret

1、Secret介绍

        k8s secrets用于存储和管理一些敏感数据,比如密码,token,密钥等敏感信息。它把 Pod 想要访问的加密数据存放到 Etcd 中。然后用户就可以通过在 Pod 的容器里挂载 Volume 的方式或者环境变量的方式访问到这些 Secret 里保存的信息了。

2、Secret类型

1.Opaque

• base64 编码格式的 Secret,用来存储密码、密钥等;但数据也可以通过base64 –decode解码得到原始数据,所有加密性很弱。

• Opaque 类型的数据是一个 map 类型,要求value是base64编码。

• 手动创建base64加密&解密:

[root@hd1 ~]# echo -n 'admin' | base64
YWRtaW4=
[root@hd1 ~]# echo -n '1f2d1e2e67df' | base64
MWYyZDFlMmU2N2Rm

#解码
[root@hd1 ~]# echo 'MWYyZDFlMmU2N2Rm' | base64 --decode
1f2d1e2e67df

2.Service Account(SA)

• 用来访问Kubernetes API,由Kubernetes自动创建,并且会自动挂载到Pod的 /run/secrets/kubernetes.io/serviceaccount 目录中。

• Service Account 对象的作用,就是 Kubernetes 系统内置的一种“服务账户”,它是 Kubernetes 进行权限分配的对象。比如Service Account A,可以只被允许对 Kubernetes API 进行 GET 操作,而 Service Account B,则可以有 Kubernetes API 的所有操作权限。

3.kubernetes.io/dockerconfigjson

• 用来创建用户docker registry认证的Secret,存储私有docker registry的认证信息。

• 直接使用kubectl create命令创建即可,如下:

[root@hd1 ~]# kubectl create secret docker-registry myregistry --docker-server=DOCKER_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL
secret/myregistry created
[root@hd1 ~]# kubectl get secret 
NAME                          TYPE                                  DATA   AGE
default-token-x49g5           kubernetes.io/service-account-token   3      9d
myregistry                    kubernetes.io/dockerconfigjson        1      12s
nfs-provisioner-token-2djmf   kubernetes.io/service-account-token   3      24h

• 若需拉去私有仓库中的docker镜像的话就需要使用到上面的myregistry这个Secret:

apiVersion: v1
kind: Pod
metadata:
  name: foo
spec:
  containers:
  - name: foo
    image: 192.168.1.100:5000/test:v1
  imagePullSecrets:
  - name: myregistry

3、Secret与ConfigMap对比

1.相同点

• key/value的形式

• 属于某个特定的namespace

• 可以导出到环境变量

• 可以通过目录/文件形式挂载

• 通过 volume 挂载的配置信息均可热更新

2.不同点

• Secret 可以被 ServerAccount 关联

• Secret 可以存储 docker register 的鉴权信息,用在 ImagePullSecret 参数中,用于拉取私有仓库的镜像

• Secret 支持 Base64 加密

• Secret 分为 kubernetes.io/service-account-token、kubernetes.io/dockerconfigjson、Opaque 三种类型,而 Configmap 不区分类型

4、Secret的创建

1.通过kubectl create secret 命令创建

#通过文件创建
[root@hd1 secret]# echo newuser > username.txt
[root@hd1 secret]# echo 123456 > password.txt
[root@hd1 secret]# kubectl create secret generic user --from-file=./username.txt
secret/user created
[root@hd1 secret]# kubectl create secret generic pass --from-file=./password.txt
secret/pass created
#通过键值对创建
[root@hd1 secret]# kubectl create secret generic user02 --from-literal=username=user02
secret/user02 created
[root@hd1 secret]# kubectl create secret generic pass02 --from-literal=password=123456
secret/pass02 created
#查看所创建的seccret,默认情况下key为文件名
[root@hd1 secret]# kubectl get secret |grep Opaque
pass                          Opaque                                1      2m19s
pass02                        Opaque                                1      14s
user                          Opaque                                1      2m30s
user02                        Opaque                                1      41s

2.通过yaml文件创建

[root@hd1 secret]# cat secret_test.yaml 
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  user: dXNlcjAzCg==
  pass: MTIzLmNvbQo=
#注:通过yaml创建Opaque类型的Secret值需要base64编码
[root@hd1 secret]# kubectl create -f secret_test.yaml 
[root@hd1 secret]# kubectl get secret |grep Opaque
mysecret                      Opaque                                2      13s

5、Secret的使用

1.通过Volume挂载

[root@hd1 secret]# cat test-secret-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: test-secret02 
spec:
  containers:
  - name: test-secret02-pod
    image: busybox:1.28
    imagePullPolicy: IfNotPresent
    args:
    - sleep
    - "86400"
    volumeMounts:
    - name: mysql-cred                #匹配挂载对象
      mountPath: "/projected-volume"  #指定挂载路径
      readOnly: true
  volumes:
  - name: mysql-cred
    projected:
      sources:
      - secret:
          name: mysecret         #匹配secret为刚创建的mysecret
#创建pod对象
[root@hd1 secret]# kubectl create -f test-secret-pod.yaml 
#Pod变成Running状态后,再验证 Secret 对象是否已在容器内
[root@hd1 secret]# kubectl exec -it test-projected-volume -- /bin/sh
/ # ls
bin               home              root              usr
dev               proc              sys               var
etc               projected-volume  tmp
/ # cd projected-volume/
/projected-volume # ls
password.txt  username.txt
/projected-volume # cat password.txt 
123456
/projected-volume # cat username.txt 
newuser
/projected-volume # exit

2.通过环境变量

[root@hd1 secret]# cat pod-secret-env.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-secret-env
spec:
  containers:
  - name: myapp
    image: busybox:1.28
    args:
      - sleep
      - "86400"
    env:            #环境变量设置
    - name: SECRET_USERNAME   #键名
      valueFrom:
        secretKeyRef:
          name: mysecret      #值来自
          key: user           #值指定
    - name: SECRET_PASSWORD
      valueFrom:
        secretKeyRef:
          name: mysecret
          key: pass
  restartPolicy: Never
#创建pod
[root@hd1 secret]# kubectl create -f pod-secret-env.yaml 
pod/pod-secret-env created  
#进入容器查看环境变量
[root@hd1 secret]# kubectl exec -it pod-secret-env -- /bin/sh
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # env |grep SECRET
SECRET_PASSWORD=123.com
SECRET_USERNAME=user03
/ # exit

3.Volume挂载与环境变量的区别

        通过Volume挂载到容器内部时,当该Secret的值发生变化时,容器内部具备自动更新的能力,但是通过环境变量设置到容器内部该值不具备自动更新的能力。所以一般推荐使用Volume挂载的方式使用Secret。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值