我如何知道哪个Pod处理请求?

时间:2019-02-10 14:46:07

标签: kubernetes azure-kubernetes

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

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个副本/吊舱。

  

现在,我想知道哪个吊舱接受了请求,我怎么知道   那?

谢谢!

3 个答案:

答案 0 :(得分:2)

嗯,最简单的方法-让Pod在响应中写出自己的身份,这样您就知道哪个Pod响应了。另一种方法-使用Zipkin \ Jaeger实现分布式跟踪。这将使您深入了解网络流。

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

答案 1 :(得分:0)

将pod名称添加到在用户浏览器上呈现的响应中。这样您才能知道哪个Pod正在处理请求

答案 2 :(得分:0)

您可以查看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

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

相关问题