简体   繁体   English

将流量路由回特定的 pod

[英]Routing traffic back to a specific pod

I have an ingress resource like the following:我有一个如下所示的入口资源:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: "app-ingress"
  labels:
    app: "app"
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/backend-protocol: "GRPC"
    cert-manager.io/cluster-issuer: letsencrypt
spec:
  tls:
    - hosts:
        - api.example.com
      secretName: tls-secret
  rules:
    - host: api.example.com
      http:
        paths:
          - path: /grpc
            pathType: ImplementationSpecific
            backend:
              service:
                name: "app-server"
                port:
                  number: 50051
          - path: /callback
            pathType: ImplementationSpecific
            backend:
              service:
                name: "app-server"
                port:
                  number: 80

app-server runs two services (on the same pod), a grpc web server and a http web server. app-server运行两个服务(在同一个 pod 上),一个grpc web 服务器和一个http web 服务器。 To start, a client makes a request to api.example.com/grpc .首先,客户端向api.example.com/grpc发出请求。 To fulfill the request, the server makes a request to a third party service, waits for a callback on api.example.com/callback , and then completes the client's request ( api.example.com/callback is only accessed by the third party service, not the client).为了完成请求,服务器向第三方服务发出请求,等待回调api.example.com/callback ,然后完成客户端的请求( api.example.com/callback仅由第三方访问服务,而不是客户)。

The question is, how can I specifically route api.example.com/callback back to the same pod that made the initial request?问题是,我如何专门api.example.com/callback路由回发出初始请求的同一个 pod? Obviously if pod1 were to make a request to the third party service and pod2 were to receive the callback, pod1 would never receive the callback and the client request would have to timeout.显然,如果pod1向第三方服务发出请求,而pod2收到回调, pod1将永远不会收到回调,客户端请求将不得不超时。 I can attach information to the callback I receive from the third party service, so is there a way I could attach some sort of pod id so that kube.netes will route the callback back to the pod that initiated it?我可以将信息附加到我从第三方服务收到的回调中,那么有没有一种方法可以附加某种 pod id,以便 kube.netes 将回调路由回发起它的 pod?

I've looked at maybe using StatefulSets , but I'm not sure exactly how this would work.我已经看过可能使用StatefulSets ,但我不确定它到底是如何工作的。 Or would it be better to have sort of "notification pod" that receives callbacks and routes them to the correct pod?或者拥有一种接收回调并将它们路由到正确的 pod 的“通知 pod”会更好吗?

One way would be to user sticky sessons .一种方法是用户粘性会话
If you want to make sure that connections from a particular client are passed to the same Pod each time, you can select the session affinity based on the client's IP addresses by setting service.spec.sessionAffinity to "ClientIP" (the default is "None").如果你想确保来自特定客户端的连接每次都传递到同一个 Pod,你可以通过将service.spec.sessionAffinity设置为“ClientIP”(默认为“None”)来基于客户端的 IP 地址 select session affinity ”)。 You can also set the maximum session sticky time by setting service.spec.sessionAffinityConfig.clientIP.timeoutSeconds appropriately (the default value is 10800, which works out to be 3 hours).您还可以通过适当设置service.spec.sessionAffinityConfig.clientIP.timeoutSeconds来设置最长 session 粘滞时间(默认值是 10800,算出来是 3 小时)。

Another way is to design the Application in away that it doesn't matter which pod receives callback request, the proper pod with the pending client connection will be notified (and provided with data), and send a response back to the client.另一种方法是设计应用程序,无论哪个 pod 接收回调请求都无关紧要,具有待处理客户端连接的正确 pod 将收到通知(并提供数据),并将响应发送回客户端。 To communicate requests data you can use some sort of messaging service or distributed in-memory data stores like Redis.要传达请求数据,您可以使用某种消息服务或分布式内存数据存储,如 Redis。

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

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