<p>This is my <code>~/.kube/config</code> file:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
clusters:
- cluster:
    server: https://192.168.10.190:6443
  name: cluster-1
- cluster:
    server: https://192.168.99.101:8443
  name: cluster-2
contexts:
- context:
    cluster: cluster-1
    user: kubernetes-admin-1
  name: cluster-1
- context:
    cluster: cluster-2
    user: kubernetes-admin-2
  name: cluster-2
kind: Config
preferences: {}
users:
- name: kubernetes-admin-1
  user:
    client-certificate: /home/user/.minikube/credential-for-cluster-1.crt
    client-key: /home/user/.minikube/credential-for-cluster-1.key
- name: kubernetes-admin-2
  user:
    client-certificate: /home/user/.minikube/credential-for-cluster-2.crt
    client-key: /home/user/.minikube/credential-for-cluster-2.key
</code></pre>
<hr />
<p>My understanding is, <code>cluster-1</code> &amp; <code>cluster-2</code> are kubernetes physical clusters (<code>Control Plane</code>).</p>
<p>Each physical cluster has multiple virtual clusters (<code>Namespaces</code>)</p>
<p>If my understanding is correct, then with the above <code>kubeConfig</code>, What is the <code>kubectl</code> syntax to <strong>get all the namespaces in cluster</strong>?</p>

<p>short answer, you can get all ns across the existing cluster in <code>kubeconfig</code></p>
<pre><code>for context in $(kubectl config view -o jsonpath='{.clusters[*].name}'); do 
kubectl config use-context $context ;
kubectl get ns;
done
#or
for context in $(kubectl config view -o jsonpath='{.clusters[*].name}'); do kubectl config use-context $context ;kubectl get ns;done
</code></pre>
<p>you can get <strong>all namespace</strong> from each cluster using below command <strong>(current context)</strong></p>
<pre><code>kubectl get namespace
</code></pre>
<p>the above will return <code>namespace</code> in the current context, so you have two cluster, its mean you will need two different context to get all the namespace from both cluster</p>
<blockquote>
<p>A context element in a kubeconfig file is used to group access parameters under a convenient name. Each context has three parameters: <strong>cluster, namespace, and user</strong>. By default, the kubectl command-line tool uses parameters from the <strong>current context to communicate with the cluster</strong>.</p>
</blockquote>
<p><a href="https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#context" rel="nofollow noreferrer">organize-cluster-access-kubeconfig-context</a></p>
<p>A namespace is simply the <strong>isolation</strong> of the resources. for example</p>
<p>you can not create <strong>two deployments</strong> with the same name in a single namespace, because those resources are <strong>namespace scoped</strong>.</p>
<p>so you can deploy multiple deployment under <code>develop</code>, <code>stage</code> and <code>production</code> namespace.</p>
<p><a href="https://i.stack.imgur.com/EXoY6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EXoY6.png" alt="enter image description here" /></a></p>
<p><a href="https://belowthemalt.com/2022/04/09/kubernetes-namespaces/" rel="nofollow noreferrer">kubernetes-namespaces</a></p>
