对 Kubernetes 集群类型服务的卷曲请求

Curl request to a Kubernetes cluster type service

我正在尝试向 Kubernetes ClusterType 服务发送 curl 请求。有什么方法可以执行 curl 服务请求吗?

我正在使用 Blue/Green 部署来部署应用程序。所以这里需要验证蓝色版本是否正常工作。因此决定向蓝色版本发送 curl 请求。当我获得 200 状态时,我会将所有流量路由到此版本。

但现在面临向应用程序的新版本(蓝色版本)发送 curl 请求。

您不能向 ClusterType 服务发送 curl 请求,除非您在集群内,否则您可以 运行 一个 pod 来执行此操作,例如

kubectl run --rm --image nginx -i -- curl 'http://clusterip/'

这将 运行 一个临时 pod 到 运行 你的命令然后死掉。

ClusterIP 使服务只能从集群内访问。这是默认的服务类型。您可以阅读有关服务的更多信息 here

由于第一个答案中的命令不起作用,我发布了有效的解决方案:

kubectl run tmp-name --rm --image nginx -i --restart=Never -- /bin/bash -c 'curl -s clusterip:port'

使用上面的命令 curl 工作正常。您可以使用服务名称而不是集群 IP 地址。

像这样使用 curl 需要

--restart=Never

--rm 确保 Pod 在 shell 退出时被删除。

已编辑:

但是如果您想从 运行 kubectl 所在的主机访问您的 ClusterIp 服务,您可以使用端口转发。

kubectl port-forward service/yourClusterIpServiceName 28015:yourClusterIpPort

你会得到这样的输出:

Forwarding from 127.0.0.1:28015 -> yourClusterIpPort
Forwarding from [::1]:28015 -> yourClusterIpPort

之后您将能够使用此命令访问您的 ClusterIP 服务:

curl localhost:28015

更多关于端口转发的信息在the official documentation page