当前位置:网站首页>How to successfully pass the CKA exam?

How to successfully pass the CKA exam?

2022-08-01 11:33:00 Zhao Huabing

了解 CKA 考察的内容

Before starting to prepare for the exam must readCNCF 官方考试大纲,了解 CKA The main content of the test your,In the reference appendix do know,有的放矢,According to the exam outline to prepare and practice.According to the outline K8s 的版本进行更新,But each version of the exam content involved in little change,Here is when I was preparing for the examination of version(v1.22)The main content of the requirement:

  • 25% - Cluster Architecture, Installation & Configuration
  • 15% - Workloads & Scheduling
  • 20% - Services & Networking
  • 10% - Storage
  • 30% - Troubleshooting

Familiar with test software environment

CKA The test software environment is as follows,Ensure that the practice of using the same software before the exam environment,Familiar with test environment in order to advance:

  • 操作系统:Ubuntu 18.04
  • Shell:bash
  • 编辑器:vi
  • 命令行工具:kubectl jq tmux curl wget
  • 浏览器 chrome

YouTube 上有一个 Linux Recorded foundation CKA Test environment video,大家可以看一下,The test environment has a basic understanding of:https://www.youtube.com/watch?v=9UqkWcdy140

Suggestions in preparing for the examination practice and fully familiar with the following tools:

编辑器 vi

vi Is a very powerful editing software,Command is also very much,But we don't need to master all the command.了解如何在 vi The editor and the command mode switch between,And familiar with will be used in the test to a few vi The editor's common commands can be,包括删除、剪切、拷贝、粘贴、Scroll, etc.注意 vi 在粘贴 yaml When the automatic formatting treatment may not correct.可以通过 :set paste 取消 vi 的自动格式化.常用的 vi 命令:

  • 进入编辑模式 i
  • 进入命令模式 Esc
  • 储存后离开 vi :wq
  • Last line cursor movement G
  • 光标移动到第一行 gg
  • The cursor to the specified nG (n为行数)

vi Use method and the command of introduction to see this article:https://www.runoob.com/linux/linux-vim.html

Josn/yaml 处理 jq

在对 K8s crd 和 kubectl The command-line output during operation need to Json/Yaml Code snippets to operate,For example, intercept or modify the output in a particular field.Test environment with Json/Yaml 的命令行工具 jq.In practice the use of the familiar with the command,For example the following commands can be obtain pod 中的镜像名称:

$ k get pod busybox -ojson|jq '.spec.containers[0].image'
"busybox"

