官网地址:https://kubernetes.io/zh/docs/concepts/configuration/secret/
Secret解决了密码、token、密钥等敏感数据的配置问题,将加密数据存放在etcd中,而不需要把这些敏感数据暴露到镜像或者Pod Spec中。Secret可以以Volume或者环境变量的方式使用。
Secret是用来保存小片敏感数据的k8s资源,例如密码,token,或者秘钥。这类数据当然也可以存放在Pod或者镜像中,但是放在Secret中是为了更方便的控制如何使用数据,并减少暴露的风险。
用户可以创建自己的secret,系统也会有自己的secret。
Pod需要先引用才能使用某个secret,Pod有2种方式来使用secret:
1、作为volume的一个域被一个或多个容器挂载;
2、在拉取镜像的时候被kubelet引用。
1、创建用户与密码文件
[root@master ~]# echo -n 'zhangsan' > /root/username.txt
[root@master ~]# echo -n 'zhangsan123' > /root/password.txt
[root@master ~]# kubectl create secret generic db-user-pass --from-file=/root/username.txt --from-file=/root/password.txt
secret/db-user-pass created
'//可以使用 kubectl create secret --help查看命令帮助'
2、查看secret资源
[root@master ~]# kubectl get secret
NAME TYPE DATA AGE
db-user-pass Opaque 2 11s
default-token-x8jtv kubernetes.io/service-account-token 3 21d
[root@master ~]# kubectl describe secret db-user-pass
Name: db-user-pass
Namespace: default
Labels:
Annotations:
Type: Opaque
Data
====
password.txt: 11 bytes
username.txt: 8 bytes
1、创建变量参数(进行base64解码 )
[root@master ~]# echo -n 'zhangsan' | base64
emhhbmdzYW4=
[root@master ~]# echo -n 'zhangsan123' | base64
emhhbmdzYW4xMjM=
2、创建yaml文件
[root@master ~]# vim secret.yaml
apiVersion: v1
kind: Secret '//指定secret类型'
metadata:
name: mysecret
type: Opaque
data:
username: emhhbmdzYW4= '//输入解码后的参数'
password: emhhbmdzYW4xMjM=
3、创建secret资源并查看详细信息
[root@master ~]# kubectl create -f secret.yaml
secret/mysecret created
[root@master ~]# kubectl get secret
NAME TYPE DATA AGE
db-user-pass Opaque 2 9m32s
default-token-x8jtv kubernetes.io/service-account-token 3 21d
mysecret Opaque 2 9s
[root@master ~]# kubectl describe secret mysecret
Name: mysecret
Namespace: default
Labels:
Annotations:
Type: Opaque
Data
====
password: 11 bytes
username: 8 bytes
1、调用secret资源中的变量
key: username赋值给SECRET_USERNAME
key: password 赋值给SECRET_PASSWORD
[root@master ~]# kubectl get secret mysecret -o yaml
apiVersion: v1
data:
password: emhhbmdzYW4xMjM=
username: emhhbmdzYW4=
kind: Secret
metadata:
creationTimestamp: 2020-05-20T15:05:48Z
name: mysecret
namespace: default
resourceVersion: "362715"
selfLink: /api/v1/namespaces/default/secrets/mysecret
uid: 635765b5-9aab-11ea-8c4f-000c294b2dd3
type: Opaque
2、创建yaml文件并创建资源
[root@master ~]# vim secret-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: nginx
image: nginx
env:
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: mysecret '//指定mysecret资源pod'
key: username '//指定用户名'
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret '//指定mysecret资源pod'
key: password '//指定密码'
[root@master ~]# kubectl apply -f secret-pod.yaml
pod/mypod created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 22s
3、登陆pod资源验证用户名和密码
[root@master ~]# kubectl exec -it mypod bash
root@mypod:/# echo $SECRET_USERNAME
zhangsan
root@mypod:/# echo $SECRET_PASSWORD
zhangsan123
root@mypod:/# exit
exit
以volume的形式挂载到pod的某个目录下
1、创建yaml文件资源
[root@master ~]# vim secret-volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod01
spec:
containers:
- name: nginx01
image: nginx
volumeMounts:
- name: foo
mountPath: "/etc/foo" '//容器内的挂载路径'
readOnly: true
volumes:
- name: foo
secret:
secretName: mysecret
[root@master ~]# kubectl create -f secret-volume.yaml
pod/mypod01 created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 6m36s
mypod01 1/1 Running 0 36s
2、登陆pod资源验证用户密码
[root@master ~]# kubectl exec -it mypod01 bash
root@mypod01:/# cd /etc/foo
root@mypod01:/etc/foo# ls
password username
root@mypod01:/etc/foo# cat password
zhangsan123root@mypod01:/etc/foo# cat username
zhangsanroot@mypod01:/etc/foo# exit
exit
configmap与Secret类似,区别在于ConfigMap保存的是不需要加密配置的信息
应用场景:应用配置
有两种创建方式:1、使用kubectl创建(yaml文件)2、使用变量参数创建
1、编写redis服务需要的配置并创建configmap资源
[root@master ~]# vim redis.properties
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
[root@master ~]# kubectl create configmap redis-config --from-file=redis.properties
configmap/redis-config created
2、查看configmap资源
[root@master ~]# kubectl get configmap
NAME DATA AGE
redis-config 1 7s
[root@master ~]# kubectl get cm '//configmap可以缩写成cm'
NAME DATA AGE
redis-config 1 15s
[root@master ~]# kubectl describe configmap redis-config
Name: redis-config
Namespace: default
Labels:
Annotations:
Data
====
redis.properties:
----
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
Events:
3、编写yaml文件并创建pod资源
[root@master ~]# vim cm.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod-2
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh","-c","cat /etc/config/redis.txt" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: redis-config
restartPolicy: Never
[root@master ~]# kubectl apply -f cm.yaml
pod/mypod02 created
[root@master ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 26m
mypod01 1/1 Running 0 20m
mypod02 0/1 ContainerCreating 0 6s
mypod02 0/1 Completed 0 43s
4、验证结果
^C[root@master ~]# kubectl logs mypod02
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
1、创建configmap资源
[root@master ~]# vim myconfig.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig
namespace: default
data:
special.level: info
special.type: hello
[root@master ~]# kubectl create -f myconfig.yaml
configmap/myconfig created
[root@master ~]# kubectl get cm
NAME DATA AGE
myconfig 2 5s
redis-config 1 15m
2、创建测试pod
[root@master ~]# vim configmap-test.yaml
apiVersion: v1
kind: Pod
metadata:
name: configmap-test
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
[root@master ~]# kubectl apply -f configmap-test.yaml
pod/configmap-test created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
configmap-test 0/1 Completed 0 24s
mypod 1/1 Running 0 33m
mypod01 1/1 Running 0 27m
mypod02 0/1 Completed 0 7m40s
3、查看变量输出结果
[root@master ~]# kubectl logs configmap-test
info hello