使用 Kubectl -f 文件名.yml 应用配置文件,但收到此错误



我创建了一个部署yaml文件。我正在尝试使用kubectl apply -f filename.yml应用此配置,但出现此错误:The connection to the server localhost:8080 was refused - did you specify the right host or port?

如何确保连接到localhost:8080? 或删除这样做的任何限制?

kubectl 是一个命令行工具工具,允许您针对 Kubernetes 集群运行命令。

默认情况下,kubectl 在 $HOME/.kube 目录中查找名为config 的文件以连接集群。 确保此路径中有一个名为 config 的文件,其中包含正确的值。

$ pwd
/home/ubuntu/.kube
$ ll
total 88
drwxrwxrwx 4 ubuntu ubuntu  4096 Jan 16 10:31 ./
drwxr-xr-x 7 ubuntu ubuntu  4096 Jan 16 10:30 ../
drwxr-x--- 3 ubuntu ubuntu  4096 Jan 13 13:00 cache/
-rwxr-xr-x 1 ubuntu ubuntu  5455 Jan 13 15:17 config*

您可以通过设置 KUBECONFIG 环境变量或设置 --kubeconfig 标志来指定其他 kubeconfig 文件。

注意:如果使用--kubeconfig传递配置文件,则它优先于 KUBECONFIG 环境变量

示例:设置 KUBECONFIG env varaible。

$ KUBECONFIG=$HOME/.kube/cluster-1
$ kubectl cluster-info
Kubernetes master is running at https://xx.xx.xx.101:6443
KubeDNS is running at https://xx.xx.xx.101:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

示例 : 使用 --kubeconfig 传递配置文件以覆盖默认的 KUBECONFIG

$ kubectl cluster-info --kubeconfig=/home/ubuntu/.kube/cluster-2
Kubernetes master is running at https://xx.xx.xx.102:6443
KubeDNS is running at https://xx.xx.xx.102:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

为了重现您的错误,我只需将 KUBECONFIG 设置为空,以表明您在运行 kubectl 命令时缺少此设置

$ KUBECONFIG=""
$ kubectl get pods
The connection to the server localhost:8080 was refused - did you specify the right host or port?

似乎您的kubectlCLI 配置不正确。

默认情况下,kubectl 在$HOME/.kube目录中查找名为 config 的文件。您可以通过设置KUBECONFIG环境变量或设置--kubeconfig标志来指定其他 kubeconfig 文件。

最新更新