阅读这篇文章《My jq Cheatsheet》(https://medium.com/geekculture/my-jq-cheatsheet-34054df5b650),了解更多 jq 的使用方法.

终端复用器 tmux

When the exam can only open a terminal,But we may need to perform multiple tasks at the same time during the test,Or in multiple comparison between terminal view、复制粘贴.Can use the test environment with terminal reuse tools tmux 来打开多个终端.May use in the examination to the commonly used tmux 命令:

  • Ctrl+b %:划分左右两个窗格.
  • Ctrl+b “:划分上下两个窗格.
  • Ctrl+b :光标切换到其他窗格. 是指向要切换到的窗格的方向键,比如切换到下方窗格,就按方向键↓.

关于 tmux 的更多使用方法,可以参考 阮一峰 老师的 《Tmux 使用教程》.

Examination of some skills

CKA Test a total of two hours,时间是比较紧张的,There might be time enough of.The following techniques can be used to accelerate the speed of doing problems,In the test time to complete as many questions.

为常用的 kubectl 命令定义 alias

You can set according to their own habits alias,如下:

alias k=kubectl
alias kgp="k get pod"
alias kgd="k get deploy"
alias kgs="k get svc"
alias kgn="k get nodes"
alias kd="k describe"
alias kge="k get events --sort-by='.metadata.creationTimestamp' |tail -8"

使用 kubectl 的自动补全功能

source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc

使用 K8s Resource The acronym instead of full name

熟练使用常见 K8s Resource 名称的缩写:

Short name

Full name

cm

configmaps

ds

daemonsets

deploy

deployments

ep

endpoints

ev

events

hpa

horizontalpodautoscalers

ing

ingresses

limits

limitranges

ns

namespaces

no

nodes

pvc

persistentvolumeclaims

pv

persistentvolumes

po

pods

rs

replicasets

rc

replicationcontrollers

quota

resourcequotas

sa

serviceaccounts

svc

services

采用 dry run 来生成 yaml

Candidates will be required to create some K8s 资源,例如 pod,deployment,service 等等.From the beginning to write these resources yaml File not only time consuming,And we also hard to remember the entire structure of a resource.可以使用 dry run 来生成一个基础的 yaml 文件,And then based on the file modified,Finally, using the modified files to create the resources.

例如这道题:创建一个 nginx pod,将 request 的 memory 设置为 1M, CPU 设置为 500m

k run nginx --image=nginx --dry-run=client -oyaml > pod.yaml
vi pod.yaml //添加 resource limit 设置
k create -f pod.yaml

Due to the frequently used in the test to --dry-run=client -oyaml 选项来生成 k8s 对象的 yaml 文件,我们可以采用 export 来定义一个变量 do,To save the input time.

export do="--dry-run=client -o yaml"

定义 do 变量后,就可以像下面这样使用:

k run nginx --image=nginx $do > pod.yaml

快速删除 pod

CKA The exam sometimes need to delete pod,k8s The default adopt the way of grace to delete,这意味着 kubectl The command 'guild is hang wait for a long time,Related resources is cleared after back again.This time may be up to 10 多秒.CKA The test time is relatively nervous,As much as possible in order to reduce the waiting time when delete,We can adopt the way of mandatory delete quickly delete pod.

export now="--force --grace-period 0"

定义 now 变量后,Can like this fast to remove an pod:

k delete pod test $now

利用 kubectl command help View the create resource sample

kubectl command --help The output of the command provides many common example,I copy the example slightly modified can be used in the test.By using this command to save in k8s Online documentation to find out the dates of the search for an example.

例如 kubectl run --help The output of a large number of create pod 的示例:

kubectl run --help
Create and run a particular image in a pod.

Examples:
  # Start a nginx pod.
  kubectl run nginx --image=nginx

  # Start a hazelcast pod and let the container expose port 5701.
  kubectl run hazelcast --image=hazelcast/hazelcast --port=5701

  # Start a hazelcast pod and set environment variables "DNS_DOMAIN=cluster" and "POD_NAMESPACE=default" in the
container.
  kubectl run hazelcast --image=hazelcast/hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"

  # Start a hazelcast pod and set labels "app=hazelcast" and "env=prod" in the container.
  kubectl run hazelcast --image=hazelcast/hazelcast --labels="app=hazelcast,env=prod"

  # Dry run. Print the corresponding API objects without creating them.
  kubectl run nginx --image=nginx --dry-run=client

  # Start a nginx pod, but overload the spec with a partial set of values parsed from JSON.
  kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'

  # Start a busybox pod and keep it in the foreground, don't restart it if it exits.
  kubectl run -i -t busybox --image=busybox --restart=Never

  # Start the nginx pod using the default command, but use custom arguments (arg1 .. argN) for that command.
  kubectl run nginx --image=nginx -- <arg1> <arg2> ... <argN>

  # Start the nginx pod using a different command and custom arguments.
  kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>

采用 kubectl explain 来查看 resource 的定义

通过 kubectl command --help Command to create resources can view the sample,但 help In the command only shows the common options,Does not provide a complete resource definition.If we need to look at in the examination a k8s 资源的定义,A method to the in k8s Online documentation to search the resources API,但在 K8s Document search function is not very easy to use,You may need to click many times to find the correct link.Another, more convenient way is to use kubectl explain Command to view the resource definition.kubectl explain Has the advantage of hierarchical view,Such as the need to look at pod 中容器的 limit 如何定义,But don't remember clearly pod yaml 的结构层次,Can such queries:

k explain pod.spec //查看 pod 的 spec
k explain pod.spec.containers //进一步查看 pod spec 中 containers 部分的定义
k explain pod.spec.containers.resources //进一步查看 resources 部分的定义
k explain pod.spec.containers.resources.limits //进一步查看 limits 部分的定义

创建临时 Pod 来进行测试

Exams often allow students create a temporary pod To test certain features,For example, create a temporary busybox pod ,在该 pod 中通过 wget Command to test on a step expose 的某个 k8s service.可以采用 kubectl run 加上 --rm Option to create the pod,--rm Runs a specified command option after the pod Will be immediately deleted.The technique allows us to quickly create a can perform wget, curl Commands such as temporary pod,命令执行后 pod 会被自动删除掉,无需手动清理. The technique in the usual K8s Run the application in making a mistake is also useful to.

*  ~ kubectl -it  run busybox --rm --image=busybox -- sh
If you don't see a command prompt, try pressing enter.
/ # wget -O- 172.17.254.255

安装 k8s Some matters needing attention of cluster

Before the installation first sudo -i 命令切换到 root 用户.

We only need to know about the installation need related tools and general steps,Do not need to remember to install related command.When the exam open K8s 官网中的 Bootstrapping clusters with kubeadm 文档,Follow the steps in the document for installation can be.

安装 Docker

Docker There are more steps in the installation manual website,Those who didn't allow access to Docker 官网.It is recommended to use a key installation script to install Docker.Docker A key installation script address get.docker.com 很容易记住.

bash <(wget -O- get.docker.com)

注意需要设置 systemd 为 docker 的 cgroup driver,参见 https://kubernetes.io/docs/setup/production-environment/container-runtimes/#docker

初始化 master 节点

If a node has multiple network card,注意通过 --apiserver-advertise-address 参数设置 apiserver 的监听地址,The address should be and worker Nodes on the same LAN address.

如果使用了 flannel 插件,需要在 kubeadm 命令中加入 pod cidr 参数, kubeadm init --pod-network-cidr=10.244.0.0/16,cidr 和 https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml 中配置的 CIDR 一致.

安装 CNI 插件

采用 kubeadm After initial cluster,需要通过 kubectl apply -f <add-on.yaml> 安装 CNI addon,Or to join the cluster nodes will have been in NotReady 状态.When installing at ordinary times we will pass k8s Online documentation to navigate to an external CNI 网站上,找到该 addon 的 yaml 文件.In the exam is not allowed to visit CNI 的网站,在下面的 K8s In the document has installed CNI 插件的例子,Web addresses can be added to the browser favorites. https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/high-availability/#steps-for-the-first-control-plane-node

Collection of commonly used k8s 文档

When the exam can see k8s 在线文档,So you can in advance will test may be used in k8s 文档加入 chrome 收藏夹,Avoid the exam temporary search is a waste of time. You can need according to exercise judgment which collection K8s 文档,And according to the sorting folder,Below is my collection of documents:

Some useful document links:

  • kubectl 命令参考:https://kubernetes.io/docs/reference/kubectl/cheatsheet/
  • 使用 kubeadm 安装 K8s 集群 Kubernetes API:https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/
  • 设置 Docker:https://kubernetes.io/docs/setup/production-environment/container-runtimes/#docker
  • 安装 K8s CNI addon:https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/high-availability/#steps-for-the-first-control-plane-node
  • 升级 K8s Cluster: https://kubernetes.io/docs/tasks/administer-cluster/kubeadm/kubeadm-upgrade/
  • 备份 etcd :https://kubernetes.io/docs/tasks/administer-cluster/configure-upgrade-etcd/#snapshot-using-etcdctl-options
  • K8s Cluster 排错:https://kubernetes.io/docs/tasks/debug-application-cluster/debug-cluster/
  • Nginx ingress controller 安装:https://github.com/kubernetes/ingress-nginx/blob/main/docs/deploy/index.md

注意:The exam is not allowed to visit https://helm.sh/docs/,https://kubernetes.io/docs/,https://github.com/kubernetes/,https://kubernetes.io/blog/ Other than the document,So be careful not to click k8s In the document outside the chain,例如 cni addon 和 docker 网站的外链.

练习,练习,练习

CKA Students completed in time for K8s The designated management tasks,This requires that the examinee to understand K8s 的相关概念,And are very familiar with kubectl The command line related operations.而熟悉 kubectl The command line method is repeated practice.Github There are some good resources on,Can be reference to practice when preparing for the examination:

Suggest an exercise plan before the exam,According to the plan and stick to it to practice.I followed the plan is to exam the first three months of practice,Monday to Friday and half an hour every morning before going to work take time.Weekend time is flexible,Will pay on Saturday and Sunday2Hours practice.The longer you practice,对 kubectl Command line operation the more familiar,The more confident for the upcoming exam,The greater the likely smoothly pass the exam.

购买 CKA After the test will present two killer.sh 的模拟考试,The difficulty of mock exam slightly greater than the actual exam.In practice for a period of time after the above problem,Can attend the first mock exam;And then according to the result of simulation test to leak fill a vacancy,To analyze the wrong topic in the first test and strengthen practice,Then on the second mock exam.After two mock exam,And mastered the knowledge of all questions in the mock exam after,You basically is to test the content of the larger style,You can take the formal test.

According to the above method to prepare,我成功通过了 CKA 的考试.I wish everyone passed the exam!


原网站

版权声明
本文为[Zhao Huabing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/213/202208011105149361.html