Above, is one of the most common error we get whenever we install a new K8 cluster or start working on one deployed by other users.
First thing first, this error is not version specific and also doesn’t imply something is wrong with your configuration. (So please don’t send that mail you wrote for your kubernetes administrator)
Here what it looks like
[centos@cntmaster ~]$ kubectl get pods The connection to the server localhost:8080 was refused - did you specify the right host or port?
Now when I said its not anyone’s fault actually, reason is pretty simple. Just check the call flow here. I logged in my cntmaster system as centos user and tried to call kube apiserver on default address “localhost:8080”.
Now connection was refused, so either I am specifying wrong address or my kube-apiserver container is down. Checking sceond scenario ist,
[centos@cntmaster ~]$ sudo docker ps | grep kube-apiserver e60470040c84 b6d7abedde39 "kube-apiserver --ad…" 33 minutes ago Up 33 minutes 61ef28650ca k8s.gcr.io/pause:3.6 "/pause" 33 minutes ago Up 33 minutes
So kube-apiserver is up, next we verify address. Now to get your k8 cluster’s server adress you may check admin.conf file either at /etc/kubernetes loction or your cluster home directory.
For me, while installing k8 cluster via kubeadm, I was logged in as root and I copied admin.conf file to newly created $HOME/.kube directory. Since I was doing all these steps via root, my k8 cluster home directory with config file was created in /root.
[root@cntmaster centos]# ls -al /root/.kube/ total 8 drwxr-xr-x 3 root root 33 Jan 14 04:47 . dr-xr-x---. 3 root root 148 Feb 3 02:35 .. drwxr-xr-x 4 root root 35 Jan 14 04:47 cache -rwxr-xr-x 1 root root 5641 Jan 14 04:39 config
I read my config file to find out cluster IP and port on which k8 is listening.
[root@cntmaster centos]# cat /etc/kubernetes/admin.conf apiVersion: v1 clusters: - cluster: server: https://192.168.18.15:6443 name: kubernetes contexts: - context: cluster: kubernetes user: kubernetes-admin name: kubernetes-admin@kubernetes current-context: kubernetes-admin@kubernetes
Verifying my cluster listening ip and port, next I need to check why kubectl command cannot read this config file and send api call to correct cluster.
Finally, issue was identified in improper rights to read file. Like I said before, I created my home directory in /root and gave permission to admin.conf file for root user and group only.
While I am trying to run kubecetl command from centos normal user which don’t have permission to read config file.
[centos@cntmaster ~]$ cd /root/.kube -bash: cd: /root/.kube: Permission denied
To resolve issue, I created new .kube directory with centos user and copied config file from /etc/kubernetes
[centos@cntmaster ~]$ mkdir -p $HOME/.kube [centos@cntmaster ~]$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config [centos@cntmaster ~]$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
Now kubectl can read config file and send http calls to apiserver on correct cluster IP and port.
[centos@cntmaster ~]$ kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE calico-kube-controllers-5b869f6dcc-cchb6 1/1 Running 2 (101m ago) 19d calico-node-fvmlz 0/1 Running 2 (101m ago) 19d calico-node-s46w4 0/1 Running 0 19d coredns-64897985d-7bphf 1/1 Running 2 (101m ago) 19d coredns-64897985d-mdqgv 1/1 Running 2 (101m ago) 19d etcd-cntmaster 1/1 Running 2 (101m ago) 19d kube-apiserver-cntmaster 1/1 Running 2 (101m ago) 19d kube-controller-manager-cntmaster 1/1 Running 2 (101m ago) 19d kube-proxy-2pm8f 1/1 Running 0 19d kube-proxy-85xm8 1/1 Running 2 (101m ago) 19d kube-scheduler-cntmaster 1/1 Running 2 (101m ago) 19d