Chris Stryczynski
Chris Stryczynski

Reputation: 33881

Why does kubectl have different behavior with sudo?

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.

Upvotes: 2

Views: 2901

Answers (3)

tmwong
tmwong

Reputation: 96

  • When you run sudo kubectl, kubectl will refer to /root/.kube/config .
  • When you run kubectl without sudo, kubectl will refer to $HOME/.kube/config .
  • If the <user home dir>/.kube/config file is not found by the kubectl, it will just call the default kubernetes API server URL localhost:8080 .

Upvotes: 0

Anshul Jindal
Anshul Jindal

Reputation: 388

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.

Upvotes: -1

Jordan Liggitt
Jordan Liggitt

Reputation: 18111

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

Upvotes: 7

Related Questions