有没有办法知道服务在 Kubernetes 中负载均衡的是哪个 pod?

Is there any way to know which pod the service is load-balanced in Kubernetes?

我通过 Deployment 管理 3 Pods 并通过 NodePort of Service 连接。
我想知道每当我从外部连接时,服务负载平衡的是哪个 pod。
Pods log很难查,可以通过event或者kubectl命令查吗?

我不确定这是否正是您要查找的内容,但您可以使用 Istio 为所有服务通信生成详细的遥测数据。

您可能对Distributed tracing特别感兴趣:

Istio generates distributed trace spans for each service, providing operators with a detailed understanding of call flows and service dependencies within a mesh.

通过使用分布式跟踪,您可以在每个请求流经网格时对其进行监控。
有关使用 Istio 进行分布式跟踪的更多信息,请参阅 FAQ on Distributed Tracing 文档。
Istio 支持多个跟踪后端(例如 Jaeger)。

Jaeger 是一个类似于 OpenZipkin and as we can find in the jaegertracing documentation:

的分布式跟踪系统

It is used for monitoring and troubleshooting microservices-based distributed systems, including:

  • Distributed context propagation
  • Distributed transaction monitoring
  • Root cause analysis
  • Service dependency analysis
  • Performance / latency optimization

当然,您无需安装 Istio 即可使用 Jaeger,但您必须检测您的应用程序,以便将堆栈不同部分的跟踪数据发送到 Jaeger。


我将向您展示如何使用 Jaeger 监控示例请求。

假设我有一个 app-1 Deployment,其中三个 Pods 使用 NodePort 服务公开。

$ kubectl get pod,deploy,svc
NAME                                        READY   STATUS    RESTARTS   AGE   IP           
app-1-7ddf4f77c6-g682z                      2/2     Running   0          25m   10.60.1.11   
app-1-7ddf4f77c6-smlcr                      2/2     Running   0          25m   10.60.0.7    
app-1-7ddf4f77c6-zn7kh                      2/2     Running   0          25m   10.60.2.5    

NAME                                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/app-1                      3/3     3            3           21m

NAME                                         TYPE           CLUSTER-IP    EXTERNAL-IP      PORT(S)                      AGE
service/app-1                                NodePort       10.64.0.88    <none>           80:30881/TCP                 25m

此外,我部署了 jaegeristio):

$ kubectl get deploy -n istio-system | grep jaeger
jaeger                 1/1     1            1           67m

为了检查 Jaeger 是否按预期工作,我将尝试从集群外部连接到此 app-1 应用程序(使用 NodePort 服务):

$ curl <PUBLIC_IP>:30881
app-1

让我们用 Jaeger 找到这个踪迹:

如你所见,我们可以很容易地找出是哪个Pod收到了我们的请求。