Set a Kubernetes Namespace that Does Not Interact with Other Customer Namespaces

Ramesh Babu Chayapathi
3 min readOct 8, 2024

--

As Kubernetes is increasingly used to manage multi-tenant environments, ensuring that one tenant’s resources don’t interact with or interfere with another tenant’s resources becomes crucial. A namespace is the ideal method to isolate resources, but this alone is not enough to guarantee no interaction between namespaces. By following the steps below, you can achieve effective namespace isolation in Kubernetes.

1. Create a Separate Namespace

The first and most straightforward step to isolating customer workloads is by creating a dedicated namespace. A namespace serves as a virtual cluster that provides a separation of resources within the Kubernetes cluster.

kubectl create namespace <customer-namespace>

This command creates a logical boundary for the customer, ensuring that their resources (pods, services, config maps, etc.) are not mixed with those of other customers.

2. Implement Network Policies

Kubernetes Network Policies help enforce network isolation between namespaces. By default, Kubernetes allows all pods to communicate with each other. Network policies can restrict this communication based on rules.

For example, the following network policy ensures that no pod in the customer’s namespace can communicate with pods outside the namespace:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-cross-namespace
namespace: <customer-namespace>
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector: {}
egress:
- to:
- podSelector: {}

This policy allows only intra-namespace traffic and blocks any communication with pods from other namespaces unless explicitly allowed.

3. Leverage Role-Based Access Control (RBAC)

Implementing RBAC ensures that only the required users and service accounts can access a particular namespace. It restricts unauthorized access and prevents cross-namespace access by other tenants.

For example, to give a specific user view-only access to a namespace, use the following RoleBinding:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: restrict-access
namespace: <customer-namespace>
subjects:
- kind: User
name: customer-user
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: view-only
apiGroup: rbac.authorization.k8s.io

By managing access at the namespace level, you reduce the risk of accidental or malicious interference.

4. Set Resource Quotas and Limits

Another way to ensure isolation is by setting Resource Quotas. This limits the amount of CPU, memory, and storage resources that can be used by a namespace, ensuring that one tenant does not monopolize cluster resources.

Here’s an example resource quota:

apiVersion: v1
kind: ResourceQuota
metadata:
name: customer-quota
namespace: <customer-namespace>
spec:
hard:
requests.cpu: "4"
requests.memory: "8Gi"
limits.cpu: "8"
limits.memory: "16Gi"

This ensures that no namespace can exceed the specified quota, providing resource fairness among different namespaces.

5. Use Pod Security Policies and Security Contexts

To further isolate workloads at the pod level, you can apply Pod Security Policies (PSPs) or security contexts. These policies prevent pods in one namespace from escalating privileges or accessing host resources, which is critical for preventing namespace interaction.

Example Pod Security Policy:

apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: restricted
spec:
privileged: false
runAsUser:
rule: MustRunAsNonRoot
allowPrivilegeEscalation: false
requiredDropCapabilities:
- ALL
volumes:
- 'configMap'
- 'secret'
- 'emptyDir'
hostNetwork: false

This policy prevents the use of privileged containers, limits volumes, and ensures that the pods run with the least possible permissions, ensuring tight security.

Conclusion

By implementing namespaces, network policies, RBAC, resource quotas, and pod security policies, you can create a Kubernetes environment that is highly isolated. This approach prevents any interaction between customer namespaces and protects the cluster from security risks, resource monopolization, or accidental cross-namespace communication. For Kubernetes deployments where multi-tenancy is a concern, these measures are essential for maintaining secure and well-separated environments.

--

--

No responses yet