解决kubectl exec Unauthorized报错问题
标签搜索

解决kubectl exec Unauthorized报错问题

mrui
2025-04-09 / 0 评论 / 31 阅读 / 正在检测是否收录...

搭建kubernetes集群,最好的方式是使用kubeadm命令进行,使用二进制方式手动部署,步骤繁琐极易出错,使用过程中很容易出现一些问题。

问题复现

kubectl logs myhttp
error: You must be logged in to the server (the server has asked for the client to provide credentials ( pods/log myhttp))
kubectl exec myhttp -- bash
error: Internal error occurred: unable to upgrade connection: Unauthorized

这个报错提示无法升级连接,未经授权。

验证问题

# 修改/etc/kubernetes/kubelet.config,启用匿名连接。
authentication.anonymous.enabled 的值设置为true
#保存修改后重启集群内所有的kubelet。

重启kubelet后会发现kubectl exec或者logs命令可以正常使用,这时就可以确认是kubelet与kube-apiserver之间通信时使用的认证相关配置出现了问题。

解决报错

在kube-apiserver的配置文件中,加入与kubelet通信相关的认证配置。
这涉及到两条命令,分别是--kubelet-client-certificate和--kubelet-client-key。
之后只需要根据kubelet配置文件中的认证文件来修改kube-apiserver相关参数即可。

#查看kubelet使用的证书文件
grep client kubelet.config 
    clientCAFile: /etc/kubernetes/pki/ca.crt
#根据上面的证书文件对kube-apiserver配置进行修改
vim /etc/kubernetes/apiserver
#添加下面两行内容
--kubelet-client-certificate=/etc/kubernetes/pki/ca.crt \
--kubelet-client-key=/etc/kubernetes/pki/ca.key \

对集群内master节点上的所有kube-apiserver进行修改,然后重启kube-apiserver即可。

kubectl exec -it myhttp -- bash
root@myhttp:/usr/local/apache2# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs    modules
root@myhttp:/usr/local/apache2# exit
exit
kubectl logs myhttp
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.244.61.131. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.244.61.131. Set the 'ServerName' directive globally to suppress this message
[Wed Apr 09 11:57:48.855928 2025] [mpm_event:notice] [pid 1:tid 1] AH00489: Apache/2.4.63 (Unix) configured -- resuming normal operations
[Wed Apr 09 11:57:49.254647 2025] [core:notice] [pid 1:tid 1] AH00094: Command line: 'httpd -D FOREGROUND'
0

评论 (0)

取消