Kubernetes Multi-tenancy
/ 3 min read
kubernetes , k8s , multi-tenancy , security , devops , cloud-native , containers , series:kubernetes:17
Understanding Multi-tenancy in Kubernetes
Multi-tenancy in Kubernetes allows multiple users, teams, or applications to share a Kubernetes cluster while maintaining isolation and security.
Multi-tenancy Models
1. Namespace-based Multi-tenancy
apiVersion: v1kind: Namespacemetadata: name: team-a labels: team: a---apiVersion: v1kind: ResourceQuotametadata: name: team-a-quota namespace: team-aspec: hard: requests.cpu: "4" requests.memory: 8Gi limits.cpu: "8" limits.memory: 16Gi pods: "20"2. Cluster-based Multi-tenancy
apiVersion: v1kind: Clustermetadata: name: team-b-clusterspec: kubernetesVersion: "1.26" networkPolicy: enabled: true podSecurityPolicy: enabled: trueResource Isolation
1. Network Policies
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: deny-all namespace: team-aspec: podSelector: {} policyTypes: - Ingress - Egress---apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: allow-specific namespace: team-aspec: podSelector: matchLabels: app: web policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: team: a ports: - protocol: TCP port: 802. Pod Security Standards
apiVersion: pod-security.kubernetes.io/v1kind: PodSecurityStandardmetadata: name: restricted namespace: team-aspec: enforce: restricted audit: restricted warn: restrictedAccess Control
1. Role-Based Access Control (RBAC)
apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: namespace: team-a name: pod-readerrules:- apiGroups: [""] resources: ["pods"] verbs: ["get", "list", "watch"]---apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: read-pods namespace: team-asubjects:- kind: User name: jane apiGroup: rbac.authorization.k8s.ioroleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io2. Service Accounts
apiVersion: v1kind: ServiceAccountmetadata: name: team-a-sa namespace: team-a---apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: team-a-sa-binding namespace: team-asubjects:- kind: ServiceAccount name: team-a-sa namespace: team-aroleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.ioResource Management
1. Resource Quotas
apiVersion: v1kind: ResourceQuotametadata: name: compute-resources namespace: team-aspec: hard: requests.cpu: "1" requests.memory: 1Gi limits.cpu: "2" limits.memory: 2Gi requests.nvidia.com/gpu: 12. Limit Ranges
apiVersion: v1kind: LimitRangemetadata: name: mem-limit-range namespace: team-aspec: limits: - default: memory: 512Mi cpu: 500m defaultRequest: memory: 256Mi cpu: 200m type: ContainerCost Allocation
1. Resource Labels
apiVersion: v1kind: Podmetadata: name: web-app namespace: team-a labels: cost-center: team-a environment: production project: webspec: containers: - name: web image: nginx:1.14.22. Chargeback System
apiVersion: cost.k8s.io/v1alpha1kind: CostReportmetadata: name: monthly-reportspec: timeframe: start: "2024-12-01" end: "2024-12-31" groupBy: - namespace - label:cost-centerSecurity Considerations
1. Pod Security Context
apiVersion: v1kind: Podmetadata: name: secure-pod namespace: team-aspec: securityContext: runAsUser: 1000 runAsGroup: 3000 fsGroup: 2000 containers: - name: app image: secure-app:1.0 securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true2. Network Segmentation
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: tenant-isolation namespace: team-aspec: podSelector: {} policyTypes: - Ingress - Egress ingress: - from: - namespaceSelector: matchLabels: tenant: team-a egress: - to: - namespaceSelector: matchLabels: tenant: team-a - namespaceSelector: matchLabels: common: trueMonitoring and Logging
1. Prometheus Configuration
apiVersion: monitoring.coreos.com/v1kind: ServiceMonitormetadata: name: team-a-monitor namespace: monitoringspec: selector: matchLabels: team: a namespaceSelector: matchNames: - team-a endpoints: - port: metrics2. Logging Pipeline
apiVersion: logging.banzaicloud.io/v1beta1kind: Flowmetadata: name: team-a-logs namespace: team-aspec: filters: - tag_normaliser: {} - parser: remove_key_name_field: true parse: type: json match: - select: labels: app.kubernetes.io/name: team-a localOutputRefs: - elasticsearch-outputBest Practices
- Use Namespaces for Logical Separation
- Implement Resource Quotas and Limits
- Configure Network Policies
- Apply Pod Security Standards
- Set Up RBAC Properly
- Monitor Resource Usage
- Implement Cost Allocation
- Regular Security Audits
Troubleshooting
Common Issues
- Resource Constraints
kubectl describe quota -n team-akubectl top pods -n team-a- Access Issues
kubectl auth can-i --as=jane --namespace=team-a get pods- Network Policy Issues
kubectl describe networkpolicy -n team-aConclusion
Implementing multi-tenancy in Kubernetes requires careful planning and consideration of security, resource isolation, and management aspects. By following best practices and using the right combination of Kubernetes features, you can create a secure and efficient multi-tenant environment.
Series Navigation
- Previous: Kubernetes Operators and CRDs
- Next: Kubernetes Service Mesh (Istio)