当配置未指向 localhost 时,与服务器 localhost:8080 的连接被拒绝

The connection to the server localhost:8080 was refused when the configuration does not point to localhost

我无法连接到我们的 Kubernetes 集群。 kubectl 命令似乎没有考虑配置...

当我发出 kubectl cluster-info(或 kubectl get pods) 我收到以下错误消息:

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

我怀疑 ~/.kube/config 指向我的 minikube,但事实并非如此:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS...==
    server: https://10.95.xx.yy:6443
  name: cluster.local
contexts:
- context:
    cluster: cluster.local
    namespace: xxx-cluster-xxx-xx-username
    user: username
  name: username-context
current-context: ""
kind: Config
preferences: {}
users:
- name: username
  user:
    client-certificate: .certs/username.crt
    client-key: .certs/username.key

令人惊讶的是,$KUBECONFIG 环境变量设置为正确的路径:

KUBECONFIG=/Users/username/.kube/config

并且 kubectl config view 工作正常(a.k.a。不是指向 localhost 而是指向 https://10.95.xx.yy:6443

最后我也尝试在调用kubectl时指定配置文件的路径(kubectl get pods --kubeconfig=/Users/username/.kube/config),但是还是报错...

您的当前上下文未设置,如 current-context: "" 所示;如果你要 运行 kubectl --context username-context get pods 我希望它能做你想做的更多。如果情况确实如此,可以 运行 kubectl config use-context username-context 设置 current-context 前进