How do I know which pod processes the request?

2/10/2019

I used this Deployment.yml to create 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

And, later on, I used this Service.yml to expose port to external.

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

Finally, both are working now. Good.

As I configured the LoadBalancer in the Service.yml, there shall be a LoadBalancer dispatches requests to the 2 replicas/pods.

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

Thanks!!!

-- ske
azure
azure-aks
azure-kubernetes
kubernetes

3 Answers

2/10/2019

You may view pod logs to see the requests made to the pod:

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

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

Or add to the response in the application itself, for instance in Nodejs app it might look like this:

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);

Then you can use curl to test out your service and get a response:

Request:
curl http://serviceIp:servicePort

Response:
You have hit podName

Depending on the app’s programming language, just find a module/library that provides a utility method to get a hostname and you’ll be good to go to give it back in the response for debugging purposes.

-- Alexz
Source: StackOverflow

2/10/2019

Append pod name to the response that gets rendered on the user's browser. That is how you know which pod is processing the request

-- P Ekambaram
Source: StackOverflow

2/10/2019

well, the easiest way - make pods write their identity in the response, that way you will know which pod responds. Another way - implement distributed tracing with Zipkin\Jaeger. That will give you deep insights into networking flows.

I believe kubernetes doesnt offer any sort of built-in network tracing.

-- 4c74356b41
Source: StackOverflow