k8s之安全信息(secret)及配置信息(configmap)管理


前言

在K8s中有Secret和configMap这两种资源对象,这也是实现数据持久化的一种方式,与之前写过的PV或挂载目录等这些数据持久化的方式有些许不一样。

  • Secret资源对象:可以保存轻量的敏感信息,比如数据库的用户名和密码或者认证秘钥等。 它保存的数据是以秘文的方式存放的

  • configMap资源对象:和Secret一样,拥有大多数共同的特性,但是区别是,configMap保存的是 一些不太重要的信息,它保存的数据是以明文的方式存放的。

当我们创建上述两种资源对象时,其实就是将这两种资源对象存储的信息写入了k8s群集中的etcd数据中心。

一:secret和configMap的区别

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

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

## 二:创建Secert方式

加密数据并存放在Etcd中,让Pod的容器以挂载Volume方式访问

### 2.1:通过 --from-file:

- 使用`--from-file`的方式,文件中的数据不用使用base64加密。和ConfigMap类似,如果不指定key的名称,默认使用文件名。

```handlebars
'采用的加密方式是generic(通用的、一般的加密方式)''
''// 注意,一个--from-literal语句 ,只能保存一条信息'

'创建用户名/密码文件'
echo -n 'shuaige' > ./username.txt
echo -n '123123' > ./password.txt

'写入Server对象中'
kubectl create secret generic shuai-user-pass --from-file=./username.txt --from-file=./password.txt
  • 查看secret资源
[root@master ~]# kubectl get secret
NAME                  TYPE                                  DATA   AGE
default-token-cfdcs   kubernetes.io/service-account-token   3      16d
shuai-user-pass       Opaque                                2      13s

'查看刚写的详细信息'
[root@master ~]# kubectl describe secret shuai-user-pass
Name:         shuai-user-pass
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque   '表示不透明的,看不见的'

Data
====
password.txt:  6 bytes   '只能查看键的名称,无法查看键对应的值'
username.txt:  7 bytes
  • 查看刚才创建的secret使用yaml方式查看
[root@master ~]# kubectl get secret shuai-user-pass -o yaml
apiVersion: v1
data:
  password.txt: MTIzMTIz
  username.txt: c2h1YWlnZQ==
kind: Secret
metadata:
  creationTimestamp: 2020-10-15T13:43:24Z
  name: shuai-user-pass
  namespace: default
  resourceVersion: "349589"
  selfLink: /api/v1/namespaces/default/secrets/shuai-user-pass
  uid: 65e06dfd-0eec-11eb-a2d8-000c2984c1e3
type: Opaque

2.2:通过YAML创建Secret

  • 先将要保存的值进行Base64编码
[root@master ~]# echo -n 'shuaige' | base64
c2h1YWlnZQ==
[root@master ~]# echo -n '123123' | base64
MTIzMTIz
  • 编写yaml文件
vim secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: mysecret           '//名称不可以有大写'      
type: Opaque
data: 
  username: c2h1YWlnZQ==   '//将加密后的形成的值写到配置文件中'
  password: MTIzMTIz
  • 执行 kubectl apply 创建 Secret
[root@master shuai]# kubectl apply -f secret.yaml
  • 查看secret
[root@master shuai]# kubectl get secret
NAME                  TYPE                                  DATA   AGE
default-token-cfdcs   kubernetes.io/service-account-token   3      16d
mysecret              Opaque                                2      97s
shuai-user-pass       Opaque                                2      32m
  • 查看条目key
[root@master shuai]# kubectl describe secret mysecret
Name:         mysecret
Namespace:    default
Labels:       <none>
Annotations:  
Type:         Opaque

Data
====
password:  6 bytes
username:  7 bytes
  • 通过kubectl edit secret mysecret 查看vlaue
apiVersion: v1
data:
  password: MTIzMTIz
  username: c2h1YWlnZQ==
kind: Secret
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"password":"MTIzMTIz","username":"c2h1YWlnZQ=="},"kind":"Secret","metadata":{"annotations":{},"name":"mysecret","namespace":"default"},"type":"Opaque"}
  creationTimestamp: 2020-10-15T14:13:59Z
  name: mysecret
  namespace: default
  resourceVersion: "352162"
  selfLink: /api/v1/namespaces/default/secrets/mysecret
  uid: ab28ca46-0ef0-11eb-a2d8-000c2984c1e3
type: Opaque
注:即使,在保存数据前,我们对要保存的数据做了加密处理,但,base64这种方法也不是绝对
的安全,比如上边我们用base64这种方法得到的乱码字符串,就可以使用--decode解码如下:
  • 通过base64将value反解码
[root@master shuai]# echo -n c2h1YWlnZQ== | base64 --decode
shuaige[root@master shuai]# echo -n MTIzMTIz | base64 --decode
123123[root@master shuai]# 

2.3:以环境变量的方式使用secret

2.31:方式一:使用secret中的变量导入到pod中
  • 查看刚才创建的secret使用yaml方式查看
kubectl get secret mysecret -o yaml

apiVersion: v1
data:
  password: MTIzMTIz
  username: c2h1YWlnZQ==
kind: Secret
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"password":"MTIzMTIz","username":"c2h1YWlnZQ=="},"kind":"Secret","metadata":{"annotations":{},"name":"mysecret","namespace":"default"},"type":"Opaque"}
  creationTimestamp: 2020-10-15T14:13:59Z
  name: mysecret
  namespace: default
  resourceVersion: "352162"
  selfLink: /api/v1/namespaces/default/secrets/mysecret
  uid: ab28ca46-0ef0-11eb-a2d8-000c2984c1e3
type: Opaque
  • 编写yaml文件

key: username赋值给SECRET_USERNAME

key: password 赋值给SECRET_PASSWORD

vim secret.var.yaml

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: nginx
    image: nginx
    env:                         '设置环境变量'
      - name: SECRET_USERNAME    '容器变量名称'
        valueFrom:
          secretKeyRef:
            name: mysecret       '调用的是mysecret'
            key: username        '对应的是mysecret中username对应的值'
      - name: SECRET_PASSWORD    '第二个环境变量名'
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: password
  • 创建资源
[root@master shuai]# kubectl apply -f secret.var.yaml 

[root@master shuai]# kubectl get pods
NAME                     READY   STATUS      RESTARTS   AGE
mypod                    1/1     Running     0          40s

'进入容器之后,查看变量对应的值'
[root@master shuai]# kubectl exec -it mypod bash
root@mypod:/# echo $SECRET_USERNAME
shuaige
root@mypod:/# echo $SECRET_PASSWORD
123123

注:通过发现,用环境变量的方式也可以正确引用secret资源,但是,它并不会像Volume的方式一样,它引用数据不会进行动态的更新,除非重新生成pod。

2.4:volume 方式使用 Secret

Pod 可以通过 Volume 或者环境变量的方式使用 Secret,先学习 Volume 方式。

Pod 的配置文件如下所示:

vim secret-vol.yaml

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: foo
      mountPath: "/etc/foo"     '//指定容器中的目录'
      readOnly: true            '//以只读的方式挂载'
  volumes:
  - name: foo                   '//定义 volume foo,来源为 secret mysecret'
    secret:
      secretName: mysecret      '//指定的是已有的secret资源的名称'
  • 创建 Pod 并在容器中读取 Secret:
'删除原有资源'
[root@master shuai]# kubectl delete -f secret.var.yaml 

'创建资源'
[root@master shuai]# kubectl apply -f secret-vol.yaml 

'查看pod资源'
[root@master shuai]# kubectl get pods
NAME                     READY   STATUS      RESTARTS   AGE
mypod                    1/1     Running     0          48s
  • 进入容器查看数据
[root@master shuai]# kubectl exec -it mypod bash
root@mypod:/# ls /etc/foo
password  username
root@mypod:/# cat /etc/foo/username 
shuaigeroot@mypod:/# cat /etc/foo/password 

'一样查看'
[root@master shuai]# kubectl exec -it mypod bash

'//查看对应目录是否存在数据'
root@mypod:/# cat -n /etc/foo/username /etc/foo/password 
     1	shuaige123123root@mypod:/# 

三:configMap数据的两种引用方式

大多数情况下,配置信息都以文件形式提供,所以在创建 ConfigMap 时通常采用 --from-file 或 YAML 方式读取 ConfigMap 时通常采用 Volume 方式。

比如给 Pod 传递如何记录日志的配置信息:

3.1:创建kubectl

  • 编写redis服务需要的配置并创建configmap资源
vim redis.properties

redis.host=127.0.0.1
redis.port=6379
redis.password=123456
  • 创建configmap资源
[root@master shuai]# kubectl create configmap redis-config --from-file=redis.properties
configmap/redis-config created

'查看资源'
[root@master shuai]# kubectl get configmap
NAME           DATA   AGE
redis-config   1      17s

'简写查看'
[root@master shuai]# kubectl get cm
NAME           DATA   AGE
redis-config   1      37s

'查看详细信息'
[root@master shuai]# kubectl describe cm 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>
  • 如果想看到键值的化,可以用kubectl get
'我们已yaml格式输出配置'
[root@master shuai]# kubectl get configmaps redis-config -o yaml
apiVersion: v1
data:
  redis.properties: |
    redis.host=127.0.0.1
    redis.port=6379
    redis.password=123456
kind: ConfigMap
metadata:
  creationTimestamp: 2020-10-15T15:52:49Z
  name: redis-config
  namespace: default
  resourceVersion: "360526"
  selfLink: /api/v1/namespaces/default/configmaps/redis-config
  uid: 79aa92e1-0efe-11eb-a2d8-000c2984c1e3
  • 创建mypod资源查看文件导入
vim cm.yaml

apiVersion: v1
kind: Pod
metadata:
  name: mypod
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
  • 创建资源
'删掉之前创建重名的mypod'
[root@master shuai]# kubectl delete pod mypod

'创建资源'
[root@master shuai]# kubectl apply -f cm.yaml 

[root@master shuai]# kubectl get pods
NAME                     READY   STATUS      RESTARTS   AGE
mypod                    0/1     Completed   0          6s
  • 查看里面配置信息
[root@master shuai]# kubectl logs mypod
redis.host=127.0.0.1
redis.port=6379
redis.password=123456

3.2:第二种变量参数形式

  • 创建configmap资源
vim myconfig.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: myconfig
  namespace: default
data:
  special.level: info
  special.type: hello
  
'创建资源'
kubectl apply -f myconfig.yaml 

'查看资源'
kubectl get cm
NAME           DATA   AGE
myconfig       2      3m     'configmap资源名myconfig'
  • 创建mymod使用configmap资源输出变量参数
vim config-var.yaml

apiVersion: v1
kind: Pod
metadata:
  name: mypod
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     '引入资源名myconfig'
              key: special.type   '资源变量'
  restartPolicy: Never
  • 创建mypod资源
kubectl apply -f config.yaml 

kubectl get pods
NAME                     READY   STATUS      RESTARTS   AGE
mypod                    0/1     Completed   0          5s
  • 查看变量的输出
[root@master shuai]# kubectl logs mypod
info hello

本次实验结束,感谢观看

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值