在 Istio 中,istio-proxy
(基于 Envoy)代理 HTTP 流量的端口取决于具体配置和服务类型。以下是以 Markdown 格式整理的详细说明,涵盖 istio-proxy
如何处理 HTTP 流量以及相关端口信息:
istio-proxy
使用 iptables 规则 或 直接配置 来拦截和代理应用的流量。HTTP 流量的代理端口通常包括以下几种情况:
出站流量(Outbound):
istio-proxy
拦截 Pod 的出站 HTTP 流量(通常是 80 或 8080 端口),并将其重定向到 15001 端口(Envoy 的默认出站端口)。istio-proxy
的 15001 端口处理。入站流量(Inbound):
istio-proxy
默认监听 15006 端口,然后根据服务的目标端口(例如 80 或 8080)进行转发。port
和 targetPort
定义。例如,Service 定义的端口是 80,istio-proxy
会将入站流量从 15006 转发到应用的实际端口(如 8080)。服务定义的端口:
istio-proxy
会根据 VirtualService
和 DestinationRule
的配置,代理这些端口的 HTTP 流量。VirtualService
中定义的 http
路由会指定目标服务的端口(如 host: reviews.default.svc.cluster.local, port: 9080
)。可以通过以下方法查看 istio-proxy
代理 HTTP 流量的具体端口:
在 istio-proxy
容器中运行以下命令,查看 Envoy 监听的端口:
kubectl exec -it <pod-name> -n <namespace> -c istio-proxy -- netstat -tuln
或使用更现代的 ss
命令:
kubectl exec -it <pod-name> -n <namespace> -c istio-proxy -- ss -tuln
输出示例:
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 0.0.0.0:15001 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:15006 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:15021 0.0.0.0:*
使用 istioctl
查看 Envoy 的集群和监听器配置,确认 HTTP 流量的端口:
istioctl proxy-config listener <pod-name>.<namespace> -o json
查找 port
字段,筛选与 HTTP 相关的监听器(通常绑定到 15001 或 15006)。输出示例:
{
"name": "0.0.0.0_15001",
"address": {
"socketAddress": {
"address": "0.0.0.0",
"portValue": 15001
}
},
"filterChains": [
{
"filters": [
{
"name": "envoy.filters.network.http_connection_manager"
}
]
}
]
}
这里的 envoy.filters.network.http_connection_manager
表示该监听器处理 HTTP 流量。
HTTP 流量的目标端口通常由 Kubernetes Service 和 VirtualService
定义。查看 Service 配置:
kubectl get service <service-name> -n <namespace> -o yaml
输出示例:
apiVersion: v1
kind: Service
spec:
ports:
- name: http
port: 80
targetPort: 8080
再检查 VirtualService
配置:
kubectl get virtualservice -n <namespace> -o yaml
输出示例:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
spec:
hosts:
- reviews.default.svc.cluster.local
http:
- route:
- destination:
host: reviews.default.svc.cluster.local
port:
number: 9080
这里,HTTP 流量被路由到 reviews
服务的 9080 端口,而 istio-proxy
会在 15001(出站)或 15006(入站)处理这些流量。
访问 Envoy 管理接口查看实时监听端口:
kubectl exec -it <pod-name> -n <namespace> -c istio-proxy -- curl -s http://localhost:15000/listeners
输出会列出所有监听器,查找与 HTTP 流量相关的监听器(通常绑定到 15001 或 15006)。
出站 HTTP 流量:
http://reviews:9080
)被 iptables 规则重定向到 istio-proxy
的 15001 端口。istio-proxy
再根据 VirtualService
和 DestinationRule
将请求转发到目标服务的正确端口。入站 HTTP 流量:
istio-proxy
的 15006 端口。istio-proxy
根据配置转发到应用的实际端口(如 8080)。HTTPS 流量:
istio-proxy
仍使用 15006(入站)或 15001(出站)代理,但会处理 TLS 终止或透传,具体取决于 DestinationRule
的 tls
配置。iptables 重定向:Istio 使用 iptables 规则将流量重定向到 istio-proxy
,因此应用本身可能感知不到 15001 或 15006 端口,而是直接与 Service 端口交互。
自定义端口:如果服务使用非标准端口(例如 8080、9090),需要在 VirtualService
中明确指定目标端口。
多协议支持:istio-proxy
不仅代理 HTTP/1.1,还支持 HTTP/2 和 gRPC,端口配置方式类似,但需确保 VirtualService
中的协议正确(http
或 http2
)。
调试端口冲突:如果端口配置错误,可能导致流量无法正确路由。使用 istioctl analyze
检查配置是否正确:
istioctl analyze -n <namespace>
VirtualService
定义(如 80、8080 等)。netstat
、ss
、istioctl proxy-config listener
或 Envoy 管理接口(curl http://localhost:15000/listeners
)确认端口。