简体   繁体   English

仅从一个statefulset的一个pod中选择服务

[英]Service selection from only one pod of one statefulset

It is possible to create a service that only points to a pod, created by a statefulset? 是否可以创建仅指向由状态集创建的pod的服务?

The solutions that make me would be: 让我成为的解决方案是:

  • Put as a provider on behalf of the pod. 作为提供者代表pod。
  • Dynamic labels with the name of the pod. 带有pod名称的动态标签。

As per Kubernetes 1.9 you can use: statefulset.kubernetes.io/pod-name 根据Kubernetes 1.9,您可以使用:statefulset.kubernetes.io/pod-name

From the documentation: 从文档:

"When the StatefulSet controller creates a Pod, it adds a label, statefulset.kubernetes.io/pod-name, that is set to the name of the Pod. This label allows you to attach a Service to a specific Pod in the StatefulSet." “当StatefulSet控制器创建Pod时,它会添加一个标签statefulset.kubernetes.io/pod-name,该标签设置为Pod的名称。此标签允许您将服务附加到StatefulSet中的特定Pod。 “

https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-name-label https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-name-label

You can use the ordinal value of statefulset pod to label a pod. 您可以使用statefulset pod的序数值来标记pod。

I would use kubectl in initContainer to label the pods from within the pods created by statefulset and use that label in service selector spec. 我会在initContainer中使用kubectl来标记由statefulset创建的pod中的pod,并在服务选择器规范中使用该标签。

example init container: 示例init容器:

initContainers:
  - name: set-label
    image: lachlanevenson/k8s-kubectl:v1.8.5
    command:
      - sh
      - -c
      - '/usr/local/bin/kubectl label pod $POD_NAME select-id=${HOSTNAME##*-} --server=https://kubernetes.default --token=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) --certificate-authority=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt -n $POD_NAMESPACE'
    env:
      - name: POD_NAMESPACE
        valueFrom:
          fieldRef:
            fieldPath: metadata.namespace
      - name: POD_NAME
        valueFrom:
          fieldRef:
            fieldPath: metadata.name

example service selector: 示例服务选择器:

selector:
  app: my-app
  select-id: <0|1|2 ordinal value>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM