核心概念-pod
任务:Create a new pod with the name redis and the image redis123.
Utilize a pod-definition YAML file. Please note that the image name redis123 is intentionally incorrect. Do NOT correct the image at this stage; you will address this in the subsequent task.
答案: kubectl run redis --image=redis123 --dry-run=client -o yaml > redis-definition.yaml

任务:Now change the image on this pod to redis.(原先故意选错的镜像为redis123)
Once done, the pod should be in a running state.
答案:
Use the kubectl edit command to update the image of the pod to redis.
kubectl edit pod redis
If you used a pod definition file then update the image from redis123 to redis in the definition file via Vi or Nano editor and then run kubectl apply command to update the image :
kubectl apply -f redis-definition.yaml
任务:
Create a ReplicaSet using the replicaset-definition-1.yaml file located at /root/.
There is an issue with the file, so try to fix it.
答案:
Run the command: You can check for apiVersion of replicaset by commandkubectl api-resources | grep replicaset
kubectl explain replicaset | grep VERSION and correct the apiVersion for ReplicaSet.
Then run the command: kubectl create -f /root/replicaset-definition-1.yaml
任务:
Fix the issue in the replicaset-definition-2.yaml file and create a ReplicaSet using it.
This file is located at /root/.
答案:
Then run: kubectl apply -f /root/replicaset-definition-2.yaml
概念:

DESIRED (4): The number of Pods you want in the ReplicaSet — in this case, 4 Pods.
CURRENT (4): The number of Pods currently managed by the ReplicaSet — also 4, meaning it's fully scaled to your desired count.
Node 是集群中的一台机器(物理或虚拟),是运行 Pods 的“房子”。
ReplicaSet 是一个控制器,用来确保一定数量的 Pods(在你的例子中是 4 个)在集群中运行。
所以,Pods 运行在 Node 上,而 ReplicaSet 管理这些 Pods。你可以把 ReplicaSet 想成一个“调度员”,它确保有正确数量的 Pods 在 Nodes 上。
任务:
How many PODs are READY in the new-replica-set?
答案:
The kubectl get rs command shows the ReplicaSet info, but it doesn't display the READY status for individual Pods. To see how many Pods are READY, run:
kubectl get pods -l name=busybox-pod
This will list all Pods with the label name=busybox-pod and show their READY status in the READY column
任务:
Create a ReplicaSet using the replicaset-definition-1.yaml file located at /root/.
There is an issue with the file, so try to fix it.
答案:
Run the command: You can check for apiVersion of replicaset by command
kubectl api-resources | grep replicaset
kubectl explain replicaset | grep VERSION and correct the apiVersion for ReplicaSet.
Then run the command: kubectl create -f /root/replicaset-definition-1.yaml
任务:
Scale the ReplicaSet to 5 PODs.
Use kubectl scale command or edit the replicaset using kubectl edit replicaset.
答案:
Run the command: kubectl edit replicaset new-replica-set, modify the replicas and then save the file OR run:kubectl scale rs new-replica-set --replicas=5 to scale up to 5 PODs.
概念:
简单来说:
Deployment 管理应用的部署和版本升级。
ReplicaSet 保持一定数量的 Pod 副本。
Pod 是实际运行的容器实例。
它们的关系:Deployment 创建并管理 ReplicaSet,ReplicaSet 负责维护一定数量的 Pod。
你可以直接用 ReplicaSet 的 YAML 文件来管理它,但在实际操作中,推荐使用 Deployment,因为它提供了更高级的功能,比如滚动升级、版本控制和更简洁的管理界面。Deployment 内部会自动创建和管理 ReplicaSet,简化了操作流程。
总结:
直接用 ReplicaSet YAML 管理(可以,但不推荐)
更常用的是用 Deployment YAML 管理(推荐)
概念:
kubectl apply -f
在 kubectl 命令中,-f 代表 "file"(文件),用来指定你要应用的配置文件。没有 -f,kubectl 不知道你要用哪个文件中的定义来创建或更新资源。简单来说,-f 就像告诉 kubectl:“嘿,这个是我想用的配置文件!”

kubectl create -f 和 kubectl apply -f 都用来创建资源,但有细微差别:
kubectl create -f:只会创建资源,如果资源已存在,会报错。适合第一次创建。
kubectl apply -f:会根据配置文件的内容,创建或更新资源,适合后续修改和维护。
总结:create 是“创建新资源”,apply 是“创建或更新资源”。在你的场景中,apply 更灵活!

任务:
Run the command to get exact the number of pods in the research namespace
答案:
kubectl -n research get pods --no-headers | wc -l
kubectl -n research get pods --no-headers:列出research命名空间中的所有Pod,但不显示标题行。
|:管道,将前一个命令的输出传递给下一个命令。
wc -l:统计传入内容的行数,也就是Pod的数量。

任务:
Create a POD in the finance namespace.
答案:
kubectl run redis --image=redis -n finance
任务:
找出含有blue的pod的namespace
答案:
kubectl get pods --all-namespaces | grep blue
任务:
What DNS name should the Blue application use to access the database db-service in the dev namespace?
You can try it in the web application UI. Use port 6379.
答案:
For your question, the DNS name to access db-service in the dev namespace follows this pattern:
<service-name>.<namespace>.svc.cluster.local
So, for db-service in dev, it becomes:
db-service.dev.svc.cluster.local
This allows your Blue app to connect to the database seamlessly!
概念:
Service 是一种抽象,用来定义一组 Pod 的访问策略,就像一个稳定的入口点或负载均衡器。而 ReplicaSet 是一种控制器,负责确保一定数量的 Pod 副本一直在运行,主要用来管理 Pod 的数量。
所以,ReplicaSet 不是一种 Service,它们的作用不同:一个管理 Pod,另一个负责对外暴露和负载均衡。

任务:
Create a service named redis-service to expose the existing redis pod within the cluster on port 6379.
Service: redis-service
Port: 6379
Type: ClusterIP
答案:
kubectl expose pod redis --port=6379 --name=redis-service --type=ClusterIP
任务:
Create a deployment named webapp using the image kodekloud/webapp-color with 3 replicas.
答案:
kubectl create deployment webapp --image=kodekloud/webapp-color --replicas=3
写法:

任务:

答案:
kubectl run httpd --image=httpd:alpine --port=80 --expose

概念
kubectl api-resources这个命令可以帮你列出集群中所有可用的资源类型,比如pods、services、deployments等


任务:
Use kubectl explain to explore the spec field of a Pod. What is the TYPE of the containers field?
答案:
kubectl explain pod.spec.containers

再深入解释下有哪些字段这个怎么探索,要一个个字段深入下去看
Let's explore the Deployment resource.
What is the TYPE of the replicas field?
如果要递归的去看这个字段下的所有字段:

