k8s 中Pod,Deployment,ReplicaSet,Service關聯、關係分析

# 分析

pod
首先,我們先從最小的調度單位pod開始。
我的k8s集羣中目前有一個pod,它的name爲admin-mysql-1d29997-5db458497c-h6rrs

[root@k8s-master ~]# kubectl get pod admin-mysql-1d29997-5db458497c-h6rrs
NAME                                   READY   STATUS    RESTARTS   AGE
admin-mysql-1d29997-5db458497c-h6rrs   1/1     Running   0          5d23h

查看一下pod的詳細內容:

[root@k8s-master ~]# kubectl describe pod admin-mysql-1d29997-5db458497c-h6rrs
Name:               admin-mysql-1d29997-5db458497c-h6rrs
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               k8s-node1/192.168.0.247
Start Time:         Fri, 01 Nov 2019 10:57:47 +0800
Labels:             name=mysql
                    pod-template-hash=5db458497c
Annotations:        <none>
Status:             Running
IP:                 10.244.1.72
Controlled By:      ReplicaSet/admin-mysql-1d29997-5db458497c
Containers:
  mysql57-container:
    Container ID:   docker://7e68cae8d4e9840ed908965252ae7aff8281ca81954ab0b5d58e053f5371bb5d
    Image:          mysql57:5.7
    Image ID:       docker://sha256:f6509bac49801f48628167728aba66d64533aaa7d384e03b75a8fe4e6c0f6599
    Port:           3306/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 01 Nov 2019 10:57:48 +0800
    Ready:          True
    Restart Count:  0
    Environment:
      MYSQL_ROOT_PASSWORD:  welcome1
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-9rfpv (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-9rfpv:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-9rfpv
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none>
[root@k8s-master ~]# 

在這裏能看到嗎,pod是被一個名字爲admin-mysql-1d29997-5db458497c的ReplicaSet管理的,所以我們認爲,RS是比Pod高一級別的專門用來管理pod的組件。一個RS會管理一批pod。

Controlled By: ReplicaSet/admin-mysql-1d29997-5db458497c

ReplicateSet(rs)

接下來,我們就看看這個RS的詳細情況

[root@k8s-master ~]# kubectl describe rs admin-mysql-1d29997-5db458497c
Name:           admin-mysql-1d29997-5db458497c
Namespace:      default
Selector:       name=mysql,pod-template-hash=5db458497c
Labels:         name=mysql
                pod-template-hash=5db458497c
Annotations:    deployment.kubernetes.io/desired-replicas: 1
                deployment.kubernetes.io/max-replicas: 2
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/admin-mysql-1d29997
Replicas:       1 current / 1 desired
Pods Status:    1 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  name=mysql
           pod-template-hash=5db458497c
  Containers:
   mysql57-container:
    Image:      mysql57:5.7
    Port:       3306/TCP
    Host Port:  0/TCP
    Environment:
      MYSQL_ROOT_PASSWORD:  welcome1
    Mounts:                 <none>
  Volumes:                  <none>
Events:                     <none>
[root@k8s-master ~]# 

抓住關鍵信息
Controlled By: Deployment/admin-mysql-1d29997

這個RS被名字爲admin-mysql-1d29997的Deployment控制,這樣看,Deployment是比RS高一級別用於管理RS的組件。

在RS級別上發生的事件,均是對pod的操作,創建pod,刪除pod

Deployment
接下來,我們來看看Delpoyment

[root@k8s-master ~]# kubectl describe deploy admin-mysql-1d29997
Name:                   admin-mysql-1d29997
Namespace:              default
CreationTimestamp:      Fri, 01 Nov 2019 10:57:46 +0800
Labels:                 name=mysql
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               name=mysql
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  name=mysql
  Containers:
   mysql57-container:
    Image:      mysql57:5.7
    Port:       3306/TCP
    Host Port:  0/TCP
    Environment:
      MYSQL_ROOT_PASSWORD:  welcome1
    Mounts:                 <none>
  Volumes:                  <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   admin-mysql-1d29997-5db458497c (1/1 replicas created)
Events:          <none>
[root@k8s-master ~]# 

可以看出,在deployment級別上,不再受其他組件的控制,而他的狀態的轉變是作爲API被調用而產生的。我們看到,在deployment級別上發生的事件一般是創建服務、滾動升級一個服務,或者是操作RS伸縮Pod集羣。

service
最後,到這裏也很明白量,service其實是在這一整套基礎之上提供給外部的穩定的服務。

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