Reputation: 1
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
Reputation: 1495
Hope this answer helps you:
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
kubeconfig
fileIn a kubeconfig file, we have:
kubeconfig
fileContext
Context
kubeconfig
manuallykubectl config
commandskubectl config use-context <CONTEXT-NAME>
kubectl config get-context
current-context
kubectl config gcurrent-context
Each context consists actually 3 components
default
namespace is configuredLets 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...
kubectl config set-context --current --namespace kube-system
~/.kube/config
file
contexts:
- context:
cluster: kubernetes
namespace: kube-system # Just added!
user: kubernetes-admin
name: kubernetes-admin@kubernetes
Upvotes: 1
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.
Upvotes: 2
Reputation: 540
In kubernetes there is no such thing as physical or virtual cluster. Kubeconfig consists of three parts.
kube-admin
or a normal developer. user can have roles that define what resources can the user manipulate (RBAC)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