真题解析

题目

在 namespace monitoring 中获取名为 db1-test 的现有 secret 的内容

username 字段存储在名为 /home/candidate/user.txt 的文件中,并将 password 字段存储在名为 /home/candidate/old-password.txt 的文件中。

您必须创建以上两个文件;它们还不存在。

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

解析

获取 secret 加密字符串

kubectl get secret db1-test -n monitoring -o yaml
apiVersion: v1
data:
  password: cGFzcw==
  username: YWRtaW4=
kind: Secret
metadata:
  creationTimestamp: "2024-08-01T09:17:36Z"
  name: db1-test
  namespace: monitoring
  resourceVersion: "503232"
  uid: 6c3fada7-f913-45f7-9c21-1395c0c22ec5
type: Opaque

# 由上述结果可知,加密字符串为:
#   password: cGFzcw==
#   username: YWRtaW4=

将加密字符串解密并写入文件

echo -n "YWRtaW4=" | base64 -d > /home/candidate/user.txt

echo -n "cGFzcw==" | base64 -d > /home/candidate/old-password.txt

创建 secret

kubectl create secret generic dev-mark \
  --from-literal=username=production-instance \
  --from-literal=password=aV7HR7nU3JLx \
  -n monitoring

创建 Pod yaml

kubectl run secret-pod --image=redis -n monitoring --dry-run=client -o yaml > secret-pod.yaml

编辑 Pod yaml

# vim secret-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: secret-pod
  name: secret-pod
  namespace: monitoring
spec:
  containers:
  - image: redis
    name: test-secret-container     # 修改名称
    resources: {}
    volumeMounts:                   # 添加挂载
    - name: secret-volume
      mountPath: "/etc/test-secret"
      readOnly: true
  dnsPolicy: ClusterFirst
  restartPolicy: Always
  volumes:                          # 添加挂载
  - name: secret-volume
    secret:
      secretName: dev-mark
status: {}

创建 Pod

kubectl create -f secret-pod.yaml

Last updated