使用二进制方式安装K8S时使用kubectl命令报错:The connection to the server localhost:8080 was refused - did you specify the right host or port?

时间:2020-11-26 17:57:37

解决思路:

kubectl 默认从 ~/.kube/config 配置文件获取访问 kube-apiserver 地址、证书、用户名等信息,如果没有配置该文件,或者该文件个别参数配置出错,执行命令时出错:The connection to the server localhost:8080 was refused - did you specify the right host or port?

解决办法:

1 查看该文件是否存在,若不存在则生成

2 查看该文件内容是否有缺,尤其是server字段,这个是kube-apiserver的地址,执行kubectl命令只要是跟这个地址进行交互操作

/root/.kube/config文件示例:

apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR4akNDQXE2Z0F3SUJBZ0lVVTlNOFd4SGVnK3NpRldMM210TXp2cHBKQ3RBd0RRWUpLb1pJaHZjTkFRRUwKQlFBd2FERUxNQWtHQTFVRUJoTUNRMDR4RURBT0JnTlZCQWdUQjBKbGFVcHBibWN4RURBT0JnTlZCQWNUQjBKbAphVXBwYm1jeEREQUtCZ05WQkFvVEEyczRjekVTTUJBR0ExVUVDeE1KTkZCaGNtRmthV2R0TVJNd0VRWURWUVFECkV3cHJkV0psY201bGRHVnpNQ0FYRFRFNU1URXdOekExTkRFd01Gb1lEekl4TVRreE1ERTBNRFUwTVRBd1dqQm8KTVFzd0NRWURWUVFHRXdKRFRqRVFNQTRHQTFVRUNCTUhRbVZwU21sdVp6RVFNQTRHQTFVRUJ4TUhRbVZwU21sdQpaekVNTUFvR0ExVUVDaE1EYXpoek1SSXdFQVlEVlFRTEV3azBVR0Z5WVdScFoyMHhFekFSQmdOVkJBTVRDbXQxClltVnlibVYwWlhNd2dnRWlNQTBHQ1NxR1NJYjNEUUVCQVFVQUE0SUJEd0F3Z2dFS0FvSUJBUUNsMis0T0dzRi8Kc25KVFdFdkpYR21wWmpkN1ZNa1dOWkVwVVlraW1GNXNlYzZXUnNtcGcwSmtDWmN3VllXQmRHZXVXNXlIbENidgo3SXZ6RDFDSD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ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
server: https://127.0.0.1:8443
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: admin
name: kubernetes
current-context: kubernetes
kind: Config
preferences: {}
users:
- name: admin
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQ0ekNDQXN1Z0F3SUJBZ0lVY0lBYmlTQm1jeDlDZDFMbjQrczlYY0l5cFhZd0RRWUpLb1pJaHZjTkFRRUwKQlFBd2FERUxNQWtHQTFVRUJoTUNRMDR4RURBT0JnTlZCQWdUQjBKbGFVcHBibWN4RURBT0JnTlZCQWNUQjBKbAphVXBwYm1jeEREQUtCZ05WQkFvVEEyczRjekVTTUJBR0ExVUVDeE1KTkZCaGNtRmthV2R0TVJNd0VRWURWUVFECkV3cHJkV0psY201bGRHVnpNQjRYRFRFNU1URXdOekEyTURjd01Gb1hEVEk1TVRFd05EQTJNRGN3TUZvd2JqRUwKTUFrR0ExVUVCaE1DUTA0eEVEQU9CZ05WQkFnVEIwSmxhVXBwYm1jeEVEQU9CZ05WQkFjVEIwSmxhVXBwYm1jeApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1SSXdFQVlEVlFRTEV3azBVR0Z5WVdScFoyMHhEakFNCkJnTlZCQU1UQldGa2JXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQTR5MmwKNEsxWkdMMmdLbE5zVmZiUXpBQlI3Tis5bGxTeTFxNnU5NmVsMkxDeDgrQTZad25Ra2QyR0FiYTZWNDBwN3liWAptVGI4a3VXV0NXQ2Q0UmRsQk9BQ3ZsVEVFcDBFT0RQSGF3WHk4V2FMbTVLT2NpNy9LS1J5ZUErRUIwWklkTmhiCm1pSmloU0xFb21YMEhKY2FYOUM3Vkl0SEl3QmUzbXhIRTdFZ1Bid0VpNGxDbkJwRHk0bElIaVlqTEN5ajJ6aWQKQkRyMVlBd2Fi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