kubernetes中Deployment和replicaset關係剖析

前言:

本片文章主要介紹了Deployment和Replicaset之間的關係以及如何產生Pod

#vim nginx-deploy.yaml   #先給出Deployment的YAML文件實例;

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2021-01-11T02:54:57Z"
  generation: 1
  labels:
    app: nginx
  name: nginx
  namespace: default
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.15.2
        imagePullPolicy: IfNotPresent
        name: nginx
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30

kubectl create -f nginx-deploy.yaml   #生成Deployment

#確定deployment

[root@k8s-master01 ~]# kubectl get deployment nginx -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 3/3 3 3 8m38s nginx nginx:1.15.2 app=nginx
[root@k8s-master01 ~]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-66bbc9fdc5-llhnn 1/1 Running 0 8m55s 172.18.195.60 k8s-master03 <none> <none>
nginx-66bbc9fdc5-sdhnh 1/1 Running 0 8m55s 172.25.244.203 k8s-master01 <none> <none>
nginx-66bbc9fdc5-x4x67 1/1 Running 0 8m55s 172.27.14.230 k8s-node02 <none> <none>

對生成的deployment的nginx進行日誌分析,確定在deployment產生replica set的信息

#kubectl describe deploy nginx

..........
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  10m   deployment-controller  Scaled up replica set nginx-66bbc9fdc5 to 3

 

可以看出在message對應的信息中出現了一個Replica set,通過deployment-controller控制器創建一個replica set名爲nginx-66bbc9fdc5

#確認Replica Set

[root@k8s-master01 ~]# kubectl get rs  nginx-66bbc9fdc5
NAME               DESIRED   CURRENT   READY   AGE
nginx-66bbc9fdc5   3         3         3       29m
[root@k8s-master01 ~]#kubectl describe rs nginx-66bbc9fdc5
Name:           nginx-66bbc9fdc5
Namespace:      default
Selector:       app=nginx,pod-template-hash=66bbc9fdc5
Labels:         app=nginx
                pod-template-hash=66bbc9fdc5
Annotations:    deployment.kubernetes.io/desired-replicas: 3
                deployment.kubernetes.io/max-replicas: 4
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/nginx
Replicas:       3 current / 3 desired
Pods Status:    3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
           pod-template-hash=66bbc9fdc5
  Containers:
   nginx:
    Image:        nginx:1.15.2
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  30m   replicaset-controller  Created pod: nginx-66bbc9fdc5-x4x67
  Normal  SuccessfulCreate  30m   replicaset-controller  Created pod: nginx-66bbc9fdc5-sdhnh
  Normal  SuccessfulCreate  30m   replicaset-controller  Created pod: nginx-66bbc9fdc5-llhnn

可以清晰的看到3個pod實例是通過Replica set生成的;

#確認Pod

語法格式:kubectl describe pod nginx-66bbc9fdc5-x4x67


[root@k8s-master01 ~]# for i in `kubectl get pod | awk 'NR>1{print $1}'`;do echo $i;kubectl describe pod $i | tail -n 6;done
nginx-66bbc9fdc5-llhnn
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  42m   default-scheduler  Successfully assigned default/nginx-66bbc9fdc5-llhnn to k8s-master03
  Normal  Pulled     42m   kubelet            Container image "nginx:1.15.2" already present on machine
  Normal  Created    42m   kubelet            Created container nginx
  Normal  Started    42m   kubelet            Started container nginx
nginx-66bbc9fdc5-sdhnh
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  42m   default-scheduler  Successfully assigned default/nginx-66bbc9fdc5-sdhnh to k8s-master01
  Normal  Pulled     42m   kubelet            Container image "nginx:1.15.2" already present on machine
  Normal  Created    42m   kubelet            Created container nginx
  Normal  Started    42m   kubelet            Started container nginx
nginx-66bbc9fdc5-x4x67
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  42m   default-scheduler  Successfully assigned default/nginx-66bbc9fdc5-x4x67 to k8s-node02
  Normal  Pulled     42m   kubelet            Container image "nginx:1.15.2" already present on machine
  Normal  Created    42m   kubelet            Created container nginx
  Normal  Started    42m   kubelet            Started container nginx

 

總結:

從上面一步一步的剖析,我們可以清晰的看到Pod的產生過程:配置完deployment的yaml文件之後:

1、通過kubectl create 創建一個deployment,那麼此時就會調用deployment-controller(deployment控制器)創建一個replica set

2、replica set調用replicaset-controller創建pod

3、Pod創建完之後就會由啓用資源調度程序,pod分配對應的node節點,由kubelet管理pod

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章