Why does kubectl have different behavior with sudo?

对着背影说爱祢 提交于 2019-12-12 04:20:45

问题


Running kubectl get pods with sudo:

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

Running as a normal user:

kubectl get pods
No resources found.

回答1:


By default, kubectl looks in ~/.kube/config (or the file pointed to be $KUBECONFIG) to determine what server to connect to. Your home directory and environment are different when running commands as root. When no connection info is found, kubectl defaults to localhost:8080




回答2:


You would have run these commands from the normal user :

sudo cp /etc/kubernetes/admin.conf $HOME/
sudo chown $(id -u):$(id -g) $HOME/admin.conf
export KUBECONFIG=$HOME/admin.conf

which would have copied config file in your normal user home directory and that is why you are able to get to the connection from the normal host and not from sudo.



来源:https://stackoverflow.com/questions/45536428/why-does-kubectl-have-different-behavior-with-sudo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!