使用Kubectl -f filename.yml应用配置文件,但出现此错误

时间:2019-12-17 17:12:26

标签: kubernetes kubectl

我已创建一个部署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?或取消任何限制?

2 个答案:

答案 0 :(得分:0)

您的kubectl CLI似乎配置不正确。

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

答案 1 :(得分:0)

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变量。

$ 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?
相关问题