Kubernetes
kubectl
user management
Kubernetes tutorial
adding users

How to Add Users to Kubernetes kubectl?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Kubernetes is a powerful open-source platform designed to automate the deployment, scaling, and operation of application containers, thereby managing containerized applications across a cluster of machines effectively. One of the common tasks when working with Kubernetes is the addition of new users. This task can include setting up access credentials, defining roles, and configuring permissions. This article provides a detailed guide to adding users to Kubernetes using the kubectl command-line tool.

Understanding Kubernetes Authentication

Kubernetes supports several authentication methods including client certificates, bearer tokens, and OpenID Connect. For managing users, you typically work in conjunction with your organization's existing authentication mechanisms.

Configuring Cluster Access

Before adding users, it is crucial to configure kubectl to access your cluster. This is done with a kubeconfig file that contains cluster information and credentials.

Step-by-Step Guide to Add Users

  1. Create a Certificate and Key for the User: To authenticate users, you can use client certificates. Generate a certificate and a private key for the user:
    Commands:
bash
1    # Generate a private key
2    openssl genrsa -out <username>.key 2048
3
4    # Create a certificate signing request
5    openssl req -new -key <username>.key -out <username>.csr -subj "/CN=<username>"
6
7    # Sign the certificate with your cluster's Certificate Authority
8    openssl x509 -req -in <username>.csr -CA <path-to-ca-cert> -CAkey <path-to-ca-key> -CAcreateserial -out <username>.crt -days 365
  1. Update Kubeconfig with New User Credentials: Add user credentials to your kubeconfig file. Here's an example configuration:
    Example Kubeconfig:
yaml
1    apiVersion: v1
2    kind: Config
3    users:
4    - name: <username>
5      user:
6        client-certificate: <path-to-certificate>
7        client-key: <path-to-key>

Use the kubectl config set-credentials command to add this programmatically:

Command:

bash
    kubectl config set-credentials <username> --client-certificate=<path-to-certificate> --client-key=<path-to-key>
  1. Define User Roles with RBAC: Role-Based Access Control (RBAC) is used to define permissions. Create a role and binding for the user.
    Example Role:
yaml
1    apiVersion: rbac.authorization.k8s.io/v1
2    kind: Role
3    metadata:
4      namespace: default
5      name: pod-reader
6    rules:
7    - apiGroups: [""]
8      resources: ["pods"]
9      verbs: ["get", "watch", "list"]

Example RoleBinding:

yaml
1    apiVersion: rbac.authorization.k8s.io/v1
2    kind: RoleBinding
3    metadata:
4      name: read-pods
5      namespace: default
6    subjects:
7    - kind: User
8      name: <username>
9      apiGroup: rbac.authorization.k8s.io
10    roleRef:
11      kind: Role
12      name: pod-reader
13      apiGroup: rbac.authorization.k8s.io
  1. Authenticate the User: With the user's credentials and the kubeconfig file, the user can authenticate to the cluster using kubectl.

Recap of Key Points

Below is a summary of the key steps and considerations when adding users to Kubernetes:

StepDescription
Certificate GenerationCreate certificates and keys required for user access. Ensure they are signed by the cluster's CA for security.
Kubeconfig UpdateModify the kubeconfig file to include user credentials, or use kubectl config to add users programmatically.
RBAC ConfigurationDefine user roles and permissions using RBAC for restricting access based on user roles.
User AuthenticationAuthenticate the user to the Kubernetes cluster using kubectl with appropriate permissions.

Conclusion

Adding users to Kubernetes involves securely managing certificates and configuring Role-Based Access Control through kubeconfig and RBAC policies. This setup ensures that users have the necessary permissions without compromising on security. By following the systematic approach outlined in this guide, you can efficiently add new users to your Kubernetes clusters, thereby maintaining a secure and organized cloud infrastructure.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.