Managing Access to Kubernetes Clusters for Engineering Teams
Tags: kubernetes
Published: 2022-02-07 17:33:50

---
title: Managing Access to Kubernetes Clusters for Engineering Teams
published: true
description: A look at how to securely provide access for engineers who use Kubernetes clusters.
tags: Kubernetes
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7oiku5mxtu1tq89b3to8.jpg
canonical_url: https://loft.sh/blog/managing-access-to-kubernetes-clusters-for-engineering-teams/
---
by Daniel Olaogun

[Kubernetes](https://kubernetes.io/) is a container orchestration tool for managing, deploying, and scaling containerized applications. It helps engineering teams deploy and manage applications across multiple servers with fewer complexities. Some of its best-known features include: 

* [Self-healing](https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#how-a-replicationcontroller-works), which automatically restarts your application container if it crashes
* [Horizontal scaling](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/) your application up or down as the traffic load increases or decreases
* [Automatic rollouts and rollbacks](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/) for gradually deploying an updated version of your application or quickly rolling back to the previous version if you detect an issue 

In Kubernetes, your containerized application is abstracted by a pod that can be replicated in a node or across many nodes. Nodes run your containerized applications and can contain one or multiple pods depending on the node resources. A set of nodes is called a cluster. 

As your Kubernetes cluster grows, you may need help managing it. This means you’ll need the ability to add users to your cluster and provide the required permissions, among other tasks. In this article, you’ll learn how to manage access to your Kubernetes cluster as well as how to manage the users who are given this access.

## Why You Need Kubernetes Clusters

A Kubernetes cluster contains control plane and worker nodes that work together to handle and distribute traffic from within and outside the cluster. Worker nodes are a set of virtual or physical machines that run your containerized applications, while the control plane node controls the worker nodes. The control plane node manages and maintains the desired state of the cluster, scheduling pods to worker nodes based on available resources. It also provides the API endpoint that users interact with.

The following are some important use cases for Kubernetes clusters.

### Provisioning of Multiple Environments

During the development and release of your application, you need environments for development and testing as well as a separate production environment for the application release. A Kubernetes cluster provides these environments using [namespaces](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/). 

Previously, managing multiple environments for a single application meant spinning up separate virtual machines for each environment. This was tedious, and managing the differences between the environments could be difficult. Kubernetes clusters simplify the process. 

### Running Multiple Deployments

Kubernetes clusters allow you to run multiple deployments of your applications, for example development, testing, and production deployments of the same application, in one cluster. You can also deploy multiple applications with different functionalities. However, these deployments will share the resources provided in the cluster.

### Easy Scaling of Deployments

As your application traffic increases, the application consumes more resources. Increasing its resources ensures that the increased traffic won’t cause downtime. With Kubernetes, you can scale your application deployments by replicating them on multiple nodes in your cluster. This distributes the incoming traffic load across all the nodes running your application in the cluster.

## Managing Access in Your Kubernetes Cluster

As your organization grows and the number of deployments in your Kubernetes cluster skyrockets, you need more users to help manage the cluster. However, you should also ensure that you can effectively manage the access of those users.

Access to the Kubernetes API is managed through authentication, authorization, and admission control. When a user makes a request through the API using a client such as kubectl, Kubernetes checks the user’s authenticity. If the user can’t be verified, the request is rejected; if Kubernetes can verify the user, the request moves to authorization, which confirms that the user has the required permission to initiate this request. If they do not, the request is rejected, but if the user has the right permissions, then the request moves to the final verification of admission control. Admission control ensures that the request is good (such as verifying that the container image you want to deploy is secure).

### Granting Users Access

Other users must be recognized by Kubernetes in order to connect with your cluster. However, Kubernetes does not provide the ability to manage users out of the box. Users are generally managed outside of Kubernetes using services like Microsoft Active Directory, Okta OpenID Connect, AWS Identity and Access Management, and [Loft](https://loft.sh/).

Managed Kubernetes services such as [Azure Kubernetes Service](https://docs.microsoft.com/en-us/azure/aks/managed-aad) (AKS), [Amazon Elastic Kubernetes Service](https://docs.aws.amazon.com/eks/latest/userguide/add-user-role.html) (Amazon EKS), and [Google Kubernetes Engine](https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control) (GKE) incorporate their identity management system with Kubernetes services to manage and authenticate users.

If you have a self-managed Kubernetes cluster, there are multiple services available to manage user access:

* [Single sign-on](https://loft.sh/docs/getting-started/single-sign-on) with [Loft](https://loft.sh)
* [OpenID Connect](https://developer.okta.com/blog/2021/11/08/k8s-api-server-oidc) with [Okta](https://www.okta.com/)
* [Kubernetes authentication](https://dexidp.io/docs/kubernetes/) through [Dex](https://dexidp.io/)
* [Access management](https://medium.com/swlh/how-we-effectively-managed-access-to-our-kubernetes-cluster-38821cf24d57) using [OpenSSL](https://kubernetes.io/docs/tasks/administer-cluster/certificates/#openssl); note that you should send the `kube-config` file in a secure and encrypted manner to your authenticated users

You can also use the above options to handle user access for managed Kubernetes services. The Kubernetes documentation provides other [authentication strategies](https://kubernetes.io/docs/reference/access-authn-authz/authentication/) for authenticating users in your cluster.

### Managing Access

Once users have been granted access to your Kubernetes cluster, there are several strategies to best manage that access.

#### Providing Only Needed Permissions

Once you have successfully authenticated the users required in your cluster, give them just enough permissions to perform their duties. Depending on your team structure, it is not always a good idea for all users to have the same high-level access. Otherwise, a user might perform an operation without understanding its consequences, or you might lose access to your admin rights because your user privileges were changed by a malicious user.

There are different [authorization modes](https://kubernetes.io/docs/reference/access-authn-authz/authorization/#authorization-modules) in Kubernetes used for access control, including role-based access control (RBAC), attribute-based access control (ABAC), node, and webhook. However, RBAC is commonly used to implement user roles and permissions. For more information on how to implement RBAC in your Kubernetes cluster, check out the [official documentation](https://kubernetes.io/docs/reference/access-authn-authz/rbac/).

#### Decommissioning Users as Needed

When a user is no longer a part of your cluster team, you should delete the user from the cluster. This prevents the user from continuing to access the cluster and performing unauthorized activities.

Most of the user management platforms noted above allow you to remove users with ease. 

#### Enabling Auditing

Kubernetes auditing logs all actions performed in your cluster sequentially. Auditing your cluster gives you this information:

* What happened?
* When did it happen?
* Who initiated it?
* On what did it happen?
* Where was it observed?
* From where was it initiated?
* Where was it going?

When something unexpected happens in your Kubernetes cluster, the logs generated by the audit will guide you in getting to the root cause.

Kubernetes does not enable auditing by default; you must enable it yourself. Doing so is up to you and your team, but for the security of your Kubernetes cluster, [enabling auditing](https://kubernetes.io/docs/tasks/debug-application-cluster/audit/) is recommended.

## Using Loft for Access Control

[Loft](https://loft.sh/) is a platform built on top of Kubernetes that adds multitenancy and self-service capabilities, enabling you to control and manage access in your Kubernetes cluster. As previously noted, you can integrate Loft into your cluster to handle authentication and access control. Loft also integrates with [many single sign-on (SSO) providers](https://loft.sh/docs/getting-started/single-sign-on) that you can use with your cluster.

In Kubernetes, non-admin users don’t have the privileges to list, create, or delete namespaces in a shared cluster. However, Loft offers a feature called [spaces](https://loft.sh/docs/spaces/spaces), a virtual abstraction of a Kubernetes namespace. Once a space is created, a corresponding namespace is created, and if the space is deleted, the namespace is deleted as well.

Loft also offers an auditing section similar to Kubernetes auditing, which records all operations and actions performed by users and applications using the Loft API in your Kubernetes cluster.

## Best Practices

Remember that when configuring access in your Kubernetes cluster, there are some best practices you should follow: 

1. Follow the principle of least privilege
2. Enable auditing in your cluster
3. Routinely check roles and permissions assigned to users
4. Remove users that are no longer relevant in your cluster

## Conclusion

As you’ve learned, you have multiple options for granting and managing user access in your Kubernetes cluster, whether your cluster is self-managed or managed by a cloud provider. It’s important that you provide the right level of access to your different users and revoke that access when necessary. This way, you can ensure that your cluster is safe from misuse as you scale up your Kubernetes workflow.

If you need a third-party solution for managing access control and self-service in your Kubernetes cluster, consider [Loft](https://loft.sh/). It integrates well with cloud-native tools and can be used with kubectl or GitOps. Loft is easy to implement and offers several cost optimization features. You can [request a demo](https://loft.sh/demo/request) to see what Loft can do for you.

*Photo by [Matt Seymour](https://unsplash.com/@mattseymour?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/walls?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)*