user1844634
user1844634

Reputation: 1263

How do i get hostname in the kubernetes pod

I have 3 ingress pointing to the same service. In my kubernetes pod, how can i find the hostname and from which subdomain request is coming . my backend code in golang server.
when the request comes to any pod, i want to know from which subdomain(x,y,x) request has come to pod. Currently in the golang code it's giving hostname as pod ip address

kind: Ingress
metadata:
  annotations:
    ingress.kubernetes.io/browser-xss-filter: 'true'
    ingress.kubernetes.io/force-hsts: 'true'
    ingress.kubernetes.io/hsts-include-subdomains: 'true'
    ingress.kubernetes.io/hsts-max-age: '315360000'
  name: test
  namespace: test
spec:
  rules:
    - host: http://x.test.com
      http:
        paths:
          - backend:
              serviceName: test-service
              servicePort: 8080
            path: /
    - host: http://y.test.com
      http:
          paths:
            - backend:
                serviceName: test-service
                servicePort: 8080
              path: /
    - host: http://z.test.com
      http:
          paths:
            - backend:
                serviceName: test-service
                servicePort: 8080
              path: /
func Subdomain( r *http.Request) {
 host := r.URL.Host
host = strings.TrimSpace(host)
//Figure out if a subdomain exists in the host given.
host_parts := strings.Split(host, ".")
if len(host_parts) > 2 {
    //The subdomain exists, we store it as the first element 
    //in a new array
    subdomain := []string{host_parts[0]}
}
}

Upvotes: 2

Views: 2788

Answers (1)

XciD
XciD

Reputation: 2617

Try to use the X-Forwarded-Host header added by the ingress controller

Upvotes: 1

Related Questions