在这份CKAD考试实操指南中,我将为你详细介绍如何利用CKAD-exercises项目和知十平台进行CKAD考试的准备和复习。通过CKAD-exercises提供的练习题,你可以在知十平台的云原生环境中进行实践和模拟。在这个过程中,你将熟悉Kubernetes的各种操作和场景,并在实践中加深对知识的理解。这种结合实践和理论的学习方式将为你在考试中取得优异成绩提供强有力的支持。
首先,打开浏览器,访问知十平台。在页面右上角点击“登录”,然后使用微信扫码登录即可。
在未登录状态下,每个环境只能体验15分钟,每天有5次机会使用。登录后,每个环境可用时长为1小时,每天登录也有5次的使用机会。
当选择好要进入环境后,通常只需要等待约一分钟左右,就能进入环境中。在等待期间,你可以浏览环境说明文档,了解该环境包含哪些组件及版本。
现在开始第七个主题----状态持久性的实操
这里使用到官网的链接及访问路径如下:
Kubernetes Documentation > Concepts > Storage > Volumes
https://kubernetes.io/docs/concepts/storage/volumes/
Kubernetes Documentation > Concepts > Storage > Persistent Volumes
https://kubernetes.io/docs/concepts/storage/persistent-volumes/
1、Create busybox pod with two containers, each one will have the image busybox and will run the'sleep 3600'
command. Make both containers mount an emptyDir at'/etc/foo'.
Connect to the second busybox, write the first column of '/etc/passwd'
file to '/etc/foo/passwd'.
Connect to the first busybox and write'/etc/foo/passwd'
file to standard output. Delete pod.
译:用两个容器创建busybox pod,每个容器都使用busybox镜像,并运行'sleep 3600'
命令。让两个容器都在'/etc/foo'
挂载一个emptyDir。连接到第二个busybox容器,将“/etc/passwd”
文件的第一列写入“/etc/foo/passwd”
。连接到第一个busybox容器并将'/etc/foo/passwd'
文件写入标准输出。删除pod。
# 首先创建一个pod的模板文件:
# kubectl: 这是Kubernetes命令行工具,用于与Kubernetes集群进行交互和管理。
# run busybox: 这部分命令表示要创建一个名为"busybox"的Pod。Pod是Kubernetes中的最小部署单元,通常包含一个或多个容器。
# --image=busybox: 这是一个选项,用于指定要在Pod中使用的容器镜像。在这里,使用的是"busybox"容器镜像,这是一个轻量级Linux发行版。
# --restart=Never: 这是另一个选项,用于指定Pod的重启策略。"Never"表示一旦Pod终止,就不会自动重新启动。这意味着这个Pod只会运行一次并在完成后停止。
# -o yaml: 这是一个选项,用于指示kubectl以YAML格式输出Pod的配置。
# --dry-run=client: 这是另一个选项,用于执行模拟运行而不实际创建Pod。这样,你可以在创建之前查看生成的YAML配置。
# -- /bin/sh -c 'sleep 3600': 这部分命令是容器的入口命令。它告诉容器在启动时执行/bin/sh -c 'sleep 3600'命令,即在容器内部运行sleep 3600,使容器保持运行状态3600秒(1小时)。
# > pod.yaml: 这部分命令将kubectl命令的输出重定向到一个名为"pod.yaml"的文件中,以保存生成的Pod配置。
kubectl run busybox --image=busybox --restart=Never -o yaml --dry-run=client -- /bin/sh -c 'sleep 3600' > pod.yaml
vi pod.yaml
# 复制粘贴容器定义,并键入末尾有注释的行:
---
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: busybox
name: busybox
spec:
dnsPolicy: ClusterFirst
restartPolicy: Never
containers:
- args:
- /bin/sh
- -c
- sleep 3600
image: busybox
imagePullPolicy: IfNotPresent
name: busybox
resources: {}
volumeMounts: #
- name: myvolume #
mountPath: /etc/foo #
- args:
- /bin/sh
- -c
- sleep 3600
image: busybox
name: busybox2 # 第二个容器的名称,必须与第一个容器的名称不同
volumeMounts: #
- name: myvolume #
mountPath: /etc/foo #
volumes: #
- name: myvolume #
emptyDir: {} #
---
# 创建pod
kubectl apply -f pod.yaml
# 连接到第二个容器:
kubectl exec -it busybox -c busybox2 -- /bin/sh
# 将/etc/passwd第一列写入/etc/foo/passwd
cat /etc/passwd | cut -f 1 -d ':' > /etc/foo/passwd
# 确认写入成功
cat /etc/foo/passwd
exit
# 连接到第一个容器:
kubectl exec -it busybox -c busybox -- /bin/sh
# 确认已经挂载
mount | grep foo
# 查看内容
cat /etc/foo/passwd
exit
#删除pod
kubectl delete po busybox
知识点:
localhost
相互通信,也可以使用相同的网络端口。2、Create a PersistentVolume of 5Gi, called 'myvolume'. Make it have accessMode of 'ReadWriteOnce' and 'ReadWriteMany', storageClassName 'normal', mounted on hostPath '/etc/foo'. Save it on pv.yaml, add it to the cluster. Show the PersistentVolumes that exist on the cluster
译:创建一个5 Gi的PersistentVolume,称为“myvolume”。使其accessMode为“ReadWriteOnce”和“ReadWriteMany”,storageClassName为“normal”,挂载在hostPath“/etc/foo”上。将其保存在pv.yaml中,并将其添加到集群中。展示群集上存在的PersistentVolumes。
# 写入pv.yaml文件
vi pv.yaml
---
kind: PersistentVolume
apiVersion: v1
metadata:
name: myvolume # PV的名称,供后续引用
spec:
storageClassName: normal # 指定StorageClass的名称
capacity:
storage: 5Gi # PV的存储容量为5GB
accessModes:
- ReadWriteOnce # 支持单个Pod以读写模式挂载
- ReadWriteMany # 支持多个Pod以读写模式挂载
hostPath:
path: /etc/foo # PV使用本地主机路径作为存储
---
# 创建pv
kubectl create -f pv.yaml
# 获取pv状态
kubectl get pv
知识点:
Immediate
或 WaitForFirstConsumer
。Immediate 表示PV将立即绑定到PVC,而 WaitForFirstConsumer 表示PV将等待第一个Pod使用PVC时才绑定。3、Create a PersistentVolumeClaim for this storage class, called 'mypvc', a request of 4Gi and an accessMode of ReadWriteOnce, with the storageClassName of normal, and save it on pvc.yaml. Create it on the cluster. Show the PersistentVolumeClaims of the cluster. Show the PersistentVolumes of the cluster
译:为这个存储类创建一个PersistentVolumeClaim,名为'mypvc',请求为4Gi,accessMode为ReadWriteOnce,storageClassName为normal,并将其保存在pvc. yaml上。在集群上创建它。展示集群的PersistentVolumeClaims。展示群集的PersistentVolumes
vi pvc.yaml
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mypvc # PVC的名称,供后续引用
spec:
storageClassName: normal # 指定使用的StorageClass的名称
accessModes:
- ReadWriteOnce #请求以单个Pod以读写模式挂载
resources:
requests:
storage: 4Gi # 请求4GB的存储容量
---
#创建PVC
kubectl create -f pvc.yaml
# 查看pvc
kubectl get pvc
# 查看pv
kubectl get pv
知识点:
4、Create a busybox pod with command 'sleep 3600', save it on pod.yaml. Mount the PersistentVolumeClaim to '/etc/foo'. Connect to the 'busybox' pod, and copy the '/etc/passwd' file to '/etc/foo/passwd'
译:使用命令'sleep 3600'创建一个busybox pod,将其保存在pod. yaml中。挂载pvc到/etc/foo目录,连接到'busybox' pod,并将'/etc/passwd'文件复制到'/etc/foo/passwd'
# kubectl: 这是Kubernetes命令行工具,用于与Kubernetes集群进行交互和管理。
# run busybox: 这部分命令表示要创建一个名为"busybox"的Pod。
# --image=busybox: 指定了要在Pod中使用的容器镜像,这里使用的是"busybox"容器镜像,它是一个轻量级的Linux发行版。
# --restart=Never: 指定Pod的重启策略。"Never"表示一旦Pod终止,就不会自动重新启动。这意味着这个Pod只会运行一次,运行一个sleep命令,然后在1小时后自动终止。
# -o yaml: 指示kubectl以YAML格式输出Pod的配置。
# --dry-run=client: 它告诉kubectl执行模拟运行而不实际创建Pod。这样,可以在创建之前查看生成的YAML配置,以便检查是否符合预期。
# /bin/sh -c 'sleep 3600': 这部分命令是容器的入口命令。它告诉容器在启动时执行/bin/sh -c 'sleep 3600'命令,即在容器内部运行sleep 3600,使容器保持运行状态3600秒(1小时)。
# > pod.yaml: 这部分命令将kubectl命令的输出重定向到一个名为"pod.yaml"的文件中,以保存生成的Pod配置。
kubectl run busybox --image=busybox --restart=Never -o yaml --dry-run=client -- /bin/sh -c 'sleep 3600' > pod.yaml
vi pod.yaml
#添加以注释结尾的行:
---
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: busybox
name: busybox
spec:
containers:
- args:
- /bin/sh
- -c
- sleep 3600
image: busybox
imagePullPolicy: IfNotPresent
name: busybox
resources: {}
volumeMounts: #
- name: myvolume #
mountPath: /etc/foo #
dnsPolicy: ClusterFirst
restartPolicy: Never
volumes: #
- name: myvolume #
persistentVolumeClaim: #
claimName: mypvc #
status: {}
---
# 创建pod:
kubectl create -f pod.yaml
# 连接到pod并将'/etc/passwd'复制到'/etc/foo/passwd':
kubectl exec busybox -it -- cp /etc/passwd /etc/foo/passwd
5、Create a second pod which is identical with the one you just created (you can easily do it by changing the 'name' property on pod.yaml). Connect to it and verify that '/etc/foo' contains the 'passwd' file. Delete pods to cleanup. Note: If you can't see the file from the second pod, can you figure out why? What would you do to fix that?
译:创建第二个pod,它与刚才创建的pod相同(可以通过更改pod.yaml上的'name'属性轻松完成)。连接到它并验证'/etc/foo'包含'passwd'文件。删除要清理的pod。注意:如果您看不到第二个pod中的文件,您能找出原因吗?你会怎么做来弥补
# 创建第二个pod,名为busybox2:
vim pod.yaml
# 修改 'metadata.name: busybox' 为 'metadata.name: busybox2'
kubectl create -f pod.yaml
# 查看/etc/foo文件夹
kubectl exec busybox2 -- ls /etc/foo
# 清理资源
kubectl delete po busybox busybox2
kubectl delete pvc mypvc
kubectl delete pv myvolume
# 如果文件在第二个pod上没有显示,但在第一个pod上显示,则很可能是在其他节点上调度的。
# 查看pod是否存在于同一个节点
kubectl get po busybox -o wide
kubectl get po busybox2 -o wide
# 如果它们位于不同的节点上,您将看不到该文件,因为我们使用了hostPath卷类型。如果需要访问多节点群集中的相同文件,则需要独立于特定节点的卷类型。每个云提供商都有很多不同的类型,一般的解决方案可能是使用NFS。
知识点:
常见的Volume类型:
6、Create a busybox pod with 'sleep 3600' as arguments. Copy '/etc/passwd' from the pod to your local folder
译:创建一个busybox pod,参数为'sleep 3600'。将Pod中的“/etc/passwd”复制到本地文件夹
# kubectl: 这是 Kubernetes 命令行工具,用于与 Kubernetes 集群进行交互和管理。
# run busybox: 这部分命令表示要创建一个名为 "busybox" 的 Pod。"busybox" 是一个轻量级的 Linux 发行版,通常用于调试和测试。
# --image=busybox: 这是一个选项,指定了要在 Pod 中使用的容器镜像,这里使用的是 "busybox" 容器镜像。
# --restart=Never: 这是另一个选项,它指定了 Pod 的重启策略。"Never" 表示一旦 Pod 终止,就不会自动重新启动。这意味着这个 Pod 只会运行一次,执行 sleep 3600 命令,然后在 3600 秒(1 小时)后自动终止。
# -- sleep 3600: 这部分命令是容器的入口命令。它告诉容器在启动时执行 sleep 3600 命令,即在容器内部运行 sleep 命令以使容器保持运行状态 3600 秒(1 小时)。
kubectl run busybox --image=busybox --restart=Never -- sleep 3600
# kubectl: 这是 Kubernetes 命令行工具,用于与 Kubernetes 集群进行交互和管理。
# cp: 这是 kubectl 命令的子命令,用于复制文件到或从Pod中。
# busybox:/etc/passwd: 这是指定源文件的部分,表示要从名为 "busybox" 的 Pod 中的 /etc/passwd 文件进行复制。
# busybox 是目标 Pod 的名称。
# :/etc/passwd 是目标文件的路径。这表示要从目标 Pod 的 /etc/passwd 文件复制数据。
# ./passwd: 这是指定目标文件的部分,表示复制的数据将保存为当前工作目录下的 passwd 文件。
kubectl cp busybox:/etc/passwd ./passwd
cat passwd
知识点:
kubectl cp
命令的基本语法如下:
kubectl cp /:
kubectl cp /:
注意:注册CKA,CKAD或CKS认证考试的考生将有 2 次机会(每次注册的考试)到 Killer.sh 参加模拟考试。
预约方式:
CKAD考试实操指南(一)--- 登顶CKAD:征服考试的完美蓝图
CKAD考试实操指南(二)--- 深入核心:探秘Kubernetes核心实操秘技
CKAD考试实操指南(三)--- 舞动容器:多容器Pod实践指南
CKAD考试实操指南(四)--- 优雅设计:掌握Pod设计技巧
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。