Cloud & Deployment

Kubernetes Privacy Shields & AI Workload Security

Protect sensitive data in Kubernetes with Secrets, RBAC, Network Policies, Pod Security Standards, and AI-specific guardrails for model serving.

Why Kubernetes Needs Privacy Shields

Kubernetes is powerful — and that power creates a large attack surface. A misconfigured cluster can expose secrets, allow cross-namespace data access, or let an AI workload exfiltrate training data. Understanding Kubernetes privacy controls is non-negotiable for any team running production workloads.

AI workloads add a new dimension: models process user data, inference APIs handle PII, and training jobs can access datasets that are sensitive by nature. Privacy shields must account for both the infrastructure layer and the data flowing through it.

Kubernetes Secrets

Kubernetes Secrets store sensitive data (API keys, passwords, tokens) as base64-encoded values. They are better than hardcoding values in manifests, but they are NOT encrypted by default.

yaml
# Create a Secret
apiVersion: v1
kind: Secret
metadata:
  name: ai-api-keys
  namespace: inference
type: Opaque
stringData:
  openai-api-key: "sk-your-key-here"
  db-password: "s3cr3t-password"
yaml
# Consume a Secret as environment variables
apiVersion: v1
kind: Pod
spec:
  containers:
    - name: inference-server
      image: myapp:latest
      env:
        - name: OPENAI_API_KEY
          valueFrom:
            secretKeyRef:
              name: ai-api-keys
              key: openai-api-key

Enable encryption at rest — by default, Secrets are stored as plain base64 in etcd. Enable EncryptionConfiguration to encrypt them:

yaml
# /etc/kubernetes/encryption-config.yaml
apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
  - resources:
      - secrets
    providers:
      - aescbc:
          keys:
            - name: key1
              secret: <base64-encoded-32-byte-key>
      - identity: {}

Role-Based Access Control (RBAC)

RBAC controls who can read, write, or delete Kubernetes resources. It is the primary access control mechanism for cluster privacy.

yaml
# Role: restrict to reading Secrets in a specific namespace
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: secret-reader
  namespace: inference
rules:
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get", "list"]
    resourceNames: ["ai-api-keys"]  # limit to specific secret
---
# Bind the role to a service account
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: inference-secret-reader
  namespace: inference
subjects:
  - kind: ServiceAccount
    name: inference-sa
    namespace: inference
roleRef:
  kind: Role
  name: secret-reader
  apiGroup: rbac.authorization.k8s.io

Apply least privilege: create a dedicated ServiceAccount per workload — never use the default ServiceAccount.

Network Policies

By default, all pods in Kubernetes can communicate with each other. Network Policies restrict traffic to only what is explicitly allowed — essential for preventing data exfiltration.

yaml
# Deny all ingress and egress by default in a namespace
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-all
  namespace: inference
spec:
  podSelector: {}  # applies to ALL pods in namespace
  policyTypes:
    - Ingress
    - Egress
---
# Allow only the API gateway to reach inference pods
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-api-gateway
  namespace: inference
spec:
  podSelector:
    matchLabels:
      app: model-server
  policyTypes:
    - Ingress
  ingress:
    - from:
        - namespaceSelector:
            matchLabels:
              name: api-gateway
          podSelector:
            matchLabels:
              app: gateway
      ports:
        - protocol: TCP
          port: 8080

Pod Security Standards

Pod Security Standards (PSS) replace the deprecated PodSecurityPolicy and enforce security constraints at the namespace level.

yaml
# Label a namespace to enforce the restricted security standard
apiVersion: v1
kind: Namespace
metadata:
  name: inference
  labels:
    pod-security.kubernetes.io/enforce: restricted
    pod-security.kubernetes.io/audit: restricted
    pod-security.kubernetes.io/warn: restricted

The restricted profile requires:

  • Pods run as non-root
  • No privilege escalation
  • Seccomp profile set to RuntimeDefault or Localhost
  • All capabilities dropped
yaml
# Compliant pod spec for restricted namespace
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 1000
    seccompProfile:
      type: RuntimeDefault
  containers:
    - name: model-server
      securityContext:
        allowPrivilegeEscalation: false
        capabilities:
          drop: ["ALL"]
        readOnlyRootFilesystem: true

AI-Specific Privacy Guardrails

AI inference workloads have unique privacy concerns:

Isolate model servers — run inference pods in a dedicated namespace with Network Policies that prevent them from making arbitrary outbound calls. A model server should only be able to reach the services it needs.

Protect training data — mount datasets as read-only volumes. Use PersistentVolumeClaims with restricted access modes.

yaml
volumes:
  - name: training-data
    persistentVolumeClaim:
      claimName: training-dataset-pvc
      readOnly: true

Audit inference logs — all inference requests touching PII should be logged to an append-only audit store. Use Kubernetes audit policies to log API access to sensitive resources.

Limit egress for model pods — a model server should never be able to exfiltrate data to external endpoints. Use egress Network Policies to allowlist only required destinations (your own API, object storage).

AI-generated infrastructure code review — when AI tools generate Kubernetes YAML, always review for:

  • Missing RBAC restrictions
  • hostNetwork: true or hostPID: true (breaks isolation)
  • privileged: true containers
  • Missing resource limits (enables DoS)
  • Secrets referenced by value rather than secretKeyRef

Key Takeaways

  • Enable Secret encryption at rest in etcd — base64 is not encryption
  • Create a dedicated ServiceAccount per workload and grant only the permissions it needs
  • Apply default-deny Network Policies and explicitly allowlist traffic
  • Use the restricted Pod Security Standard for all production namespaces
  • AI workloads processing PII require dedicated namespaces, egress restrictions, and audit logging

Example

yaml
# Complete AI inference namespace setup
apiVersion: v1
kind: Namespace
metadata:
  name: inference
  labels:
    pod-security.kubernetes.io/enforce: restricted
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: inference-sa
  namespace: inference
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: inference-role
  namespace: inference
rules:
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get"]
    resourceNames: ["ai-api-keys"]
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-all
  namespace: inference
spec:
  podSelector: {}
  policyTypes:
    - Ingress
    - Egress
Try it yourself — YAML

Docker, AWS, Vercel, Netlify, GitHub, GitHub Actions are trademarks of Docker, Inc., Amazon.com, Inc., Vercel, Inc., Netlify, Inc., Microsoft Corporation. DevForge Academy is not affiliated with, endorsed by, or sponsored by these companies. Referenced for educational purposes only. See full disclaimers