Why does kubectl have different behavior with sudo?

8/6/2017

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.
-- Chris Stryczynski
kubectl
kubernetes

2 Answers

8/6/2017

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

-- Jordan Liggitt
Source: StackOverflow

8/6/2017

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.

-- Anshul Jindal
Source: StackOverflow