Secret创建键/值对,pod不同引用方法
创建Secret键/值对
kubectl create secret generic dev-mark --from-literal username=thanos --from-literal password=u9YuWVpoVu7m -n openeuler
secret/dev-mark created
kubectl get secret -n openeuler dev-mark
NAME TYPE DATA AGE
dev-mark Opaque 2 61s
挂载卷方法引用键/值对
生成pod yaml增加功能配置
kubectl run pod-secret --image=nginx --image-pull-policy=IfNotPresent -n openeuler --dry-run=client -o yaml > pod-secret.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: pod-secret
name: pod-secret
namespace: openeuler
spec:
volumes:
- name: devolume
secret:
secretName: dev-mark
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: pod-secret
resources: {}
volumeMounts:
- name: devolume
mountPath: /etc/secret
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
验证效果
kubectl apply -f pod-secret.yaml
kubectl exec -it pod-secret -n openeuler -- bash
root@pod2:/# cat /etc/secret/username
thanosroot@pod2:/#
root@pod2:/# cat /etc/secret/password
u9YuWVpoVu7mroot@pod2:/# exit
exit
变量方法引用键/值对
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: pod-secret
name: pod-secret
namespace: openeuler
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: pod-secret
resources: {}
env:
- name: devusername
valueFrom:
secretKeyRef:
name: dev-mark
key: username
- name: devpassword
valueFrom:
secretKeyRef:
name: dev-mark
key: password
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
验证效果
kubectl apply -f pod-secret.yaml
kubectl exec -it pod-secret -n openeuler -- bash
root@pod-secret:/# echo $devusername
thanos
root@pod-secret:/# echo $devpassword
u9YuWVpoVu7m
root@pod-secret:/#
exit