Kubernetes 从集群内部调用外部API
我想从集群内部调用外部api,所以我遵循了Kubernetes文档 详情如下:Kubernetes 从集群内部调用外部API,kubernetes,kubernetes-ingress,kong,Kubernetes,Kubernetes Ingress,Kong,我想从集群内部调用外部api,所以我遵循了Kubernetes文档 详情如下: apiVersion: v1 kind: Service metadata: name: my-service namespace: prod spec: type: ExternalName externalName: https://api.github.com/ 我得到了一个外部名称的回复: externalName: httpbin.org 但当我使用http或https端点时,它失败了
apiVersion: v1
kind: Service
metadata:
name: my-service
namespace: prod
spec:
type: ExternalName
externalName: https://api.github.com/
我得到了一个外部名称的回复:
externalName: httpbin.org
但当我使用http或https端点时,它失败了
我知道文件说明了以下内容
Warning:
You may have trouble using ExternalName for some common protocols, including HTTP and HTTPS. If you use ExternalName then the hostname used by clients inside your cluster is different from the name that the ExternalName references.
For protocols that use hostnames this difference may lead to errors or unexpected responses. HTTP requests will have a Host: header that the origin server does not recognize; TLS servers will not be able to provide a certificate matching the hostname that the client connected to.
但是我想知道如何从集群内部调用http/https到外部API?但是我想知道如何从集群内部调用http/https到外部API? 我看不出有什么理由不使用
ExternalName
从集群调用外部API。实际上,您不需要那种类型的服务来实现这一点。除非有什么你没告诉我们的
现在继续讨论为什么会遇到错误。使用ExternalName
服务类型,请求通过Host:my service
到达您的目的地,该服务是他从您创建的服务继承的。如果我们以您曾经提到的api.github.com
为例,尝试访问它,它会接收带有标题的http
请求Host:my service
,而他不知道如何处理它,也不知道将它路由到哪里。所以,如果您想预先使用api.github.com
为什么不直接访问它呢
请参见我制作的一个示例,该示例显示了ExternalName
注入的标题(我将服务命名为my service test
):
[root@cent/]#卷曲我的服务测试-v
*即将连接()到我的服务测试端口80(#0)
*正在尝试183.36.108.201。。。
*已连接到我的服务测试(183.36.108.201)端口80(#0)
>GET/HTTP/1.1
>用户代理:curl/7.29.0
>主持人:我的服务测试
>接受:*/*
>
nginx
*正在关闭连接0
Nginx收到了请求,但是主机
头与他期望的不匹配,因此他不知道如何处理它
解决这个问题的方法可能是使用nginx pod作为代理,也可能是使用ingress在报头到达所需目标之前尝试重写报头