可以将K8s服务定向到没有标签的Pod吗?

huangapple go评论58阅读模式
英文:

Can I Target a K8s Service to a Pod Without Labels?

问题

可以在没有标签的情况下将K8s服务指向Pod吗?

也就是说,我有一个使用以下配置创建的K8s Pod。

apiVersion: v1
kind: Pod
metadata:
  name: nofrills-first-pod
spec:
  containers:
  - name: nofrills-container
    image: nofrills/to-deploy:0.0.1
    ports:
    - containerPort: 3000

我想将这个Pod暴露为K8s服务。通常,我会创建一个类似以下的服务配置:

apiVersion: v1
kind: Service
metadata:
  name: test-nofrills-service
spec:
  type: NodePort
  selector:
    ## ?????? no labels to target?
  ports:
    - protocol: TCP
      port: 3000
      targetPort: 3000
      nodePort: 32525

然而,由于Pod没有任何标签,我不知道如何告诉服务要使用哪个Pod。我想另一种问这个问题的方式是,“K8s选择器能否针对没有任何标签的对象?”

我明白在许多情况下,我可以轻松地向Pod添加标签,但我特别关注的是K8s选择器的能力。

英文:

Can I target a K8s service to a Pod without labels?

That is, I have a K8s Pod created with the following configuration.

apiVersion: v1
kind: Pod
metadata:
  name: nofrills-first-pod
spec:
  containers:
  - name: nofrills-container
	image: nofrills/to-deploy:0.0.1
	ports:
	- containerPort: 3000

I would like to expose this pod as a K8s service. Normally, I would do this by creating a Service configuration that looked something like this

apiVersion: v1
kind: Service
metadata:
  name: test-nofrills-service
spec:
  type: NodePort
  selector:
	## ?????? no labels to target?
  ports:
	- protocol: TCP
	  port: 3000
	  targetPort: 3000
	  nodePort: 32525

However, since the pod doesn't have any labels I don't know how to tell the Service which pod to use. I suppose another way of asking this questions is "Can a K8s selector target an object without any labels?"

I realize I could (in many scenarios) easily add labels to the Pod -- but I'm specifically interested in the abilities of K8s selectors here.

答案1

得分: 2

你可以定义一个不指定选择器的服务来匹配Pods。因为这个服务没有选择器,所以相应的EndpointSlice(以及传统的Endpoints)对象不会自动创建。

你可以手动添加一个EndpointSlice对象,将该服务映射到它运行的网络地址和端口。

英文:

You can define a Service without specifying a selector to match Pods. Because this Service has no selector, the corresponding EndpointSlice (and legacy Endpoints) objects are not created automatically.

You can map the Service to the network address and port where it's running, by adding an EndpointSlice object manually.

huangapple
  • 本文由 发表于 2023年3月31日 03:00:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定