Search code examples
kubernetesdevopsalibaba-cloud

kubernetes localhost:8080 was refused with context


The Kubernets cluster is setup using Alibaba container service, there is no Issue with accessing the cluster using root account, when a new namespace is created and user is added to that namespace, it throws the error The connection to the server localhost:8080 was refused

Here is the setup of troubleshoot

Defined the namespace dev and using get verb to display all the kubernetes namespace.

root@kube-master:# kubectl get namespaces
NAME          STATUS    AGE
default       Active    14d
dev           Active    56m
kube-public   Active    14d
kube-system   Active    14d

Added new context in the Kubernetes cluster.

kubectl config set-context dev  --namespace=dev --user=user1

I should get an access denied error when using the kubectl CLI with this configuration file

root@kube-master:/home/ansible# kubectl --context=dev get pods
The connection to the server localhost:8080 was refused - did you specify the right host or port?

Instead of it's showing The connection to the server localhost:8080 was refused

Without the --context it's works perfectly well

root@kube-master:# kubectl get pods -n dev
NAME      READY     STATUS    RESTARTS   AGE
busybox   1/1       Running   1          1h

Here is the kubernetes config view

root@kube-master:/home/ansible# kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: REDACTED
    server: https://172.16.2.13:6443
  name: kubernetes
contexts:
- context:
    cluster: ""
    namespace: dev
    user: user1
  name: dev
- 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

root@kube-master:# kubectl config get-contexts
CURRENT   NAME                          CLUSTER      AUTHINFO           NAMESPACE
          dev                                        user1              dev
*         kubernetes-admin@kubernetes   kubernetes   kubernetes-admin   

Solution

  • I figured it out, what i noticed when i execute the command

    kubectl config view
    

    The cluster is showing as empty

    - context:
        cluster: ""
        namespace: dev
        user: user1
    

    To fix this issues added the --cluster information and modified the set-context

    root@kube-master:/home/ansible# kubectl config set-context dev --cluster=kubernetes --namespace=dev --user=user1
    Context "dev" modified.
    

    and the context is set properly

    contexts:
    - context:
        cluster: kubernetes
        namespace: dev
        user: user1
      name: dev
    

    and i got desired results when looking up the pods with --context=dev

    root@kube-master:/home/ansible# kubectl --context=dev get pods
    No resources found.
    Error from server (Forbidden): pods is forbidden: User "system:anonymous" cannot list pods in the namespace "dev"