error: The connection to the server localhost:8080 was refused - did you specify the right host or port?
In most cases, this error message is displayed when the kubectl
configuration of your cluster is not set up correctly.
Basic information
By default ‘kubectl’ configuration file is located in $HOME/.kube/config
and contains the following sections:
Cluster information:
User information:
Context information:
- combinations of cluster and user references
current context that is used if no context is specified in kubectl
command line
contexts:
- context:
cluster: kubernetes
user: kubernetes-admin
name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
Usage:
You can view your current kubectl
configuration using the following command:
kubectl config view
It is possible to have several configuration files that you can add to the environment variable KUBECONFIG
:
export KUBECONFIG=$KUBECONFIG:config-demo:config-demo-2
You can also specify ‘kubectl’ config file in the command line:
kubectl --kubeconfig path/to/kubectl.conf get nodes
You can export your current kubectl
config and use it on another workstation:
kubectl config view --flatten
Articles:
Please find more information about kubectl
configuration and usage in the following articles:
How to create:
You can create kubectl
configuration manually or as a part of cluster creation process.
Here is how to create kubectl
configuration for existing kops cluster( link1, link2 ):
# update cluster will do it automatically after cluster creation.
# But we expect that if you're part of a team you might share the KOPS_STATE_STORE
# ASSUMPTION: You have pointed kops to some location where the cluster configurations are stored
# (I have this in my ~/.bash_profile):
export KOPS_STATE_STORE=s3://example-state-store
# Use kops to get the list of clusters
$ kops get clusters
# Export the configuration of the cluster you care about; this will update your ~/.kube/config file, so kubectl knows about it:
$ kops export kubecfg cluster-foo.example.com
# You can now use kubernetes using the kubectl tool:
$ kubectl get nodes
If you created your cluster using kubeadm, kubectl
config is located in /etc/kubernetes/admin.conf
on the master node and you can just copy it to your home directory:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config