官方文档:https://kubernetes.io/docs/concepts/configuration/secret/
echo -n ‘admin‘ > ./username.txt
echo -n ‘1f2d1e2e67df‘ > ./password.txt
kubectl create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txt
kubectl get secret
NAME TYPE DATA AGE
db-user-pass Opaque 2 37s
kubectl describe secret db-user-pass
Name: db-user-pass
Namespace: default
Labels: <none>
Annotations: <none>
Type: Opaque
Data
====
password.txt: 12 bytes
username.txt: 5 bytes
echo -n ‘admin‘ | base64
echo -n ‘1f2d1e2e67df‘ | base64
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: MWYyZDFlMmU2N2Rm
kubectl create -f user.yaml
kubectl get secret
NAME TYPE DATA AGE
db-user-pass Opaque 2 5m42s
mysecret Opaque 2 18s
vim secret-var.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: nginx
image: nginx
env:
# 环境变量名称:用户
- name: SECRET_USERNAME
valueFrom:
# 选择输入secret用户
secretKeyRef:
name: mysecret
key: username
# 环境变量名称:密码
- name: SECRET_PASSWORD
valueFrom:
# 选择输入secret密码
secretKeyRef:
name: mysecret
key: password
kubectl create -f secret-var.yaml
kubectl get pods
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 23s
kubectl exec -it mypod bash
root@mypod:/# echo $SECRET_USERNAME
admin
root@mypod:/# echo $SECRET_PASSWORD
1f2d1e2e67df
cat 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
secret:
# 创建的secret
secretName: mysecret
kubectl create -f secret-vol.yaml
kubectl get pod
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 46s
kubectl exec -it mypod bash
root@mypod:/# ls /etc/foo/
password username
root@mypod:/# cat /etc/foo/password
1f2d1e2e67dfroot@mypod:/# cat /etc/foo/username
adminroot@mypod:/#
原文:https://www.cnblogs.com/xhyan/p/13591423.html