首页 > 解决方案 > 如何为“kubectl port-forward”设置正确的端口(奇怪地转到 localhost:8080)

问题描述

我通过 kubeadm 安装 kubernetes 并关闭并再次启动。然后只是kubectl port-forward不起作用。

以下命令有效。

kubectl top nodes

但是 kubectl 端口转发失败。有什么建议吗?似乎只有端口转发到 localhost:8080。不工作

sudo kubectl port-forward -n istio-system svc/istio-ingressgateway 80:80 --address 0.0.0.0

正常模式下的日志如下。

The connection to the server localhost:8080 was refused - did you specify the right host or port?

对于调试模式日志

I0930 01:44:46.904299   47718 round_trippers.go:423] curl -k -v -XGET  -H "Accept: application/json, */*" -H "User-Agent: kubectl/v1.17.12 (linux/amd64) kubernetes/5ec4722" 'http://localhost:8080/api?timeout=32s'
I0930 01:44:46.904927   47718 round_trippers.go:443] GET http://localhost:8080/api?timeout=32s  in 0 milliseconds
I0930 01:44:46.904948   47718 round_trippers.go:449] Response Headers:
I0930 01:44:46.904987   47718 cached_discovery.go:121] skipped caching discovery info due to Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
I0930 01:44:46.905009   47718 shortcut.go:89] Error loading discovery information: Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
I0930 01:44:46.905067   47718 round_trippers.go:423] curl -k -v -XGET  -H "Accept: application/json, */*" -H "User-Agent: kubectl/v1.17.12 (linux/amd64) kubernetes/5ec4722" 'http://localhost:8080/api?timeout=32s'
I0930 01:44:46.905222   47718 round_trippers.go:443] GET http://localhost:8080/api?timeout=32s  in 0 milliseconds
I0930 01:44:46.905240   47718 round_trippers.go:449] Response Headers:
I0930 01:44:46.905263   47718 cached_discovery.go:121] skipped caching discovery info due to Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
I0930 01:44:46.905325   47718 round_trippers.go:423] curl -k -v -XGET  -H "Accept: application/json, */*" -H "User-Agent: kubectl/v1.17.12 (linux/amd64) kubernetes/5ec4722" 'http://localhost:8080/api?timeout=32s'
I0930 01:44:46.905465   47718 round_trippers.go:443] GET http://localhost:8080/api?timeout=32s  in 0 milliseconds
I0930 01:44:46.905482   47718 round_trippers.go:449] Response Headers:
I0930 01:44:46.905504   47718 cached_discovery.go:121] skipped caching discovery info due to Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
I0930 01:44:46.905556   47718 round_trippers.go:423] curl -k -v -XGET  -H "Accept: application/json, */*" -H "User-Agent: kubectl/v1.17.12 (linux/amd64) kubernetes/5ec4722" 'http://localhost:8080/api?timeout=32s'
I0930 01:44:46.905695   47718 round_trippers.go:443] GET http://localhost:8080/api?timeout=32s  in 0 milliseconds
I0930 01:44:46.905712   47718 round_trippers.go:449] Response Headers:
I0930 01:44:46.905734   47718 cached_discovery.go:121] skipped caching discovery info due to Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
I0930 01:44:46.905759   47718 helpers.go:221] Connection error: Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
F0930 01:44:46.905785   47718 helpers.go:114] The connection to the server localhost:8080 was refused - did you specify the right host or port?

kubectl config view如下

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://172.31.3.157:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

作为参考,工作kubectl top nodes调试模式如下,(日志太长,我只附上标题)

I0930 01:46:47.625764   49195 loader.go:375] Config loaded from file:  /home/ubuntu/.kube/config
I0930 01:46:47.641064   49195 round_trippers.go:423] curl -k -v -XGET  -H "User-Agent: kubectl/v1.17.12 (linux/amd64) kubernetes/5ec4722" -H "Accept: application/json, */*" 'https://172.31.3.157:6443/api?timeout=32s'
I0930 01:46:47.680091   49195 round_trippers.go:443] GET https://172.31.3.157:6443/api?timeout=32s 200 OK in 39 milliseconds
I0930 01:46:47.680116   49195 round_trippers.go:449] Response Headers:
I0930 01:46:47.680123   49195 round_trippers.go:452]     Cache-Control: no-cache, private
I0930 01:46:47.680128   49195 round_trippers.go:452]     Content-Type: application/json
I0930 01:46:47.680134   49195 round_trippers.go:452]     Content-Length: 135
I0930 01:46:47.680139   49195 round_trippers.go:452]     Date: Wed, 30 Sep 2020 01:46:47 GMT
I0930 01:46:47.680340   49195 request.go:1017] Response Body: {"kind":"APIVersions","versions":["v1"],"serverAddressByClientCIDRs":[{"clientCIDR":"0.0.0.0/0","serverAddress":"172.31.3.157:6443"}]}
I0930 01:46:47.680594   49195 round_trippers.go:423] curl -k -v -XGET  -H "Accept: application/json, */*" -H "User-Agent: kubectl/v1.17.12 (linux/amd64) kubernetes/5ec4722" 'https://172.31.3.157:6443/apis?timeout=32s'

标签: kubernetesistio

解决方案


推荐阅读