k8s(Kubernetes) 中 Pod,Deployment,ReplicaSet,Service 之间关系分析
看完 k8s 文档,好多概念似乎明白了,又似乎不明白,多个概念之间的关系也很混乱,不是很明白,不要紧,接下来,好好分析一下。
结论
您有可能在急着找答案搜到我这篇文章,不费话,结论就在开头,请看下图。
deploy 控制 RS,RS 控制 Pod,这一整套,向外提供稳定可靠的 Service。
分析
以下是分析的过程
首先,我们先从最小的调度单位 pod 开始。
我的 k8s 集群中目前有一个 pod,它的 name 为 mq-svc-5b96bf78d9-brpjw
[root@VM_0_17_centos ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mq-svc-5b96bf78d9-brpjw 1/1 Running 0 51m
来看一下它的详情
[root@VM_0_17_centos ~]# kubectl describe pod mq-svc-5b96bf78d9-brpjw
Name: mq-svc-5b96bf78d9-brpjw
Namespace: default
Node: 10.0.0.17/10.0.0.17
Start Time: Fri, 17 Aug 2018 17:24:44 +0800
Labels: pod-template-hash=1652693485
qcloud-app=mq-svc
Annotations: <none>
Status: Running
IP: 172.16.0.39
Controlled By: ReplicaSet/mq-svc-5b96bf78d9
Containers:
queue-mq:
Container ID: docker://700cdc55c111a413faaa8cabb8680009d2663701ccbe84b8a50ea6e6fe1d538c
Image: rabbitmq:management
Image ID: docker-pullable://rabbitmq@sha256:0b36ea1a8df9e53228aaeee277680de2cc97c7d675bc2d5dbe1cc9e3836a9d9f
Port: <none>
Host Port: <none>
State: Running
Started: Fri, 17 Aug 2018 17:24:49 +0800
Ready: True
Restart Count: 0
Limits:
cpu: 500m
memory: 1Gi
Requests:
cpu: 250m
memory: 256Mi
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-vzhz4 (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-vzhz4:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-vzhz4
Optional: false
QoS Class: Burstable
Node-Selectors: <none>
Tolerations: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 51m default-scheduler Successfully assigned mq-svc-5b96bf78d9-brpjw to 10.0.0.17
Normal SuccessfulMountVolume 51m kubelet, 10.0.0.17 MountVolume.SetUp succeeded for volume "default-token-vzhz4"
Normal Pulling 51m kubelet, 10.0.0.17 pulling image "rabbitmq:management"
Normal Pulled 51m kubelet, 10.0.0.17 Successfully pulled image "rabbitmq:management"
Normal Created 51m kubelet, 10.0.0.17 Created container
Normal Started 51m kubelet, 10.0.0.17 Started container
其实有一个敏感的信息,pod 是被一个名字为 mq-svc-5b96bf78d9 的 ReplicaSet 管理的,所以我们认为,RS 是比 Pod 高一级别的专门用来管理 pod 的组件。一个 RS 会管理一批 pod。
Controlled By: ReplicaSet/mq-svc-5b96bf78d9
而且在 pod 里面发生的实践也都是对容器的操作,比如拉取镜像、启动容器等等
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 51m default-scheduler Successfully assigned mq-svc-5b96bf78d9-brpjw to 10.0.0.17
Normal SuccessfulMountVolume 51m kubelet, 10.0.0.17 MountVolume.SetUp succeeded for volume "default-token-vzhz4"
Normal Pulling 51m kubelet, 10.0.0.17 pulling image "rabbitmq:management"
Normal Pulled 51m kubelet, 10.0.0.17 Successfully pulled image "rabbitmq:management"
Normal Created 51m kubelet, 10.0.0.17 Created container
Normal Started 51m kubelet, 10.0.0.17 Started container
接下来,我们就看看这个 RS 的详细情况
[root@VM_0_17_centos ~]# kubectl describe rs mq-svc-5b96bf78d9
Name: mq-svc-5b96bf78d9
Namespace: default
Selector: pod-template-hash=1652693485,qcloud-app=mq-svc
Labels: pod-template-hash=1652693485
qcloud-app=mq-svc
Annotations: deployment.changecourse=Updating
deployment.kubernetes.io/desired-replicas=1
deployment.kubernetes.io/max-replicas=2
deployment.kubernetes.io/revision=2
description=Service based on rabbitmq.
Controlled By: Deployment/mq-svc
Replicas: 1 current / 1 desired
Pods Status: 1 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: pod-template-hash=1652693485
qcloud-app=mq-svc
Containers:
queue-mq:
Image: rabbitmq:management
Port: <none>
Host Port: <none>
Limits:
cpu: 500m
memory: 1Gi
Requests:
cpu: 250m
memory: 256Mi
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 50m replicaset-controller Created pod: mq-svc-5b96bf78d9-r8n8t
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-l4zj2
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-m8tmv
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-m8tmv
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-r9wkj
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-8wzpq
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-d8gwc
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-d8gwc
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-8wzpq
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-l4zj2
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-r9wkj
Normal SuccessfulDelete 45m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-r8n8t
关键信息
Controlled By: Deployment/mq-svc
这个 RS 被名字为 mq-svc 的 Deployment 控制,这样看,Deployment 是比 RS 高一级别用于管理 RS 的组件。
在 RS 级别上发生的事件,均是对 pod 的操作,创建 pod,删除 pod
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 50m replicaset-controller Created pod: mq-svc-5b96bf78d9-r8n8t
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-l4zj2
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-m8tmv
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-m8tmv
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-r9wkj
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-8wzpq
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-d8gwc
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-d8gwc
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-8wzpq
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-l4zj2
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-r9wkj
Normal SuccessfulDelete 45m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-r8n8t
接下来,我们来看看 Delpoyment
[root@VM_0_17_centos ~]# kubectl describe deploy mq-svc
Name: mq-svc
Namespace: default
CreationTimestamp: Fri, 17 Aug 2018 17:21:13 +0800
Labels: qcloud-app=mq-svc
Annotations: deployment.changecourse=Updating
deployment.kubernetes.io/revision=2
description=Service based on rabbitmq.
Selector: qcloud-app=mq-svc
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 10
RollingUpdateStrategy: 0 max unavailable, 1 max surge
Pod Template:
Labels: qcloud-app=mq-svc
Containers:
queue-mq:
Image: rabbitmq:management
Port: <none>
Host Port: <none>
Limits:
cpu: 500m
memory: 1Gi
Requests:
cpu: 250m
memory: 256Mi
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: mq-svc-5b96bf78d9 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 58m deployment-controller Scaled up replica set mq-svc-5b96bf78d9 to 2
Normal ScalingReplicaSet 57m deployment-controller Scaled up replica set mq-svc-5b96bf78d9 to 3
Normal ScalingReplicaSet 57m deployment-controller Scaled up replica set mq-svc-5b96bf78d9 to 4
Normal ScalingReplicaSet 57m deployment-controller Scaled down replica set mq-svc-5b96bf78d9 to 3
Normal ScalingReplicaSet 57m deployment-controller Scaled up replica set mq-svc-5b96bf78d9 to 6
Normal ScalingReplicaSet 57m deployment-controller Scaled down replica set mq-svc-5b96bf78d9 to 4
Normal ScalingReplicaSet 56m deployment-controller Scaled down replica set mq-svc-5b96bf78d9 to 2
Normal ScalingReplicaSet 53m deployment-controller Scaled down replica set mq-svc-5b96bf78d9 to 1
可以看出,在 deployment 级别上,不再受其他组件的控制,而他的状态的转变是作为 API 被调用而产生的。我们看到,在 deployment 级别上发生的事件一般是创建服务、滚动升级一个服务,或者是操作 RS 伸缩 Pod 集群。
最后,到这里也很明白了,service 其实是在这一整套基础之上提供给外部的稳定的服务。
版权声明: 本文为 InfoQ 作者【ucsheep】的原创文章。
原文链接:【http://xie.infoq.cn/article/a713bfc505fbcfddbe364a40a】。未经作者许可,禁止转载。
ucsheep
譬如朝露 去日苦多 2021.03.22 加入
还未添加个人简介
评论