k8s——加密管理
Kubenetes的加密管理:
Secret
Secret 解决了密码、token、密钥等敏感数据的配置问题,而不需要把这些敏感数据暴露到镜像或者 Pod Spec 中。Secret 可以以 Volume 或者环境变量的方式使用,SECRET会以密文的方式存储,容器通过文件或者变量访问数据,Secret 主要保存的是一些密码等加密的信息.
Secret的创建(1):
[root@k8smaster~]# kubectl create secret generic mysecret --from-literal=username=test --from-literal=password=123456
secret/mysecret created
generic: 从本地 file, directory 或者 literal value 创建一个 secret
–from-literal: 对应一个键值对
查看:
[root@k8smaster~]# kubectl get secrets
NAME TYPE DATA AGE
default-token-hvq4p kubernetes.io/service-account-token 3 18d
mysecret Opaque 2 33s
详细查看:
[root@k8smaster~]# kubectl describe secrets mysecret
Name: mysecret
Namespace: default
Labels: <none>
Annotations: <none>
Type: Opaque
Data
====
password: 6 bytes
username: 4 bytes
Secret的创建(2):
[root@k8smaster]# echo -n test>username
[root@k8smaster~]# echo -n 123456 > password
[root@k8smaster~]# kubectl create secret generic mysecret1 --from-file=username --from-file=password secret/mysecret1 created
–from-file 指定文件,每个文件都有一个信息条目
Secret的创建(3):
[root@k8smaster]# cat <<EOF> secret.txt
username=test
password=123456
EOF
[root@k8smaster]# kubectlcreatesecretgenericmysecret2 --from-env-file=secret.txt
–from-env-file :指定一个文件
Secret的创建(4):
[root@k8smaster]# kubectl create secret generic mysecret3 --from-literal=username=test --from-literal=password=123456 --dry-run -o yaml > mysecret.yml
[root@k8smaster]# cat mysecret.yml
apiVersion: v1
data:
password: TOvb
username: dGVzdA==
kind: Secret
metadata:
creationTimestamp: null
name: mysecret3
[root@k8smaster]# kubectl apply-f mysecret.yml
Pod通过volume的方式使用secret
apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- name: pod
image: busybox
args:
- /bin/sh
- -c
- sleep 300000000
volumeMounts:
- mountPath: /etc/huge
name: aa
readOnly: true
volumes:
- name: aa
secret:
secretName: mysecret3
定义volume的名字为 使用下面指定的secret
运行并查看:
[root@k8smaster]# kubectl apply -f pod.yml
[root@k8smaster]# kubectl exec -it pod sh
/ # cd /etc/huge
cat username
test
cat password
123456
Secret还支持动态更新:
[root@k8smaster]# echo -n abcde | base64
[root@k8smaster]# YWJjZGU=
[root@k8smaster]# cat secret.yml
apiVersion: v1
data:
password: YWJjZGU=
username: dGVzdA==
kind: Secret
metadata:
creationTimestamp: null
name: mysecret3
执行查看:
[root@k8smaster]# kubectl apply -f secret.yml
[root@k8smaster]# kubectl exec -it pod sh
/ # cd /etc/huge
cat password
abcde
Pod通过环境变量的方式使用secret
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mypod
image: busybox
args:
- /bin/sh
- -c
- sleep 30000000000
env:
- name: myuser
valueFrom:
secretKeyRef:
name: mysecret3
key: username
- name: mypas
valueFrom:
secretKeyRef:
name: mysecret3
key: password
运行并查看:
[root@k8smaster]# kubectl apply -f pod.yml
[root@k8smaster]# kubectl exec -it mypod sh
/ # echo $myuser
test
/ # echo $mypas
abc
环境变量的方式不支持动态更新,但是读取很方便
configmap
加密配置文件可以用 configmap,一般情况下配置信息都是文件形式的,用yaml或者 --from-file 比较好
创建 configmap (1):
[root@k8smaster ]# kubectl create configmap myconmap --from-literal=test=1 --from-literal=test1=2
创建 configmap (2):
[root@k8smaster ]# echo -n 1 > test
[root@k8smaster ]# echo -n 2 > test1
[root@k8smaster ]# kubectl create configmap myconmap1 --from-file=test --from-file=test1
创建 configmap (3):
[root@k8smaster ]# cat <<EOF> envp.txt
test=1
test1=2
EOF
[root@k8smaster ]# kubectl create configmap myconmap2 --from-env-file=envp.txt
创建 configmap (4):
[root@k8smaster ]# kubectl create configmap myconmap2 --from-env-file=envp.txt --dry-run -o yaml > myconmap.yml
[root@k8smaster ]# cat myconmap.yml
apiVersion: v1
data:
test: "1"
test1: "2"
kind: ConfigMap
metadata:
creationTimestamp: null
name: myconmap2
Pod通过volume使用configmap:
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mypod
image: busybox
args:
- /bin/sh
- -c
- sleep 300000000000
volumeMounts:
- mountPath: /etc/aa
name: aa
readOnly: true
volumes:
- name: aa
configMap:
name: myconmap2
运行并查看:
[root@k8smaster ]# kubectl apply -f pod.yml
[root@k8smaster ]# kubectl exec -it mypod sh
/ # cd /etc/
cat test
1
cat test1
2
Pod通过环境变量使用configmap
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mypod
image: busybox
args:
- /bin/sh
- -c
- sleep 300000000000
env:
- name: myuser
valueFrom:
configMapKeyRef:
name: myconmap3
key: test
- name: mypas
valueFrom:
configMapKeyRef:
name: myconmap3
key: test1
运行并查看:
[root@k8smaster ]# kubectl apply -f pod.yml
[root@k8smaster ]# kubectl exec -it mypod sh
/ # echo $myuser
1
/ # echo $mypas
2