Kubernetes端口转发-连接被拒绝



我在转发端口时收到以下错误。有人能帮忙吗?

mjafary$ sudo kubectl port-forward sa-frontend 88:82
Forwarding from 127.0.0.1:88 -> 82
Forwarding from [::1]:88 -> 82

错误日志:

Handling connection for 88
Handling connection for 88
E1214 01:25:48.704335   51463 portforward.go:331] an error occurred forwarding 88 -> 82: error forwarding port 82 to pod a017a46573bbc065902b600f0767d3b366c5dcfe6782c3c31d2652b4c2b76941, uid : exit status 1: 2018/12/14 08:25:48 socat[19382] E connect(5, AF=2 127.0.0.1:82, 16): Connection refused

这是吊舱的描述。我的期望是,当我在浏览器中点击localhost:88时,请求应该转发到jafary/情绪分析前端容器,应用程序页面应该加载

mjafary$ kubectl describe pods sa-frontend
Name:         sa-frontend
Namespace:    default
Node:         minikube/192.168.64.2
Start Time:   Fri, 14 Dec 2018 00:51:28 -0700
Labels:       app=sa-frontend
Annotations:  <none>
Status:       Running
IP:           172.17.0.23
Containers:
sa-frontend:
Container ID: docker://a87e614545e617be104061e88493b337d71d07109b0244b2b40002b2f5230967
Image:          jafary/sentiment-analysis-frontend
Image ID:       docker-pullable://jafary/sentiment-analysis-frontend@sha256:5ac784b51eb5507e88d8e2c11e5e064060871464e2c6d467c5b61692577aeeb1
Port:           82/TCP
Host Port:      0/TCP
State:          Running
Started:      Fri, 14 Dec 2018 00:51:30 -0700
Ready:          True
Restart Count:  0
Environment:    <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-mc5cn (ro)
Conditions:
Type           Status
Initialized    True 
Ready          True 
PodScheduled   True 
Volumes:
default-token-mc5cn:
Type:        Secret (a volume populated by a Secret)
SecretName:  default-token-mc5cn
Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none>

连接被拒绝的原因是端口82上没有进程侦听。用于创建nginx映像的dockerfile公开了端口80,在您的pod规范中,您还公开了端口82。然而,nginx被配置为在端口80上侦听。

这意味着你的吊舱有两个暴露的端口:80和82。然而,nginx应用程序正在端口80上主动侦听,因此只有对端口80的请求才能工作。

为了使用端口82进行设置,您需要更改nginx配置文件,使其在端口82而不是80上侦听。您可以通过创建自己的docker映像来完成这一操作,并将更改内置到映像中,也可以使用configMap将默认配置文件替换为您想要的设置

正如@Patric W所说,连接被拒绝,因为端口82上没有进程侦听。那个端口还没有暴露出来。

现在,要获得吊舱正在监听的端口,您可以运行命令

NB:请务必更改<gt具有实际价值。

  • 首先,获取指定命名空间kubectl get po -n <namespace>中pod的名称
  • 现在检查您想要转发的吊舱的暴露端口
kubectl get pod <pod-name> -n <namespace>  --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"n"}}'

现在使用上面得到的暴露端口使用命令向前运行端口

  • kubectl port-forward pod/<pod-name> <local-port>:<exposed-port>

其中local-port是从浏览器访问容器的端口..localhost:<local-port>,而exposed-port是容器侦听的端口。通常使用Dockerfile 中的EXPOSE命令定义

点击此处获取更多信息

正如帕特里克正确指出的那样。同样的问题困扰了我两天。所以步骤是:
  1. 确保Dockerfile使用您的首选端口(EXPOSE 5000(
  2. 在pod.yml文件中,确保containerPort为5000(containerPort:5000(
  3. 应用kubectl命令来反映以上内容:

kubectl port-forward pod/my-name-of-pod 8080:5000

最新更新