LocalhostListener

当工作负载在监听 localhost 网络接口,但该端口在 Service 中已暴露时,会出现此消息。 当出现这种情况时,其他 Pod 将无法访问该端口。

增加此项检查主要是为了检测旧版 Istio 上的工作负载在升级到 Istio 1.10 或更高版本时可能会出现问题。 这种行为与未安装 Istio 的标准 Kubernetes 集群中会发生的情况相匹配,但旧版本的 Istio 会暴露这些端口。

示例

以一个 Service 为例,执行 nc localhost 8080 -l 命令选择 Pod

apiVersion: v1
kind: Service
metadata:
  name: netcat
spec:
  ports:
  - port: 8080
    protocol: TCP
  selector:
    app: netcat

因为应用程序正在通过 localhost 提供流量,所以不能从其他 Pod 进行访问。

以上例子演示了如何使用简单的 nc 工具。其他编程语言中可以使用的等效例子为:

  • Go:net.Listen("tcp", "localhost:8080")
  • Node.js:http.createServer().listen(8080, "localhost");
  • Python:socket.socket().bind(("localhost", 8083))

如何修复

如果您不打算将应用程序暴露给其他 Pod,请从 Service 中移除该端口。

如果要将应用程序暴露给其他 Pod,有两个选项:

  • 修改应用程序以绑定到对其他 Pod 暴露的网络接口。 通常,这意味着绑定到 0.0.0.0::,例如 nc 0.0.0.0 8080 -l
  • 创建 Sidecar 配置 来自定义 Pod 的入站网络配置。例如,对于上述应用程序:
apiVersion: networking.istio.io/v1beta1
kind: Sidecar
metadata:
  name: ratings
spec:
  workloadSelector:
    labels:
      app: netcat
  ingress:
  - port:
      number: 8080
      protocol: TCP
      name: tcp
    defaultEndpoint: 127.0.0.1:8080