[CKS] Create/Read/Mount a Secret in K8S

最近准备花一周的时间准备CKS考试,在准备考试中发现有一个题目关于读取、创建以及挂载secret的题目。

​ 专栏其他文章:

What's the Secret

详细介绍参见这篇官方介绍: https://v1-28.docs.kubernetes.io/docs/concepts/configuration/secret/

Question 1

Get the username data from the curly Secret in the larry namespace and save it to /home/cloud_user/username.txt on the CLI server.

Then, get the password data and save it to /home/cloud_user/password.txt on the CLI server.

这个题目就是要求我们读取username和password在larry命令空间下的curly secret资源下。并且将结果存在username.txt和password.txt文件中

Practice

在这里需要明确的是secret存储的内容是base64编码的,所以这儿第一步需要将base64的编码数据给获取到,我们可以使用下面的命令打印出该secret的详细内容

shell 复制代码
kubectl get secret curly -n larry -o yaml

这里我们将会获得如下结果

这里我们就知道了以下内容:

  • password的base64编码为:MTIzNDUK
  • username的base64编码为:YWRtaW4K

由此,现在我们就需要对这个base64进行解码,并存储在username.txt和password.txt文件中,我们可以使用以下两条命令完成

shell 复制代码
echo MTIzNDUK | base64 --decode > password.txt
echo YWRtaW4K | base64 --decode > username.txt

Question 2

Create a new secret in the larry namespace called moe.

Provide fields called username and password and store the following username and password credentials in the Secret:

username dbuser

password A83MaeKoz

Create a Pod called secret-pod in the larry namespace.

Mount the new Secret to /etc/credentials in the Pod's container.

这个题目要求我们创建一个moe的secret,里面需要存储username为dbuser,password为A83MaeKoz,然后创建一个secret-pod,将这个secret进行挂载在pod的/etc/credentials

Practice

刚才我们说了,secret里面存储的是base64编码的内容,所以第一步我们需要将这个明文的username和password进行一个base64的编码,可以使用如下两个命令:

shell 复制代码
echo dbuser | base64
# 结果为: ZGJ1c2VyCg==
echo A83MaeKoz | base64
# 结果为: QTgzTWFlS296Cg==

然后创建moe.yaml, 文件内容如下:

yaml 复制代码
apiVersion: v1
data:
 password: QTgzTWFlS296Cg==
 username: ZGJ1c2VyCg==
kind: Secret
metadata:
 name: moe
 namespace: larry
type: Opaque

使用下面的命令创建secret:

shell 复制代码
kubectl create -f moe.yaml

最后,修改并创建/home/cloud_user/secret-pod.yml对应的pod,修改的内容如下:

yml 复制代码
apiVersion: v1
kind: Pod
metadata:
  name: secret-pod
  namespace: larry
spec:
  volumes:
    - name: secret-volume
      secret:
        secretName: moe
  containers:
  - name: busybox
    image: busybox:1.33.1
    command: ['sh', '-c', 'cat /etc/credentials/username; cat /etc/credentials/password; while true; do sleep 5; done']
    volumeMounts:
    - name: secret-volume
      readOnly: true
      mountPath: /etc/credentials

最后使用kubectl create -f secret-pod.yml创建pod

验证

shell 复制代码
kubectl logs secret-pod -n larry

结果如下:

相关推荐
ezreal_pan13 分钟前
docker设置镜像加速
运维·docker·容器
cainiao0806051 小时前
容器化改造避坑指南:传统应用迁移K8s的10个关键节点(2025实战复盘)
云原生·容器·kubernetes
憨堡包^—^4 小时前
Docker —— MySQL主从复制集群
mysql·docker·容器
Andy杨6 小时前
20250712-1-Kubernetes 监控与日志管理-K8s日志管理与维护_笔记
笔记·容器·kubernetes
蝸牛ちゃん7 小时前
云计算三大服务模式深度解析:IaaS、PaaS、SaaS
云原生·系统架构·云计算·软考高级·saas·paas·iaas
阿里云云原生7 小时前
GPU 降成本免运维,睿观 AI 助手选择函数计算
云原生·serverless
KKKingWei9 小时前
Kubernetes Dashboard UI 部署安装
云原生·容器·kubernetes
麟城Lincoln10 小时前
【Linux-云原生-笔记】系统引导修复(grub、bios、内核、系统初始化等)
linux·笔记·云原生·bios·grub·系统修复·系统引导
KubeSphere 云原生11 小时前
云原生周刊:镜像兼容性
云原生
蓝天居士11 小时前
docker常用命令集(2)
docker·容器