Sercret篇

概述

应用启动过程中可能需要一些敏感信息,比如访问数据库的用户 名、密码或者密钥。将这些信息直接保存在容器镜像中显然不妥, Kubernetes提供的解决方案是Secret。

Secret会以密文的方式存储数据,避免了直接在配置文件中保存 敏感信息。Secret会以Volume的形式被mount到Pod,容器可通过文件 的方式使用Secret中的敏感数据;此外,容器也可以环境变量的方式 使用这些数据。

Secret可通过命令行或YAML创建。比如希望Secret中包含如下信 息:用户名admin、密码123456。

创建secret

有四种方法创建Secret:

  • 通过–from-literal:每个–from-literal对应一个信息条目。
 # Create a new secret named my-secret with key1=supersecret and key2=topsecret
  kubectl create secret generic my-secret --from-literal=key1=supersecret
--from-literal=key2=topsecret
  • 通过–from-file:每个文件内容对应一个信息条目
echo -n admin > ./username 
echo -n 123456 > ./password 
kubectl create secret generic mysecret --from-file=./username ./password
  • 通过–from-env-file:
    文件env.txt中每行Key=Value对应一个信息条目
[root@master1 secret]# cat << EOF > env.txt 
> username=admin 
> password=123456
> EOF
[root@master1 secret]# kubectl create secret generic mysecret --from-env-file=env.txt
secret/mysecret created
[root@master1 secret]# kubectl describe  secret mysecret
Name:         mysecret
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
password:  6 bytes
username:  6 bytes
  • 通过YAML配置文件(文件中的敏感数据必须是通过base64编码后的结果)
[root@master1 secret]# echo -n admin | base64
YWRtaW4=
[root@master1 secret]# echo -n 123456 | base64
MTIzNDU2
[root@master1 secret]# cat mysecret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
data:
  username: YWRtaW4=
  password: MTIzNDU2
[root@master1 secret]# kubectl apply -f mysecret.yaml
secret/mysecret created

查看Secret

  • 通过kubectl get secret查看存在的secret
[root@master1 secret]# kubectl get secret
NAME                          TYPE                                  DATA   AGE
default-token-75kvr           kubernetes.io/service-account-token   3      17d
mysecret                      Opaque                                2      109s
  • 通过kubectl describe secret查看条目的 Key
[root@master1 secret]# kubectl describe secret mysecret
Name:         mysecret
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====   # 显示有两个数据条目
password:  6 bytes
username:  5 bytes
  • 如果还想查看Value,可以用kubectl edit secret mysecret,然后通过base64将Value反编码即可看到原明文
    在这里插入图片描述
[root@master1 secret]# kubectl edit secret mysecret
Edit cancelled, no changes made.
[root@master1 secret]# echo -n MTIzNDU2 | base64 -d
123456
[root@master1 secret]# echo -n  YWRtaW4= | base64 -d
admin

删除Secret

[root@master1 secret]# kubectl delete secret mysecret
secret "mysecret" deleted

在Pod中使用Secret

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

Volume方式

[root@master1 secret]# cat volume-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
data:
  username: YWRtaW4=
  password: MTIzNDU2
---
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: mypod
      image: busybox
      args:
        - /bin/sh
        - -c
        - sleep 10; touch /tmp/healthy; sleep 30000
      volumeMounts:
        - name: foo
          mountPath: "/etc/foo"
          readOnly: true
  volumes:
  - name: foo
    secret:
      secretName: mysecret
[root@master1 secret]# kubectl apply -f volume-secret.yaml
secret/mysecret unchanged
pod/mypod created

① 定义volume foo,来源为secret mysecret。
② 将foo mount到容器路径/etc/foo,可指定读写权限为 readOnly。

[root@master1 secret]# kubectl exec -it mypod -- /bin/sh
/ # cat /etc/foo/username 
admin
/ # cat /etc/foo/password
123456/ # 

可以看到,Kubernetes会在指定的路径/etc/foo下为每条敏感数据 创建一个文件,文件名就是数据条目的Key,这里是/etc/foo/username和/etc/foo/password,Value则以明文存放在文件中.

  • 我们也可以自定义存放数据的文件名
    在这里插入图片描述
    这时数据将分别存放在/etc/foo/my-group/my-username 和/etc/foo/my-group/my-password中
