k8s遇 The connection to the server :6443 was refused

一般而言,6443端口是用于给apiserver使用的,如果报这个错误,就说明apiserver要么没起来,要么就是端口被占用了。

快速排查

  • 挨个检查以下几个守护进程有无问题,如果有报错日志,则需要进行排查
systemctl status kubelet
systemctl status docker
systemctl status containerd

如果都没有问题,那就查看apiserver容器是否起来了

docker ps -a | grep kube-apiserver 
nerdctl -n k8s.io ps | grep kube-apiserver

如果apisever没有正常运行中,就需要查看日志进行错误排查。如果正常运行了,再确认etcd是否挂了

docker ps -a | grep etcd                       
nerdctl -n k8s.io ps | grep etcd    

同样如果有问题就需要看日志进行排查解决

一、证书过期

1.1 查看证书

在master节点上查看证书过期时间

$ kubeadm certs check-expiration
[check-expiration] Reading configuration from the cluster...
[check-expiration] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[check-expiration] Error reading configuration from the Cluster. Falling back to default configuration

CERTIFICATE                EXPIRES                  RESIDUAL TIME   CERTIFICATE AUTHORITY   EXTERNALLY MANAGED
admin.conf                 Jan 21, 2023 06:27 UTC   <invalid>       ca                      no
apiserver                  Jan 21, 2023 06:27 UTC   <invalid>       ca                      no
apiserver-etcd-client      Jan 21, 2023 06:27 UTC   <invalid>       etcd-ca                 no
apiserver-kubelet-client   Jan 21, 2023 06:27 UTC   <invalid>       ca                      no
controller-manager.conf    Jan 21, 2023 06:27 UTC   <invalid>       ca                      no
etcd-healthcheck-client    Jan 21, 2023 06:27 UTC   <invalid>       etcd-ca                 no
etcd-peer                  Jan 21, 2023 06:27 UTC   <invalid>       etcd-ca                 no
etcd-server                Jan 21, 2023 06:27 UTC   <invalid>       etcd-ca                 no
front-proxy-client         Jan 21, 2023 06:27 UTC   <invalid>       front-proxy-ca          no
scheduler.conf             Jan 21, 2023 06:27 UTC   <invalid>       ca                      no

CERTIFICATE AUTHORITY   EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
ca                      Jan 19, 2032 06:27 UTC   8y              no
etcd-ca                 Jan 19, 2032 06:27 UTC   8y              no
front-proxy-ca          Jan 19, 2032 06:27 UTC   8y              no

如果出现invalid字样的,就说明证书过期了
低版本集群执行这个命令可能会报错,可以执行这个命令

kubeadm alpha certs check-expiration

1.2 备份相关文件

cp -r /etc/kubernetes /etc/kubernetes.old

1.3 在每个master上执行命令更新证书

$ kubeadm certs renew all
[renew] Reading configuration from the cluster...
[renew] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[renew] Error reading configuration from the Cluster. Falling back to default configuration

certificate embedded in the kubeconfig file for the admin to use and for kubeadm itself renewed
certificate for serving the Kubernetes API renewed
certificate the apiserver uses to access etcd renewed
certificate for the API server to connect to kubelet renewed
certificate embedded in the kubeconfig file for the controller manager to use renewed
certificate for liveness probes to healthcheck etcd renewed
certificate for etcd nodes to communicate with each other renewed
certificate for serving etcd renewed
certificate for the front proxy client renewed
certificate embedded in the kubeconfig file for the scheduler manager to use renewed

低版本的集群下,执行命令会报错,可以执行命令:

kubeadm alpha certs renew all

1.4 在每个master节点上重启相关服务

docker ps |egrep "k8s_kube-apiserver|k8s_kube-scheduler|k8s_kube-controller"|awk '{print $1}'|xargs docker restart

1.5 更新kube config文件

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

二、集群硬件时间和系统时间不同步

重启服务器后系统时间会同步硬件时间,导致集群的时间管理混乱,进而导致此类问题
此类现象需要重新更改系统时间并把硬件时间和软件时间同步

date                                                               #确认系统时间
hwclock                                                            #确认硬件
#如果此时系统时间和硬件时间同步,但明显不是服务器重启之前的时间。请继续往下看。否则就不是本情况,请查看其他案例。

date -s "2022-12-08 12:00:00"                                      #首先进行系统时间的修改,此处为举例:系统时间修改为2022年12月8日 中午12点整
hwclock --hctosys                                                  #然后用硬件时钟同步系统时钟
timedatectl |awk -F":" '/synchronized/{print $2}'                  #检查ntp时间同步是否就绪,一般等待20-30分钟左右后会显示yes
kubectl get node                                                   #检查是否还会报错  

三、查看端口是否被占用/被防火墙、iptables拦截

netstat -napt | grep 6443                                    #首选确认端口是否被占用

#如果使用firewalld服务,通过firewall添加相应的端口来解决问题
systemctl enable firewalld|
systemctl start firewalld|
firewall-cmd --permanent --add-port=6443/tcp|
firewall-cmd --permanent --add-port=2379-2380/tcp|
firewall-cmd --permanent --add-port=10250-10255/tcp|
firewall-cmd –reload

#iptables相关规则/做过相关的安全加固等措施禁用了端口
iptables -nL        #查看是否存在6443端口相关规则被禁止,如果出现相关的问题,请进行相关排查

四、主机名修改

1、通用方案

#重新声明环境变量
ll /etc/kubernetes/admin.conf                                               #查看文件是否存在,如果不存在执行下面的步骤
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile      #重新写入环境变量
source ~/.bash_profile


2、containerd容器解决方案
systemctl restart kubelet                                       #尝试重启kubelet测试是否可以重新恢复正常 
journalctl -xefu kubelet                                        #查看kubelet的日志,里面寻找相应报错
nerdctl -n k8s.io ps                                            #根据iomp版本是用docker或者nerdctl,来查看k8s容器状态
#确认相关k8s容器是否正常,如果容器出现异常,进行相关排查


kubectl get node   #检查是否还会报错
  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
"The connection to the server :6443 was refused - did you specify the right host or port?"这个错误提示是在使用kubectl命令时到的。这个错误通常是由于连接到Kubernetes集群的服务器时出现了问题导致的。 有几个可能的原因可以解释这个错误: 1. 首先,确保你指定的主机和端口是正确的。检查你在使用kubectl命令时是否正确指定了Kubernetes集群的主机地址和端口号。如果你不确定正确的主机和端口,可以联系管理员或查看相关文档。 2. 另一个可能的原因是网络连接问题。确保你的网络连接正常,并且没有任何防火墙或网络限制导致无法连接到指定的主机和端口。 3. 还有一种可能性是Kubernetes集群的API服务器没有启动或无法访问。检查集群的API服务器是否正常运行,并且可以通过指定的主机和端口进行访问。 总结来说,当出现"The connection to the server :6443 was refused - did you specify the right host or port?"这个错误时,你需要检查你是否正确指定了主机和端口,检查网络连接是否正常,并确保Kubernetes集群的API服务器正在运行并且可以访问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [问题“The connection to the server....:6443 was refused - did you specify the right host or port?...](https://blog.csdn.net/sinat_28371057/article/details/109895159)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [ssh: connect to host 192.168.136.130 port 22: Connection refused,Fodera 远程连接失败](https://download.csdn.net/download/weixin_38661100/14889097)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值