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?
CodePudding user response:
In kubernetes there is no such thing as physical or virtual cluster. Kubeconfig consists of three parts.
- clusters
- users
- contexts
- clusters - k8s clusters made of different VMs/on-prem nodes
- 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) - 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
CodePudding user response:
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
- Update
- 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
filecontexts: - context: cluster: kubernetes namespace: kube-system # Just added! user: kubernetes-admin name: kubernetes-admin@kubernetes