[root@master1 secret]# kubectl apply -f volume-secret.yaml
secret/mysecret created
pod/mypod created
[root@master1 secret]# kubectl exec -it mypod -- /bin/sh
/ # cat /etc/foo/my-group/my-username 
admin
/ # cat /etc/foo/my-group/my-password 
123456/ # exit
  • 以Volume方式使用的Secret支持动态更新:Secret更新后,容器 中的数据也会更新。
    将password更新为abcdef,base64编码为YWJjZGVm
    在这里插入图片描述
[root@master1 secret]# kubectl apply -f volume-secret.yaml
secret/mysecret configured
pod/mypod configured
[root@master1 secret]# kubectl exec -it mypod -- /bin/sh
/ # cat /etc/foo/my-group/my-password 
123456/ # 
# 需要几秒钟后,新的password会同步到容器
/ # cat /etc/foo/my-group/my-password 
abcdef/ # 

环境变量方式

通过Volume使用Secret,容器必须从文件读取数据,稍显麻烦, Kubernetes还支持通过环境变量使用Secret

[root@master1 secret]# cat env-mysecret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
data:
  username: YWRtaW4=
  password: YWJjZGVm
---
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 10; touch /tmp/healthy;sleep 30000
    env:
    - name: SECRET_USERNAME
      valueFrom: 
        secretKeyRef:
          key: username
          name: mysecret
    - name: SECRET_PASSWORD
      valueFrom: 
        secretKeyRef:
          key: password
          name: mysecret
 
[root@master1 secret]# kubectl apply -f env-mysecret.yaml
secret/mysecret unchanged
pod/mypod created
[root@master1 secret]# kubectl exec -it mypod -- /bin/sh
/ # printenv | grep SECRET
SECRET_PASSWORD=abcdef
SECRET_USERNAME=admin

通过环境变量SECRET_USERNAME和SECRET_PASSWORD成 功读取到Secret的数据。 需要注意的是,环境变量读取Secret很方便,但无法支撑Secret动 态更新。

ConfigMap

概述

Secret可以为Pod提供密码、Token、私钥等敏感数据;对于一些 非敏感数据,比如应用的配置信息,则可以用ConfigMap
ConfigMap的创建和使用方式与Secret非常类似,主要的不同是 数据以明文的形式存放

Configmap能解决哪些问题?

我们在部署服务的时候,每个服务都有自己的配置文件,如果一台服务器上部署多个服务:nginx、tomcat、apache等,那么这些配置都存在这个节点上,假如一台服务器不能满足线上高并发的要求,需要对服务器扩容,扩容之后的服务器还是需要部署多个服务:nginx、tomcat、apache,新增加的服务器上还是要管理这些服务的配置,如果有一个服务出现问题,需要修改配置文件,每台物理节点上的配置都需要修改,这种方式肯定满足不了线上大批量的配置变更要求。 所以,k8s中引入了Configmap资源对象,可以当成volume挂载到pod中,实现统一的配置管理。
在这里插入图片描述

  • Configmap是k8s中的资源, 相当于配置文件,可以有一个或者多个Configmap
  • Configmap可以做成Volume,k8s pod启动之后,通过 volume 形式映射到容器内部指定目录上
  • 容器中应用程序按照原有方式读取容器特定目录上的配置文件
  • 在容器看来,配置文件就像是打包在容器内部特定目录,整个过程对应用没有任何侵入

ConfigMap创建的方法

与Secret一样,ConfigMap也支持四种创建方式

  • 通过–from-literal:
    每个–from-literal对应一个信息条目。

kubectl create configmap myconfigmap --from-literal=config1=xxx

[root@master1 configmap]# kubectl create configmap tomcat-config --from-literal=tomcat_port=8080 --from-literal=server_name=myapp.tomcat.com
  • 通过–from-file:
    每个文件内容对应一个信息条目

echo -n xxx > ./config1
echo -n yyy > ./config2
kubectl create configmap myconfigmap --from-file=./config1 --from-file ./config2

[root@master1 configmap]# cat nginx.conf
server {
  server_name www.nginx.com;
  listen 80;
  root /home/nginx/www/
}
[root@master1 configmap]# kubectl create configmap www-nginx --from-file=www=./nginx.conf
  • 指定目录创建
