0

I use spring boot project and deployed in Kubernetes, I would like to get URL of the pod,

I referred How to Get Current Pod in Kubernetes Java Application not worked for me.

There are different environments (DEV, QA, etc..) and wanted to get URL dynamically, is there anyway?

my service yaml

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: test-service
  namespace: default
spec:
  template:
    metadata:
      annotations:
        autoscaling.knative.dev/metric: concurrency
        # Disable scale to zero with a minScale of 1.
        autoscaling.knative.dev/minScale: "1"
        # Limit scaling to 100 pods.
        autoscaling.knative.dev/maxScale: "100"
    spec:
      containers:
        - image: testImage
          ports:
            - containerPort: 8080

is it possible to add

valueFrom:
     fieldRef:
     fieldPath: status.podIP

from url https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#the-downward-api

Kartoch
  • 7,610
  • 9
  • 40
  • 68
parrotjack
  • 432
  • 1
  • 6
  • 18

1 Answers1

0

Your case is about Knative ingress, and not Kubernetes.

From the inbound network connectivity part of the runtime contract of the knative documentation:

In addition, the following base set of HTTP/1.1 headers MUST be set on the request:

  • Host - As specified by RFC 7230 Section 5.4

Also, the following proxy-specific request headers MUST be set:

  • Forwarded - As specified by RFC 7239.

Look to the headers inside your request.

An example for servlet doGet method:

Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
  String paramName = (String)headerNames.nextElement();
  out.print("<tr><td>" + paramName + "</td>\n");
  String paramValue = request.getHeader(paramName);
  out.println("<td> " + paramValue + "</td></tr>\n");
}
Kartoch
  • 7,610
  • 9
  • 40
  • 68