如何解决我如何在 kubernetes pod 中获取主机名
我有 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]}
}
}
解决方法
尝试使用入口控制器添加的 X-Forwarded-Host
标头
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。