Open In App

How to Add Roles to Nodes in Kubernetes?

Last Updated : 08 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Kubernetes (or k8) is an open-source container orchestration platform that helps in managing various containers. Its architecture consists of nodes that represent worker machines and the containers run inside those machines. The machines, or nodes, may require some roles attached to them as well in order to perform some action or designate their function. In this article, we will learn how to add roles to nodes in Kubernetes.

Steps to Add Roles to Nodes

Step 1: Creating and defining roles using YAML files

In this step, we will create a ClusterRole that specifies or lists permissions for an entire cluster. In this example, we will create a clusterrole, that allows reading nodes, to the users.

Filename: cluster role.yaml

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: node-reader
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get", "list"]

Step 2: Binding roles to nodes with RoleBindings and ClusterRoleBindings

Now, we will bind the role we defined above to a user using the ClusterRoleBindings. In this example, we will bind the "node-reader" cluster role to the user "dishebh".

Filename: clusterrolebinding.yaml

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: node-reader-binding
subjects:
- kind: User
name: dishebh
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: node-reader
apiGroup: rbac.authorization.k8s.io

Step 3: Applying the configuration using kubectl

To apply and set the configurations mentioned in the above steps, we will use the kubectl CLI commands.

To apply the ClusterRole, use the below command:

kubectl apply -f clusterrole.yaml

To apply the ClusterRoleBinding, use the below command:

kubectl apply -f clusterrolebinding.yaml

Step 4: List all the nodes in the cluster

kubectl get nodes

Output:

file

Step 5: Assign roles to nodes

kubectl label nodes minikube node-role.kubernetes.io/master=

Step 6: Verify the roles added

kubectl get nodes --show-labels

Output:

image

Step 7: Adding custom roles to nodes

kubectl label nodes node3 node-role.kubernetes.io/database=

Output:

image

    Next Article
    Article Tags :

    Similar Reads