二、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最核心的功能就是编排,而编排操作都是依靠控制器对象来完成的,高级的控制器对象控制基础的控制器对象,基础的控制器对象再去控制PodPod里面再包容器。Kubernetes项目里API对象的层级结构大概就是这样。(此处引用知乎:https://zhuanlan.zhihu.com/p/191191906

  套用到这里就是deployment控制器对replicaset控制器进行控制,replicaset(副本集)控制器用来控制副本数量,一个副本就是一个pod,pod再生成Container,也就是 deployment ==> replicaset ==> pod ==> container (实体)

  那上层控制器是如何控制下层控制器的呢,使用的就是标签选择器(Label-Selector),回头看deployment资源配置文件:

二、k8s入门系列----deployment、replicaset

 

  1. kind        要创建的资源类型,这里是Deployment
  2. replicas   生成的副本数
  3. selector  标签选择器,用来定位被控制的下层控制器
  4. template 定义POD的模板
  5. labels     POD资源的标签,由key,value组成

  通过kubectl  describe 命令可以看到已经创建资源的详细信息,可以看到deployment app003 的标签选择器为 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资源的详细信息:

 

上一篇:K8S--实战


下一篇:2021-07-26