二、k8s入門系列----deployment、replicaset
前面講了一個抽象概念pod,這裡先引出另外兩個抽象概念,deployment和replicaset(副本集),它們之間的關係如下:
deployment ==> replicaset ==> pod ==> container (實體)
先刪除前面建立的pod:
[root@ylserver10686071 ~]# kubectl delete pod app002 pod "app002" deleted [root@ylserver10686071 ~]#
編輯建立deployment的資源配置檔案:
[root@ylserver10686071 ~]# cat deployment.yml apiVersion: apps/v1 kind: Deployment metadata: name: app003 spec: replicas: 1 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: tomcat image: tomcat:8.0
應用配置檔案,建立deployment:
[root@ylserver10686071 ~]# kubectl apply -f deployment.yml deployment.apps/app003 created [root@ylserver10686071 ~]#
檢視該配置檔案建立的deployment:
[root@ylserver10686071 ~]# kubectl get deployment -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR app003 1/1 1 1 105s tomcat tomcat:8.0 app=web [root@ylserver10686071 ~]#
檢視該配置檔案建立的replicaset:
[root@ylserver10686071 ~]# kubectl get replicaset -o wide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR app003-646bdcbd54 1 1 1 2m36s tomcat tomcat:8.0 app=web,pod-template-hash=646bdcbd54 [root@ylserver10686071 ~]#
檢視該配置檔案建立的pod:
[root@ylserver10686071 ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES app003-646bdcbd54-tzlpv 1/1 Running 0 3m15s 10.233.75.51 ylserver10686071 <none> <none> [root@ylserver10686071 ~]#
檢視該配置檔案建立的Container:
[root@ylserver10686071 ~]# docker ps|grep app003 1a6708863ecd ef6a7c98d192 "catalina.sh run" 8 minutes ago Up 8 minutes k8s_tomcat_app003-646bdcbd54-tzlpv_default_92d5aa5e-f9be-4a39-9afc-7f9aa51e07af_0 f7123510af50 k8s.gcr.io/pause:3.3 "/pause" 8 minutes ago Up 8 minutes k8s_POD_app003-646bdcbd54-tzlpv_default_92d5aa5e-f9be-4a39-9afc-7f9aa51e07af_0 [root@ylserver10686071 ~]#
到這裡可以發現通過建立deployment資源後相繼建立了replicaset、pod資源,最近再建立了實體container,其實這也是k8s的設計思路:
Kubernetes最核心的功能就是編排,而編排操作都是依靠控制器物件來完成的,高階的控制器物件控制基礎的控制器物件,基礎的控制器物件再去控制Pod
,Pod
裡面再包容器。Kubernetes
專案裡API
物件的層級結構大概就是這樣。(此處引用知乎:https://zhuanlan.zhihu.com/p/191191906)
套用到這裡就是deployment控制器對replicaset控制器進行控制,replicaset(副本集)控制器用來控制副本數量,一個副本就是一個pod,pod再生成Container,也就是deployment ==> replicaset ==> pod ==> container (實體)
那上層控制器是如何控制下層控制器的呢,使用的就是標籤選擇器(Label-Selector),回頭看deployment資源配置檔案:
- kind 要建立的資源型別,這裡是Deployment
- replicas 生成的副本數
- selector 標籤選擇器,用來定位被控制的下層控制器
- template定義POD的模板
- labels POD資源的標籤,由key,value組成
通過kubectl describe命令可以看到已經建立資源的詳細資訊,可以看到deploymentapp003的標籤選擇器為 app=web:
[root@ylserver10686071 ~]# kubectl describe deployment app003 Name: app003 Namespace: default CreationTimestamp: Sun, 18 Jul 2021 11:06:45 +0800 Labels: <none> Annotations: deployment.kubernetes.io/revision: 1 Selector: app=web Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=web Containers: tomcat: Image: tomcat:8.0 Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none>
檢視replicaset資源的詳細資訊,可以看到Controlled By Deployment/app003,即由deployment控制器app003建立,標籤為app-web,標籤選擇器為app=web,pod-template-hash=646bdcbd54 :
[root@ylserver10686071 ~]# kubectl describe replicaset app003 Name: app003-646bdcbd54 Namespace: default Selector: app=web,pod-template-hash=646bdcbd54 Labels: app=web pod-template-hash=646bdcbd54 Annotations: deployment.kubernetes.io/desired-replicas: 2 deployment.kubernetes.io/max-replicas: 3 deployment.kubernetes.io/revision: 1 Controlled By: Deployment/app003 Replicas: 2 current / 2 desired Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: app=web pod-template-hash=646bdcbd54 Containers: tomcat: Image: tomcat:8.0 Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none>
檢視pod資源的詳細資訊: