[英]Very simple node.js application deployed to k8s cluster, port-forwarding is failing
我有一個非常簡單的 node.js 應用程序,服務器正在偵聽端口8282
:
const http = require('http');
const os = require('os');
...
...
var server = http.createServer(handler);
server.listen(8282);
我有一個Dockerfile
:
FROM node:12
COPY app.js /app.js
ENTRYPOINT ["node","app.js"]
然后我成功構建了鏡像myreg/my-app:1.0
並使用以下清單部署到我的 k8s 集群 (AWS EKS):
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
namespace: my-ns
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: myreg/my-app:1.0
ports:
- containerPort: 8282
imagePullSecrets:
- name: my-reg-cred
---
apiVersion: v1
kind: Service
metadata:
name: my-svc
namespace: my-ns
spec:
ports:
- name: http
port: 8282
targetPort: 8282
selector:
app: my-app
我可以看到 pod 正在運行:
kubectl --namespace=my-ns get po
NAME READY STATUS RESTARTS AGE
my-app-5477c9c798-5q4v4 1/1 Running 0 5m3s
然后我想在我的終端上做port-forwarding
:
$kubectl --namespace=my-ns port-forward my-app-5477c9c798-5q4v4 8282
Forwarding from 127.0.0.1:8282 -> 8282
Forwarding from [::1]:8282 -> 8282
我打開另一個終端 window,使用 curl 與我的 pod 通信:
curl localhost:8282
curl: (52) Empty reply from server
在運行端口轉發的另一個終端 window 上:
Handling connection for 8282
E0411 23:12:25.254291 45793 portforward.go:400] an error occurred forwarding 8282 -> 8282: error forwarding port 8282 to pod ca30fad7ea7100d684d1743573dea426caa9a333163ccbca395ed57eaa363061, uid : exit status 1: 2022/04/11 20:12:25 socat[1326] E connect(5, AF=2 127.0.0.1:8282, 16): Connection refused
為什么端口轉發在我的實現中失敗了? 我想念什么?
你的應用程序只聽 localhost ,嘗試將server.listen(8282)
更改為server.listen(8282,"0.0.0.0")
,重建和更新你的圖像並重新啟動你的部署。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.