【问题标题】:How do i get hostname in the kubernetes pod如何在 kubernetes pod 中获取主机名
【发布时间】:2021-07-23 03:48:56
【问题描述】:

我有 3 个入口指向同一个服务。在我的 kubernetes pod 中,我如何找到主机名以及来自哪个子域的请求。我在 golang 服务器中的后端代码。
当请求到达任何 pod 时,我想知道从哪个子域(x,y,x)请求到达 pod。目前在 golang 代码中,它提供主机名作为 pod ip 地址

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]}
}
}

【问题讨论】:

    标签: go kubernetes kubernetes-ingress gorilla


    【解决方案1】:

    尝试使用入口控制器添加的X-Forwarded-Host标头

    【讨论】:

    • 我们不使用 nginx,我们使用本地 golang 服务器
    • 你的入口控制器是 nginx no?
    • 入口控制器是traefik
    • 它们使用相同的标头。试试吧?
    • 如果您也可以在问题中提供 traefik 版本。
    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多