大网数据广告活动
服务器运维问答

What is spec - selector - matchLabels used for while creating a deployment?

· 52CE 我爱测

From Kubernetes documentation : The selector field defines how the Deployment finds which Pods to manage. But, when creating deployment, I already specify the pod template as part of the deployment. So, why will I need the select…

问题原文

原帖链接 · Paddy · CC BY-SA 4.0

From Kubernetes documentation:

The selector field defines how the Deployment finds which Pods to manage.

But, when creating deployment, I already specify the pod template as part of the deployment. So, why will I need the selectors as well?

Is it supposed to be used like services, where pods are already being started separately, but later brought under the umbrella of Deployment to be managed together?

已采纳解答

原帖链接 · alexander.polomodov · CC BY-SA 4.0

Answer for this question we can find in section Deployments from kubernetes.io

So, why will I need the selectors as well?

Quotes below from documentation for k8s v 1.14

.spec.selector is an required field that specifies a label selector for the Pods targeted by this deployment.

.spec.selector must match .spec.template.metadata.labels, or it will be rejected by the API.

In API version apps/v1, .spec.selector and .metadata.labels do not default to .spec.template.metadata.labels if not set. So they must be set explicitly. Also note that .spec.selector is immutable after creation of the Deployment in apps/v1.

A Deployment may terminate Pods whose labels match the selector if their template is different from .spec.template or if the total number of such Pods exceeds .spec.replicas. It brings up new Pods with .spec.template if the number of Pods is less than the desired number.

Pods are already being started separately, but later brought under the umbrella of Deployment to be managed together?

Simply speaking, No

Note: You should not create other pods whose labels match this selector, either directly, by creating another Deployment, or by creating another controller such as a ReplicaSet or a ReplicationController. If you do so, the first Deployment thinks that it created these other pods. Kubernetes does not stop you from doing this. If you have multiple controllers that have overlapping selectors, the controllers will fight with each other and won’t behave correctly.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-06-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。