8. Secret
问题:
参考链接
在 namespace monitoring 中获取名为db1-test 的现有 secret 的内容。
将 username 字段存储在名为/home/candidate/user.txt 的文件中,
并将password 字段存储在名为/home/candidate/old-password.txt 的文件中。
:::info
您必须创建以上两个文件;它们还不存在
:::
不要在以下步骤中使用/修改先前创建的文件,如果需要,创建新的临时文件。
在 monitoring namespace 中创建一个名为dev-mark 的新 secret,内容如下:
username:production-instance
password: aV7HR7nU3JLX
最后,创建一个新的 Pod,它可以通过卷访问secret dev-mark:
Pod 名 secret-pod
Namespace monitoring
容器名 test-secret-container
镜像 redis
卷名 secret-volume
挂载路径 /etc/test-secret
正确答案:
记住这里这个参数:jsonpath={.data.username},是需要符合yaml文件编写标准的。如果你要查询username的字段,必须要写清楚是在哪个模板下。
root@hk8s-master01:~# kubectl get secrets -n monitoring db1-test -o jsonpath={.data.username} | base64 -d > /home/candidate/user.txt
root@hk8s-master01:~# cat /home/candidate/user.txt
admin
root@hk8s-master01:~# kubectl get secrets -n monitoring db1-test -o jsonpath={.data.password} | base64 -d ># cat /home/candidate/passwd.txt
root@hk8s-master01:~# cat /home/candidate/old-password.txt
pass
在 monitoring namespace 中创建一个名为dev-mark 的新 secret
root@hk8s-master01:~# vim 8.yaml
1 apiVersion: v1
2 kind: Secret
3 metadata:
4 name: dev-mark
5 namespace: monitoring
6 type: kubernetes.io/basic-auth
7 stringData:
8 username: production-instance
9 password: aV7HR7nU3JLX
做第三问的时候注意了,hostpath是挂载到主机的路径,而我们题干的要求是挂载到secret。所以写的时候就不能加hostpath这个参数了.
apiVersion: v1
kind: Pod
metadata:
name: secret-pod
namespace: monitoring
spec:
containers:
- name: test-secret-container
image: redis
imagePullPolicy: IfNotPresent
volumeMounts:
- name: secret-volume
mountPath: /etc/test-secret
volumes:
- name: secret-volume
secret: #只写secret就行
secretName: dev-mark
# hostPath: 不加这个参数