[root@master1 ~]# mkdir test-a
[root@master1 ~]# cd test-a/
[root@master1 test-a]# cat my-server.cnf 
server-id=1
[root@master1 test-a]# cat my-slave.cnf 
server-id=2
#指定目录创建configmap
[root@master1 test-a]# kubectl create configmap mysql-config --from-file=/root/test-a/
  • 通过–from-env-file
    文件env.txt中每行Key=Value对应一个信息条目

cat << EOF > env.txt
config1=xxx
config2=yyy
EOF
kubectl create configmap myconfigmap --from-env-file=env.txt

  • 通过YAML配置文件
    在这里插入图片描述

查看comfigmap

[root@master1 configmap]# kubectl get cm
NAME               DATA   AGE
kube-root-ca.crt   1      17d
myconfigmap        2      68s
mysql-config       2      3d13h
tomcat-config      2      3d13h
www-nginx          1      3d13h
You have new mail in /var/spool/mail/root
[root@master1 configmap]# kubectl describe cm myconfigmap
Name:         myconfigmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
config1:
----
xxx
config2:
----
yyy
Events:  <none>

在Pod中使用ConfigMap

通过环境变量引入:使用configMapKeyRef

  • 创建一个mysql的configmap
[root@master1 test-a]# cat mysql-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql
  labels:
    app: mysql
data:
    log: "1"
    lower: "1"
[root@master1 test-a]# kubectl apply -f  mysql-configmap.yaml
configmap/mysql created
  • 创建pod,引用Configmap中的内容
[root@master1 test-a]# cat mysql-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mysql-pod
spec:
  containers:
  - name: mysql
    image: busybox
    command: [ "/bin/sh", "-c", "sleep 3600" ]
    env:
    - name: log_bin   #定义环境变量log_bin
      valueFrom: 
        configMapKeyRef:
          name: mysql     #指定configmap的名字
          key: log #指定configmap中的key
    - name: lower   #定义环境变量lower
      valueFrom:
        configMapKeyRef:
          name: mysql
          key: lower
  restartPolicy: Never
  • 测试环境变量是否生效
[root@master1 test-a]# kubectl apply -f mysql-pod.yaml
pod/mysql-pod created
[root@master1 test-a]# kubectl exec -it mysql-pod -- /bin/sh
/ # printenv | grep lo
log_bin=1
lower=1

把configmap做成volume,挂载到pod

  • 创建configmap
[root@master1 test-a]# cat mysql-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql
  labels:
    app: mysql
data:
    log: "1"
    lower: "1"
    my.cnf: |
      [mysqld]
      Welcome=hello 
[root@master1 test-a]# kubectl apply -f mysql-configmap.yaml 
configmap/mysql configured
  • 创建pod
[root@master1 test-a]# cat mysql-pod-volume.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mysql-pod-volume
spec:
  containers:
  - name: mysql
    image: busybox
    command: [ "/bin/sh","-c","sleep 3600" ]
    volumeMounts:
    - name: mysql-config
      mountPath: /tmp/config
  volumes:
  - name: mysql-config
    configMap:
      name: mysql
  restartPolicy: Never
[root@master1 test-a]# kubectl apply -f mysql-pod-volume.yaml
pod/mysql-pod-volume created
[root@master1 test-a]# kubectl exec -it mysql-pod-volume -- /bin/sh
/ # cd /tmp/config/
/tmp/config # ls
log     lower   my.cnf
/tmp/config # cat log
1

Configmap热更新

[root@master1 test-a]# kubectl edit configmap mysql

在这里插入图片描述

[root@master1 test-a]# kubectl exec -it mysql-pod-volume -- /bin/sh
/ #  cat /tmp/config/log
2/ # 

注意:
更新 ConfigMap 后:
使用该 ConfigMap 挂载的 Env 不会同步更新
使用该 ConfigMap 挂载的 Volume 中的数据需要一段时间(实测大概10秒)才能同步更新

小结

学习了如何向Pod传递配置信息。如果信息需要加密, 可使用Secret;如果是一般的配置信息,则可使用ConfigMap。 Secret和ConfigMap支持四种定义方法。Pod在使用它们时,可以 选择Volume方式或环境变量方式,不过只有Volume方式支持动态更 新

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