overexchange
overexchange

Reputation: 1

kubectl command for talking to multiple physical clusters

This is my ~/.kube/config file:

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

My understanding is, cluster-1 & cluster-2 are kubernetes physical clusters (Control Plane).

Each physical cluster has multiple virtual clusters (Namespaces)

If my understanding is correct, then with the above kubeConfig, What is the kubectl syntax to get all the namespaces in cluster?

Upvotes: 1

Views: 912

Answers (3)

Ali
Ali

Reputation: 1495

Hope this answer helps you:

Manage multiple clusters with Contexts

Let's say we have multiple clusters to administrator, so we have multiple kubeconfig file.

But it's not so efficent to use --kubeconfig option everytime with our kubectl command!


Access multiple clusters using Contexts

  • Define all the clusters and users in the 1 kubeconfig file
  • Define a context for each cluster
  • We can switch between clusters using these contexts
  • No need to specify kube konfig file

What is a Contexts?

In a kubeconfig file, we have:

  • List of K8s clusters
  • List of K8s users
  • Names to reference them inside the kubeconfig file
  • And also we have Context

Context

  • Combination of which user should access which cluster
  • Or "Use the credentials of the kubernetes-admin user to access the kubernetes cluster"
  • We interact with it via either:
    • Update kubeconfig manually
    • or Use kubectl config commands

  • How to switch the context?
    kubectl config use-context <CONTEXT-NAME>
    
  • Display list of contexts
    kubectl config get-context
    
  • Display the current-context
    kubectl config gcurrent-context
    

Namespaces in Contexts

Each context consists actually 3 components

  • cluster
  • user
  • namespace
  • By default, the default namespace is configured
  • Other than default namespace, we need to define them

Lets say most of the time, we work with 1 specific namespace (other than default) and its kind of annoying to use --namespace for each kubectl command...

  • Switch default namespace
    kubectl config set-context --current --namespace kube-system
    
  • Now check the ~/.kube/config file
    contexts:
    - context:
        cluster: kubernetes
        namespace: kube-system # Just added!
        user: kubernetes-admin
      name: kubernetes-admin@kubernetes
    

Upvotes: 1

Adiii
Adiii

Reputation: 60134

short answer, you can get all ns across the existing cluster in kubeconfig

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

you can get all namespace from each cluster using below command (current context)

kubectl get namespace

the above will return namespace 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

A context element in a kubeconfig file is used to group access parameters under a convenient name. Each context has three parameters: cluster, namespace, and user. By default, the kubectl command-line tool uses parameters from the current context to communicate with the cluster.

organize-cluster-access-kubeconfig-context

A namespace is simply the isolation of the resources. for example

you can not create two deployments with the same name in a single namespace, because those resources are namespace scoped.

so you can deploy multiple deployment under develop, stage and production namespace.

enter image description here

kubernetes-namespaces

Upvotes: 2

Pavol Krajkovič
Pavol Krajkovič

Reputation: 540

In kubernetes there is no such thing as physical or virtual cluster. Kubeconfig consists of three parts.

  • clusters
  • users
  • contexts
  1. clusters - k8s clusters made of different VMs/on-prem nodes
  2. users - users that have access to the cluster, it can be the kube-admin or a normal developer. user can have roles that define what resources can the user manipulate (RBAC)
  3. contexts - a link between one cluster and one user, because you work with one cluster as a user.

Now for namespaces, they work like linux network namespaces. think of it as a house with a family living inside. If you have a Bob Newman in the house you living in, you would simply call him Bob. If the bob would be living in another house, you would reference him as Bob Newman. Namespaces logically split resources inside the cluster. You can have e.g. monitoring namespace, payroll namespace, backend namespace. The house has different

Upvotes: 1

Related Questions