<p>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 <strong>The connection to the server localhost:8080 was refused</strong></p>

<p>Here is the setup of troubleshoot</p>

<p>Defined the namespace <strong>dev</strong> and using get verb to display all the kubernetes namespace.</p>

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

<p>Added new context in the Kubernetes cluster.</p>

<pre><code>kubectl config set-context dev  --namespace=dev --user=user1
</code></pre>

<p>I should get an <strong>access denied error</strong> when using the kubectl CLI with this configuration file</p>

<pre><code>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?
</code></pre>

<p>Instead of it's showing <strong>The connection to the server localhost:8080 was refused</strong></p>

<p>Without the <strong>--context</strong> it's works perfectly well</p>

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

<p>Here is the kubernetes config view</p>

<pre><code>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   
</code></pre>

<p>I figured it out, what i noticed when i execute the command </p>

<pre><code>kubectl config view
</code></pre>

<p>The cluster is showing as empty </p>

<pre><code>- context:
    cluster: ""
    namespace: dev
    user: user1
</code></pre>

<p>To fix this issues added the <code>--cluster</code>  information and modified the <code>set-context</code></p>

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

<p>and the context is set properly </p>

<pre><code>contexts:
- context:
    cluster: kubernetes
    namespace: dev
    user: user1
  name: dev
</code></pre>

<p>and i got desired results when looking up the pods with <code>--context=dev</code></p>

<pre><code>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"
</code></pre>
