I have logged into the master node of a k8s cluster, on the Google cloud platform(not GKE), the master has kubectl installed on it, and I am trying to get the list of nodes in the cluster

networkandcode@k8s-master:~$ kubectl get nodes
The connection to the server localhost:8080 was refused - did you specify the right host or port?

There seems to be an issue with accessing the API server, let's troubleshoot. Let's check the current kubeconfig

networkandcode@k8s-master:~$ kubectl config view
apiVersion: v1
clusters: []
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []

The kubeconfig doesn't have any info in it, we can also check the config file directly. '.kube/config' is the default path/file where kubectl checks for the kubeconfig that contains information such as the API server's IP address, user list, cluster list, user client certificates, user client keys, and so on

networkandcode@k8s-master:~$ cat .kube/config
cat: .kube/config: No such file or directory

networkandcode@k8s-master:~$ ls .kube
ls: cannot access '.kube': No such file or directory

The config file is empty and the .kube directory doesn't exist too, we have to create the .kube directory and the config file, this cluster was launched using kubeadm, so we need to first copy the kubeconfig from the path '/etc/kubernetes/admin.conf' where kubeadm generated it, to the default config file

networkandcode@k8s-master:~$ mkdir .kube

networkandcode@k8s-master:~$ touch .kube/config

networkandcode@k8s-master:~$ sudo cp /etc/kubernetes/admin.conf .kube/config

kubeconfig should now exist

networkandcode@k8s-master:~$ kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://10.128.0.4:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

We should now be able to retrieve the list of nodes from the API server

networkandcode@k8s-master:~$ kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   61d   v1.15.2
k8s-node1    Ready    <none>   61d   v1.15.2
k8s-node2    Ready    <none>   61d   v1.15.2
k8s-node3    Ready    <none>   61d   v1.15.2

--end-of-post--