干货!K8S配置管理之Secret+ConfigMap详解

一、secret概述

  • Secret:机密 验证凭据
  • 加密数据并存放在Etcd中,让Pod的容器以挂载Volume方式访问
  • 应用场景:凭据

参考官方文档:https://kubernetes.io/docs/concepts/configuration/secret/
在这里插入图片描述

1.1、secret创建方式

方式一:使用kubectl create secret方式创建

创建名为secret01的secret资源

##编写secret01数据信息,并创建
[root@k8s_master ~]# echo -n 'admin' > ./username.txt
[root@k8s_master ~]# echo -n '1f2d1e2e67df' > ./password.txt
[root@k8s_master ~]# kubectl create secret generic secret01 --from-file=./username.txt --from-file=./password.txt

查看已经创建的secret信息

[root@k8s_master ~]# kubectl describe secret secret01
Name:         secret01
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
password.txt:  12 bytes
username.txt:  5 bytes

方式二:编写yaml文件,引用64位编码

先通过64位编码方式,明文转密文

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

编写yaml文件,引用密文创建secret

[root@k8s_master ~]# vim secret02.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  username: YWRtaW4=
  password: MWYyZDFlMmU2N2Rm

##创建secret资源
[root@k8s_master ~]# kubectl apply -f secret02.yaml

查看已经创建的secret信息

[root@k8s_master ~]# kubectl describe secret mysecret
Name:         mysecret
Namespace:    default
Labels:       <none>
Annotations:  
Type:         Opaque

Data
====
password:  12 bytes
username:  5 bytes

1.2、secret资源的使用方式

第一种:使用secret中的变量导入到pod中

示例:将上面secret资源中的username和password重新赋值
key:username赋值给aaa
key:password 赋值给bbb

编写pod资源,将secret中的变量赋值

##编写yaml文件
[root@k8s_master ~]# vim secret03.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: nginx
    image: nginx
    env:
      - name: aaa
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: username
      - name: bbb
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: password

##创建pod资源
[root@k8s_master ~]# kubectl apply -f secret03.yaml

进入该pod资源,验证secret信息

##查看pod资源创建情况
[root@k8s_master ~]# kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
mypod                             1/1     Running   0          29s

##进入pod,验证secret信息
[root@k8s_master ~]# kubectl exec -it mypod bash
root@mypod:/# echo $aaa
admin
root@mypod:/# echo $bbb
1f2d1e2e67df

第二种:将secret容器以volume的形式挂载到pod的某个目录下

示例:

##编写yaml文件,创建pod资源
[root@k8s_master ~]# vim secret04.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mypod02
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: foo
      mountPath: "/etc/foo"
      readOnly: true
  volumes:
  - name: foo
    secret:
      secretName: mysecret

##创建pod资源
[root@k8s_master ~]# kubectl create -f secret04.yaml 

进入该pod查看数据卷中的secret信息

[root@k8s_master ~]# kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
mypod                             1/1     Running   0          7m35s
mypod02                           1/1     Running   0          39s

##进入pod,查看secret信息
[root@k8s_master ~]# kubectl exec -it mypod02 bash
root@mypod02:/# cd /etc/foo
root@mypod02:/etc/foo# ls
password  username
root@mypod02:/etc/foo# cat username 
adminroot
root@mypod02:/etc/foo# cat password 
1f2d1e2e67

二、ConfigMap概述

  • 与Secret类似,区别在于ConfigMap保存的是不需要加密配置的信息
  • 应用场景:应用配置
2.1、configMap创建方式

方式一:使用kubectl创建

示例:

  • 创建redis的属性配置文件
[root@k8s_master ~]# vim redis.properties
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
  • 创建configmap资源
[root@k8s_master ~]# kubectl create configmap redis-config --from-file=redis.properties
  • 查看资源信息
[root@k8s_master ~]# kubectl get configmap 
NAME           DATA   AGE
redis-config   1      74s

[root@k8s_master ~]# kubectl describe configmap redis-config
Name:         redis-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
redis.properties:
----
redis.host=127.0.0.1
redis.port=6379
redis.password=123456

Events:  <none>

方式二:变量参数形式

示例:

创建configmap资源,定义变量
special.level: info
special.type: hello

##编写yaml文件
[root@k8s_master ~]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: myconfig
  namespace: default
data:
  special.level: info
  special.type: hello

##创建configap资源
[root@k8s_master ~]# kubectl apply -f configmap.yaml
  • 查看configmap资源信息
[root@k8s_master ~]# kubectl describe configmap myconfig
Name:         myconfig
Namespace:    default
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"special.level":"info","special.type":"hello"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"myconfig...

Data
====
special.level:
----
info
special.type:
----
hello
2.2、configmap的使用

方法一:创建mypod资源查看文件导入(引用redis-config容器)

##编写yaml文件
[root@k8s_master ~]# vim test01.yaml

apiVersion: v1
kind: Pod
metadata:
  name: mypod01
spec:
  containers:
    - name: busybox
      image: busybox
      command: [ "/bin/sh","-c","cat /etc/config/redis.properties" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: redis-config
  restartPolicy: Never

##创建资源
[root@k8s_master ~]# kubectl apply -f test01.yaml
  • 查看资源内的配置信息
##查看资源创建情况
[root@k8s_master ~]# kubectl get pods
NAME                              READY   STATUS      RESTARTS   AGE
mypod01                           0/1     Completed   0          60s

##查看该资源信息
[root@k8s_master ~]# kubectl logs mypod01
redis.host=127.0.0.1
redis.port=6379
redis.password=123456

方式二:使用configmap资源输出变量参数(引用myconfig容器)

##编写yaml文件
[root@k8s_master ~]# vim test02.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mypod05
spec:
  containers:
    - name: busybox
      image: busybox
      command: [ "/bin/sh", "-c", "echo $(LEVEL) $(TYPE)" ]
      env:
        - name: LEVEL
          valueFrom:
            configMapKeyRef:
              name: myconfig
              key: special.level
        - name: TYPE
          valueFrom:
            configMapKeyRef:
              name: myconfig
              key: special.type
  restartPolicy: Never

##创建pod资源
[root@k8s_master ~]# kubectl apply -f test02.yaml
  • 查看pod资源信息
[root@k8s_master ~]# kubectl logs mypod05
info hello
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值