簡體   English   中英

Kubernetes WebSocket 服務:使用主機名的 Pod 之間的連接被拒絕

[英]Kubernetes WebSocket service: Connection refused between pods using hostname

谷歌的所有鏈接都是粉紅色的......我已經閱讀了這里幾乎所有的答案並咨詢了幾位同事,他們無法弄清楚我的配置有什么問題。

我的 WebSocket 服務器

// additional fastify websocket set-up - key point, is its running on 5000
const start = async () => {
  try {
    await fastify.listen({ port: 5000 });
    console.log(
      `Market data websocket listening on ws://localhost:5000`
    );
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};
start();

確認此服務器在本地工作

我使用wscat進行了測試,它連接得很好

wscat -c "ws://localhost:5000`

部署文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: market-data-ws
    spec:
      containers:
        - name: market-data-ws
          image: eu.gcr.io/test-repo/market-data-ws
          envFrom:
            - secretRef:
                name: market-data-ws-env
          resources:
            limits:
              memory: '0.5'
              cpu: '0.25'
          ports:
            - containerPort: 5000
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8080
            initialDelaySeconds: 5

服務

apiVersion: v1
kind: Service
metadata:
  name: market-data-ws
spec:
  type: ClusterIP
  selector:
    app: market-data-ws
  ports:
    - port: 80
      targetPort: 5000

我的問題

我正在嘗試從另一個 pod 服務器連接到此服務。 它不需要通過入口暴露。

注意:即使我執行並檢查活動探測它是否工作,pod 也會使活動探測失敗。

liveness 端點在 pod 內部工作

kubectl exec -it $(kubectl get pods -o name | grep market-data) /bin/sh
curl http://localhost:5000/healthz
{"hello":"im alive"}/

❌ 通過從另一個 pod 內卷曲主機名來訪問 pod = 連接立即被拒絕

> kubectl exec -it $(kubectl get pods -o name | grep ui) /bin/sh
# curl the health check
> curl http://market-data-ws/healthz
> curl: (7) Failed to connect to market-data-ws port 80 after 8 ms: Connection refused

❌ 卷曲 IP 而不是主機名 = 連接立即被拒絕

curl 10.104.1.61:80/healthz
curl: (7) Failed to connect to 10.104.1.61 port 80 after 3 ms: Connection refused

❓ 卷曲 IP(但錯誤的暴露端口)= 幾分鍾后連接超時

因為我們在這里得到了超時,這對我來說表明我們之前實際上已經到達了正在運行的容器。

curl 10.104.1.61:5000/healthz
curl: (28) Failed to connect to 10.104.1.61 port 5000 after 129952 ms: Operation timed out

❓ 當我將我的本地端口轉發到這個正在運行的服務端口時,它起作用了

kub port-forward service/market-data-ws 5000:5000
Forwarding from 127.0.0.1:5000 -> 5000
Forwarding from [::1]:5000 -> 5000

wscat -c http://localhost:5000
> Connected

我嘗試過的其他事情

  • 執行到market-data-ws pod 並使用wscat -c ws://localhost:5000來檢查它是否真的運行正常 - 它是。
  • 雙重、三次檢查我的 containterPort / targetPort 和同事已經確認它看起來 0okay。
  • 嘗試過NodePortClusterIp
  • 嘗試在端口 80 上運行我的 websocket 服務器並公開它

其他信息

  • 我正在使用 GCP GKE
  • kubectl describe service market-data-ws

kub描述

好吧,我終於找到了我的問題,哇,這是一段時間以來最難的問題。

感謝這個答案,我能夠解決我的問題。

Fastify 默認只監聽 localhost

老實說,我不確定為什么這不足以與 docker + kubernetes 一起使用,但我很高興它現在可以工作了。

如果有人能夠更詳細地解釋這一點,我將不勝感激

可以使用 kubectl port-forward 訪問您的服務,因此服務正在運行。

你說,你的 pod 不能從另一個 pod 訪問。 驗證另一個 pod 是否在同一命名空間中運行。

尋址 pod 的正確方法是:

servicename.namespace.svc.cluster.local

在無法連接的容器內,嘗試:

nc -zv servicename.namespace.svc.cluster.local port

應回復:打開

如果 nc 不可用,請嘗試 curl 或安裝 nc。

請注意,在您的一次嘗試中,您使用的是 clusterIp 地址。 這在集群外部不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM