简体   繁体   English

将流量路由到副本集的特定 Pod

[英]Route Traffic Into Specific Pod of a Replica Set

I need to route traffic (real-time audio/video) directly into specific container of pods.我需要将流量(实时音频/视频)直接路由到特定的 pod 容器中。 The number of pods should be scaled horizontally with a replica set. Pod 的数量应该使用副本集水平扩展。 My solution now is to create a StatefulSet with as many NodePort-type services as there are pods.我现在的解决方案是创建一个 StatefulSet,其中包含与 pod 一样多的 NodePort 类型服务。

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: foobar
  name: foobar-app
spec:
  serviceName: foobar
  replicas: 2
  selector:
    matchLabels:
      app: foobar
  template:
    metadata:
      labels:
        app: foobar
    spec:
      containers:
      - image: foobar:latest
        name: foobar
---
apiVersion: v1
kind: Service
metadata:
  name: foobar-service-0
spec:
  type: NodePort
  selector:
    statefulset.kubernetes.io/pod-name: foobar-app-0
  ports:
    - protocol: TCP
      nodePort: 30036
      port: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: foobar-service-1
spec:
  type: NodePort
  selector:
    statefulset.kubernetes.io/pod-name: foobar-app-1
  ports:
    - protocol: TCP
      nodePort: 30037
      port: 3000

Is this considered an acceptable solution or is there a better one for creating services for each pod?这是否被认为是可接受的解决方案,还是有更好的解决方案来为每个 pod 创建服务?

As explained in the comments above I found the solution provided here by using NodePort service targeting a StatefulSet with externalTrafficPolicy=Local .正如上面的评论中所解释的,我通过使用 NodePort 服务找到了此处提供的解决方案,该服务针对具有externalTrafficPolicy=Local的 StatefulSet。 This disables the cluster wide load balancing between different nodes.这将禁用不同节点之间的集群范围负载平衡。 The prerequisite is that only one pod of the stateful set may run per node, which can be achieved by setting pod anti-affinity .前提是每个节点只能运行有状态集合中的一个 pod,这可以通过设置pod anti-affinity来实现。

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

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