[英]Kubernetes WebSocket service: Connection refused between pods using hostname
谷歌的所有鏈接都是粉紅色的......我已經閱讀了這里幾乎所有的答案並咨詢了幾位同事,他們無法弄清楚我的配置有什么問題。
// 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 也會使活動探測失敗。
kubectl exec -it $(kubectl get pods -o name | grep market-data) /bin/sh
curl http://localhost:5000/healthz
{"hello":"im alive"}/
> 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
curl 10.104.1.61:80/healthz
curl: (7) Failed to connect to 10.104.1.61 port 80 after 3 ms: Connection refused
因為我們在這里得到了超時,這對我來說表明我們之前實際上已經到達了正在運行的容器。
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
來檢查它是否真的運行正常 - 它是。NodePort
和ClusterIp
kubectl describe service market-data-ws
好吧,我終於找到了我的問題,哇,這是一段時間以來最難的問題。
感謝這個答案,我能夠解決我的問題。
老實說,我不確定為什么這不足以與 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.