You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kubernetes Ingress访问/socket.io/anomaly出现502 Bad Gateway求助

Kubernetes部署下Socket.IO通信异常排查

问题现象

本地运行正常,但部署到Kubernetes后,/socket.io/spectrogram路径可正常通信,/socket.io/anomaly路径返回502 Bad Gateway错误。Chrome控制台报错:

polyfills.b93b12e29a67f209.js:1 
GET http://<host_redacted>/socket.io/anomaly/?EIO=4&transport=polling&t=OnbiQow 502 (Bad Gateway)

配置信息

Ingress配置

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ .Values.name }}
  labels:
    name: {{ .Values.name }}
spec:
  rules:
  - host: {{ .Values.host }}
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: {{ .Values.name }}-service
            port:
              number: {{ .Values.service.port }}
      - path: /socket.io/spectrogram
        pathType: Prefix
        backend:
          service:
            name: spectrogram-ms-backend-service
            port:
              number: {{ .Values.service.spectrogramMsPort }}
      - path: /socket.io/anomaly
        pathType: Prefix
        backend:
          service:
            name: anomaly-detection-ms-service
            port:
              number: {{ .Values.service.anomalyMsPort }}

Values配置

service:
  spectrogramMsPort: 8080
  anomalyMsPort: 5000
  type: ClusterIP
  port: 4200
  protocol: TCP

Angular前端WebSocket配置

Anomaly模块

constructor(@Inject(ANOMALY_DETECTION_HTTP_WS_URL) url: string) {
  super(url,  { path: '/socket.io/anomaly', onlyBinaryUpgrades: true });
  this.anomalyMessageSubject =
  new BehaviorSubject<AnomalyMessage | null>(null);
  this.anomalyMessage$ = this.anomalyMessageSubject.asObservable();
  this.connect();
  this.on(this.eventName, (payload: Uint8Array) => {
    this.handleMessage(payload);
  });
}

Spectrogram模块

constructor(@Inject(SPECTROGRAM_HTTP_WS_URL) url: string) {
  super(url,  { path: '/socket.io/spectrogram', onlyBinaryUpgrades: true });
  this.spectrogramResponseSubject =
    new BehaviorSubject<BucketedDetections | null>(null);
  this.spectrogramResponse$ = this.spectrogramResponseSubject.asObservable();
  this.connect();
  this.on(this.eventName, (payload: Uint8Array) => {
    this.handleMessage(payload);
  });
}

后端配置

  • Spectrogram后端:Java SpringBoot服务,监听localhost:8080,Socket.IO上下文设为/socket.io/spectrogram
  • Anomaly后端:Python服务,监听localhost:5000,Socket.IO路径设为/socket.io/anomaly

Kubernetes服务列表

NAME                             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                               AGE
anomaly-detection-ms-service     ClusterIP   10.43.82.60     <none>        5000/TCP                              4m7s
cassandra                        ClusterIP   10.43.170.166   <none>        9042/TCP,8080/TCP                     14d
cassandra-headless               ClusterIP   None            <none>        7000/TCP,7001/TCP,7199/TCP,9042/TCP   14d
ewsm-demo-service                ClusterIP   10.43.179.244   <none>        4200/TCP                              15d
redis-service                    ClusterIP   10.43.30.227    <none>        6379/TCP                              16d
spectrogram-ms-backend-service   ClusterIP   10.43.77.213    <none>        8080/TCP                              16d

排查步骤

  1. 修正Python服务监听地址:
    本地监听localhost仅允许容器内部进程访问,Kubernetes服务需要通过容器IP访问后端,需将Python服务的监听地址改为0.0.0.0:5000,确保服务接收来自集群内的请求。

  2. 验证集群内服务连通性:
    在集群内启动临时Pod(如busybox),执行以下命令测试服务端点:

    kubectl run -it --rm --image=busybox:1.36 busybox-test
    wget -O- http://anomaly-detection-ms-service:5000/socket.io/anomaly
    

    若返回Socket.IO的握手响应则服务正常,若出现连接拒绝/超时,需检查Pod状态和服务配置。

  3. 检查Ingress控制器日志:
    查看Ingress控制器(如nginx-ingress)的日志,定位502错误的具体原因:

    kubectl logs -n ingress-nginx <ingress-controller-pod-name>
    

    日志中会显示是连接后端超时、拒绝还是路径匹配问题。

  4. 确认Python服务Socket.IO路径配置:
    确保Python服务的Socket.IO路径配置与Ingress路径匹配,若服务内部处理路径时存在前缀截断问题,需调整Ingress的pathType或服务的路径配置。

  5. 检查Pod健康状态:
    查看anomaly-detection-ms-service对应的Pod状态和日志,确认服务是否正常启动:

    kubectl get pods -l <pod-label-selector>
    kubectl logs <anomaly-pod-name>
    

内容的提问来源于stack exchange,提问作者Ben Whitely

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 23:20:03