我怎么知道哪个 pod 处理了请求?

How do I know which pod processes the request?

我用这个 Deployment.yml 创建了 pods。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: price-calculation-deployment
  labels:
    app: price-calculation
spec:
  replicas: 2
  selector:
    matchLabels:
      app: price-calculation
  template:
    metadata:
      labels:
        app: price-calculation
    spec:
      containers:
        - name: price-calculation
          image: ${somewhere}/price-calculation:latest
          ports:
            - containerPort: 80
              protocol: TCP
        - name: pc-mongodb
          image: mongo:latest
          ports:
            - containerPort: 27017
              protocol: TCP
      imagePullSecrets:
        - name: acr-auth

后来,我用这个 Service.yml 向外部公开端口。

apiVersion: v1
kind: Service
metadata:
  name: price-calculation-service
spec:
  type: LoadBalancer
  ports:
    - port: 5004
      targetPort: 80
      protocol: TCP
  selector:
    app: price-calculation

终于,两者都可以正常工作了。好

因为我在 Service.yml 中配置了 LoadBalancer,所以应该有一个 LoadBalancer 将请求分派到 2 replicas/pods。

Now, I want to know which pod takes the request and how do I know that?

谢谢!!!

好吧,最简单的方法 - 让 pods 在响应中写下他们的身份,这样你就会知道哪个 pod 响应了。另一种方法 - 使用 Zipkin\Jaeger 实现分布式跟踪。这将使您深入了解网络流程。

我相信 kubernetes 不提供任何类型的内置网络跟踪。

将广告连播名称附加到在用户浏览器上呈现的响应中。这就是您知道哪个 Pod 正在处理请求的方式

您可以查看 pod 日志以查看对 pod 发出的请求:

 kubectl logs my-pod                                           # dump pod logs (stdout)

 kubectl logs my-pod -c my-container                 # dump pod container logs (stdout, multi-container case)

或者添加到应用程序本身的响应中,例如在 Nodejs 应用程序中它可能如下所示:

const http = require('http');
const os = require('os');

var handler = function(request, response) {
  response.writeHead(200);
  response.end("You have hit " + os.hostname() + "\n");
};

var app = http.createServer(handler);

app.listen(8080);

然后您可以使用 curl 测试您的服务并获得响应:

Request:
curl http://serviceIp:servicePort

Response:
You have hit podName

根据应用程序的编程语言,只需找到一个 module/library,它提供了一个实用方法来获取主机名,您可以在响应中将其返回以进行调试。