Learn how to manage access and permissions for your Kubernetes applications by adding roles to nodes in this comprehensive guide.
In Kubernetes, you don't directly assign roles like "master" or "worker" to nodes. Instead, you use labels, which are key-value pairs that help organize and select resources. A common format for node role labels is node-role.kubernetes.io/<role>
. For instance, to designate a node as a worker, you would use the label node-role.kubernetes.io/worker=
. You can view existing node labels using the command kubectl get nodes --show-labels
. If you see <none>
under "ROLES," it signifies that no role labels are currently assigned. Tools like kubeadm
might automatically add some role labels during the cluster creation process. It's crucial to understand that while you can label nodes for roles, the actual components running on a node, such as kubelet
and kube-proxy
, determine its functionality. Simply adding a role label doesn't automatically install the necessary components. You need to configure those separately based on your desired node type, whether it's a control plane node, a worker node, or another type.
In Kubernetes, you don't directly assign roles to nodes like "master" or "worker." Instead, you use labels.
Labels are key-value pairs that help you organize and select resources.
A common label format for node roles is node-role.kubernetes.io/<role>
.
For example, to mark a node as a worker, you'd use:
kubectl label node <node-name> node-role.kubernetes.io/worker=
You can check existing node labels with:
kubectl get nodes --show-labels
If you see <none>
under "ROLES," it means no role labels are assigned yet.
Tools like kubeadm
might automatically add some role labels during cluster creation.
While you can label nodes for roles, the actual components running on a node (like kubelet
, kube-proxy
) determine its functionality.
Important: Simply adding a role label doesn't magically install necessary components. You need to configure those separately based on your desired node type (control plane, worker, etc.).
This code demonstrates how to label nodes in a Kubernetes cluster for different roles, such as "control-plane" and "worker". It shows how to list existing nodes and their labels, add new labels to specific nodes using kubectl command, and verify the updated labels. The example emphasizes that labeling is just for identification and additional configuration is required for the nodes to function in their designated roles.
This example demonstrates how to label nodes in a Kubernetes cluster for different roles.
1. List existing nodes and their labels:
kubectl get nodes --show-labels
Output (example):
NAME STATUS ROLES AGE VERSION LABELS
node1 Ready <none> 24h v1.25.4 kubernetes.io/hostname=node1, ...
node2 Ready <none> 24h v1.25.4 kubernetes.io/hostname=node2, ...
node3 Ready <none> 24h v1.25.4 kubernetes.io/hostname=node3, ...
2. Label node1
as a "control-plane" node:
kubectl label node node1 node-role.kubernetes.io/control-plane=
3. Label node2
and node3
as "worker" nodes:
kubectl label node node2 node-role.kubernetes.io/worker=
kubectl label node node3 node-role.kubernetes.io/worker=
4. Verify the updated labels:
kubectl get nodes --show-labels
Output (example):
NAME STATUS ROLES AGE VERSION LABELS
node1 Ready control-plane 24h v1.25.4 kubernetes.io/hostname=node1, node-role.kubernetes.io/control-plane=, ...
node2 Ready worker 24h v1.25.4 kubernetes.io/hostname=node2, node-role.kubernetes.io/worker=, ...
node3 Ready worker 24h v1.25.4 kubernetes.io/hostname=node3, node-role.kubernetes.io/worker=, ...
Important Notes:
kubelet
, kube-proxy
, kube-apiserver
, etc.) on each node based on its intended role.kubeadm
might automatically label nodes during cluster creation. Check your tool's documentation for details.This code example provides a practical demonstration of how to use labels to identify node roles in a Kubernetes cluster. Remember that labeling is just the first step; you need to configure the necessary components separately to ensure your nodes function as intended.
node-role.kubernetes.io/worker=
.kubectl get nodes -l <label-key>
to filter and manage nodes based on labels effectively.Feature | Description |
---|---|
Node Roles | Kubernetes doesn't use fixed roles like "master" or "worker." Instead, it relies on labels for flexibility. |
Labels | Key-value pairs used to organize and select resources, including nodes. |
Role Label Format |
node-role.kubernetes.io/<role> , e.g., node-role.kubernetes.io/worker
|
Labeling Nodes | Use kubectl label node <node-name> <label>=
|
Checking Labels | Use kubectl get nodes --show-labels
|
No Roles Assigned | Indicated by <none> under "ROLES" column. |
Automatic Labeling | Tools like kubeadm might add role labels during cluster setup. |
Labels vs. Functionality | Labels help organize, but the components running on a node (like kubelet , kube-proxy ) determine its actual role. |
Important Note | Adding a role label doesn't install necessary components. You need to configure them separately based on the desired node type. |
In conclusion, while Kubernetes doesn't have rigid node roles, labels provide a flexible and powerful mechanism to categorize and manage nodes. By using labels effectively, you can organize your cluster, influence Pod scheduling, and integrate with other Kubernetes features like taints and tolerations. However, remember that labels are primarily for identification and selection; you must configure the necessary components on each node to ensure they fulfill their intended roles within the cluster.