Kubernetes 端口转发 - 连接被拒绝

标签 kubernetes portforwarding kubectl

转发端口时出现以下错误。任何人都可以帮忙吗?

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

这是 pod 的描述。我的期望是,当我在浏览器中点击 localhost:88 时,请求应该转发到 jafary/sentiment-analysis-frontend 容器并且应用程序页面应该加载
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.

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

要使用端口 82 使您的设置工作,您需要更改 nginx 配置文件,以便它监听端口 82 而不是 80。您可以通过创建自己的 docker 镜像来完成此操作,并将更改内置到您的镜像中,或者您也可以使用 configMap 将默认配置文件替换为您想要的设置

关于Kubernetes 端口转发 - 连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53799600/

相关文章:

kubernetes - Istio mTLS仅在某些服务之间工作,即使tls-check打印每个人的状态都可以

nginx - minikube + nginx + volumeMount不起作用

templates - Golang 模板 (helm) 遍历 map 列表

ruby-on-rails - Vagrant:无法通过 localhost:8080 访问 Rails 服务器

xml - 是否可以使用 UPnP 和 SOAP 映射端口,以绑定(bind)到我的 http 服务器正在监听的本地端口?

docker - Kubectl长时间失败

docker - 入口路由不适用于使用Helm部署的服务

postgresql - 从远程主机访问 OpenShift 转发端口

amazon-web-services - 如何使用基于 [AWS] 的目标组 "IP"设置 Kubernetes NLB 负载均衡器?

amazon-web-services - 在 CodeBuild 阶段运行 kubectl apply -f hello-k8s.yml 时出现 "Unable to recognize\"hello-k8s.yml\": Unauthorized"错误