As organizations increasingly adopt containerization and microservices-based architectures, Kubernetes has emerged as the de facto standard for orchestrating containerized workloads. While Kubernetes offers a multitude of benefits, it also introduces new security challenges that need to be addressed.
In this article, we will explore three Kubernetes security best practices that help you secure your Kubernetes deployments and protect your sensitive data and applications from potential security threats.
By following these best practices, organizations can help ensure that their Kubernetes deployments remain secure and that their applications and data are protected.
So here they are, three of our Kubernetes security best practices:
1. Use role-based access control (RBAC)
One of the fundamental security mechanisms in Kubernetes is role-based access control (RBAC). RBAC provides a flexible and granular approach to controlling access to Kubernetes resources, mitigating risks, and safeguarding sensitive data by limiting user and service accounts to the minimum required permissions.
RBAC allows administrators to control access to Kubernetes resources based on the roles and permissions of individual users or groups. This helps to prevent unauthorized access to sensitive data or resources, which is a critical security concern in any environment.
Benefits
RBAC helps to prevent privilege escalation attacks, where an attacker gains access to a low-privileged account and then escalates their privileges to gain access to sensitive data or resources. By limiting the permissions of each user or pod to only what is necessary to perform their tasks, RBAC reduces the risk of privilege escalation and also helps to mitigate the risk of insider threats, where a trusted user intentionally or unintentionally causes harm to the system.
Lastly, there is scalability. RBAC helps to manage access control in large and complex Kubernetes environments. By providing a flexible and granular system for managing roles and permissions, RBAC makes it easier to manage access control in large-scale deployments.
Implementation
To implement RBAC you can follow these measures:
Define Roles for different needs. A developer needs different permissions than a CI/CD pipelines service account. A project manager has a completely different required set of permissions than a DevOps engineer concerned with managing the Kubernetes cluster itself. A baseline role set could have the following roles:
Administrator: An administrator role can be defined to grant full access to Kubernetes resources, including the ability to create, modify, and delete resources.
Developer: A developer role can be defined to grant access to Kubernetes resources required for application development, such as pods, services, and deployments.
Viewer: A Viewer role in Kubernetes grants read-only access to resources, enabling users to view the status and configuration of resources without the ability to make any modifications.
Service Account: A service account role can be defined to grant access to Kubernetes resources required for running a specific application or service. This role is meant for applications or services that need to interact with Kubernetes in the context of CI/CD.
When creating a new Kubernetes cluster, make sure to follow the principle of least privilege and only grant the minimum required permissions to the service accounts and users that need access. For example, if you only need read-only access to the cluster for monitoring purposes, create a separate service account with read-only permissions rather than using the cluster-admin account.
RBAC allows you to specify granular permissions for users and service accounts in your cluster.
2. Network Segmentation
In the event that an attacker could successfully infiltrate a service within your cluster, it is key to prevent escalation to broader access to different services. Network segmentation tries to limit this influence.
Network segmentation is the practice of dividing a network into smaller logical parts, or segments. Each segment acts as an independent network, isolated by boundaries or restrictions on communication between them. This approach improves security and performance by controlling how traffic flows across the network. By implementing network segmentation, organizations can limit the damage caused by cybersecurity attacks, as unauthorized access is restricted to specific segments. It also allows for better monitoring and reduces the threat posed by insider threats. Kubernetes offers features to achieve this with network policies as part of the networking.k8s.io/v1 API.
Benefits
Network policies are a crucial security control that can be implemented in Kubernetes clusters to minimize attack surfaces, prevent unauthorized access to sensitive data, and enforce least-privilege access between cluster components. Kubernetes’ network segmentation feature makes it possible to isolate various network components, such as distinct levels of a multi-tier application or sensitive data, lowering the attack surface and limiting the potential damage from a successful attack.
Implementation
To enable network segmentation in your Kubernetes cluster, you can use a network plugin like Calico or Cilium.
Solutions for network Segmentation (https://owasp.org/www-project-kubernetes-top-ten/2022/en/src/K07-network-segmentation)
- Simplest implementation is an ingress/egress policy for every running application (Pod). This is however a lot of work and management effort finished solutions are available:
- Rancher ([Additional Steps for Project Network Isolation | Rancher Manager](https://ranchermanager.docs.rancher.com/integrations-in-rancher/istio/configuration-options/project-network-isolation)): does nothing special but creating egress/ingress network policies for every namespace
To use Network Policies, you have to use a CNI (Container Network Interface) that supports them, here is a selection of ones that do:
- Cilium – Linux Native, API-Aware Networking and Security for Containers: https://cilium.io/
- Weave Net: Network Containers Across Environments | Weaveworks: https://www.weave.works/oss/net/
- Kube-Router : Turnkey Kubernetes networking solution: https://www.kube-router.io/
Also use network policies to specify allowed communication between resources within the cluster.
Network policies allow you to specify which resources within a namespace are allowed to communicate with each other and with resources in other namespaces. This can help prevent unauthorized access and data exfiltration.
3. Enable and properly configure auditing to track changes to the cluster and identify security incidents
Security incidents can happen, and not all incidents correlate with outages, but it is important to be able to identify them and react to them. Auditing is the foundation for this.
Auditing is a crucial security control that can help you identify security incidents and track changes to your cluster. By enabling auditing, you can monitor the activity of users and service accounts in your cluster and identify potential security threats.
Benefits
Tracking changes to your cluster allows you to identify potential security threats and take action before they become a problem. For example, if a user or service account makes unauthorized changes to your cluster, you can identify the change and take action to prevent further damage.
Besides the security aspect, auditing is also a crucial part of compliance. Many regulations require organizations to track changes to their systems and identify potential security threats.
Implementation
To enable auditing in your Kubernetes cluster, you can add the following configuration to your audit policy.
First, we need to ensure that the API server is started with the following flags:
– –audit-policy-file=/etc/kubernetes/audit-policy.yaml
– –audit-log-path=/var/log/kubernetes/audit/audit.log
The audit-policy file is where we configure what audit events we want to log and in how much detail. In our example, the file looks like (this is NOT a manifest we can apply to the API server via kubectl, this is the file referenced by the config flag –audit-policy-file we configured before):
—
apiVersion: audit.k8s.io/v1
kind: Policy
rules:
– level: RequestResponse
resources:
– group: apps
resources: [“replicasets”]
resourceNames: [“nginx-rs”]
Conclusion
In conclusion, securing Kubernetes is critical for organizations to ensure the confidentiality, integrity, and availability of their data and applications. As Kubernetes becomes more widely adopted, it has become a prime target for attackers looking to exploit security vulnerabilities. The eight Kubernetes security best practices outlined in this whitepaper provide a comprehensive guide for organizations to secure their Kubernetes deployments and protect against potential security threats. By implementing these best practices, organizations can reduce the risk of security breaches and ensure the security of their Kubernetes clusters. It is important to note that Kubernetes security is an ongoing process that requires continuous monitoring and improvement to stay ahead of evolving security threats. By remaining vigilant and implementing these best practices, organizations can leverage the full benefits of Kubernetes while maintaining a secure and resilient infrastructure.
We hope these best practices and example configurations are helpful for securing your Kubernetes cluster.
##
ABOUT THE AUTHOR
Valentin Knabel, Professional Software Engineer Go / Kubernetes, metalstack.cloud
Teamplayer, Open Source, Developer tools, CI/CD, Agile.





