udemy學(xué)習(xí)-k8s第二章核心概念

核心概念-pod
任務(wù):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

image.png

任務(wù):Now change the image on this pod to redis.(原先故意選錯(cuò)的鏡像為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

任務(wù):
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

任務(wù):
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

概念:


image.png

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 是集群中的一臺(tái)機(jī)器(物理或虛擬),是運(yùn)行 Pods 的“房子”。
ReplicaSet 是一個(gè)控制器,用來(lái)確保一定數(shù)量的 Pods(在你的例子中是 4 個(gè))在集群中運(yùn)行。
所以,Pods 運(yùn)行在 Node 上,而 ReplicaSet 管理這些 Pods。你可以把 ReplicaSet 想成一個(gè)“調(diào)度員”,它確保有正確數(shù)量的 Pods 在 Nodes 上。

任務(wù):
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

任務(wù):
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

任務(wù):
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.

概念:

簡(jiǎn)單來(lái)說(shuō):
Deployment 管理應(yīng)用的部署和版本升級(jí)。
ReplicaSet 保持一定數(shù)量的 Pod 副本。
Pod 是實(shí)際運(yùn)行的容器實(shí)例。
它們的關(guān)系:Deployment 創(chuàng)建并管理 ReplicaSet,ReplicaSet 負(fù)責(zé)維護(hù)一定數(shù)量的 Pod。
你可以直接用 ReplicaSet 的 YAML 文件來(lái)管理它,但在實(shí)際操作中,推薦使用 Deployment,因?yàn)樗峁┝烁呒?jí)的功能,比如滾動(dòng)升級(jí)、版本控制和更簡(jiǎn)潔的管理界面。Deployment 內(nèi)部會(huì)自動(dòng)創(chuàng)建和管理 ReplicaSet,簡(jiǎn)化了操作流程。

總結(jié):
直接用 ReplicaSet YAML 管理(可以,但不推薦)
更常用的是用 Deployment YAML 管理(推薦)

概念:

kubectl apply -f
在 kubectl 命令中,-f 代表 "file"(文件),用來(lái)指定你要應(yīng)用的配置文件。沒(méi)有 -f,kubectl 不知道你要用哪個(gè)文件中的定義來(lái)創(chuàng)建或更新資源。簡(jiǎn)單來(lái)說(shuō),-f 就像告訴 kubectl:“嘿,這個(gè)是我想用的配置文件!”

image.png

kubectl create -f 和 kubectl apply -f 都用來(lái)創(chuàng)建資源,但有細(xì)微差別:

kubectl create -f:只會(huì)創(chuàng)建資源,如果資源已存在,會(huì)報(bào)錯(cuò)。適合第一次創(chuàng)建。
kubectl apply -f:會(huì)根據(jù)配置文件的內(nèi)容,創(chuàng)建或更新資源,適合后續(xù)修改和維護(hù)。
總結(jié):create 是“創(chuàng)建新資源”,apply 是“創(chuàng)建或更新資源”。在你的場(chǎng)景中,apply 更靈活!

image.png

任務(wù):
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,但不顯示標(biāo)題行。
|:管道,將前一個(gè)命令的輸出傳遞給下一個(gè)命令。
wc -l:統(tǒng)計(jì)傳入內(nèi)容的行數(shù),也就是Pod的數(shù)量。

image.png

任務(wù):
Create a POD in the finance namespace.
答案:
kubectl run redis --image=redis -n finance

任務(wù):
找出含有blue的pod的namespace
答案:
kubectl get pods --all-namespaces | grep blue

任務(wù):
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 是一種抽象,用來(lái)定義一組 Pod 的訪(fǎng)問(wèn)策略,就像一個(gè)穩(wěn)定的入口點(diǎn)或負(fù)載均衡器。而 ReplicaSet 是一種控制器,負(fù)責(zé)確保一定數(shù)量的 Pod 副本一直在運(yùn)行,主要用來(lái)管理 Pod 的數(shù)量。
所以,ReplicaSet 不是一種 Service,它們的作用不同:一個(gè)管理 Pod,另一個(gè)負(fù)責(zé)對(duì)外暴露和負(fù)載均衡。

image.png

任務(wù):
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

任務(wù):
Create a deployment named webapp using the image kodekloud/webapp-color with 3 replicas.
答案:
kubectl create deployment webapp --image=kodekloud/webapp-color --replicas=3

寫(xiě)法:


image.png

任務(wù):

image.png

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

image.png

概念

kubectl api-resources這個(gè)命令可以幫你列出集群中所有可用的資源類(lèi)型,比如pods、services、deployments等


image.png

image.png

任務(wù):
Use kubectl explain to explore the spec field of a Pod. What is the TYPE of the containers field?
答案:
kubectl explain pod.spec.containers


image.png

再深入解釋下有哪些字段這個(gè)怎么探索,要一個(gè)個(gè)字段深入下去看
Let's explore the Deployment resource.
What is the TYPE of the replicas field?

如果要遞歸的去看這個(gè)字段下的所有字段:


image.png

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容