RousselTMLEARNING

Containers

Securing Kubernetes: RBAC, NetworkPolicies, container network security and Pod Security

Securing Kubernetes: RBAC (Role, ClusterRole, bindings), ServiceAccounts, NetworkPolicies, Pod Security Standards, securityContext, secrets, auditing and checking with kubectl auth can-i.

1. The security layers #

Defense in depth.

Overview #

Kubernetes security layers
No single layer is enough.

2. RBAC #

Roles, ClusterRoles and bindings.

Granting precise rights #

Kubernetes RBAC model
A ClusterRole can be bound in a single namespace with a RoleBinding.
# Un rôle en lecture seule sur les pods du namespace production
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata: { name: lecteur-pods, namespace: production }
rules:
  - apiGroups: [""]
    resources: ["pods", "pods/log"]
    verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: { name: alice-lecteur, namespace: production }
subjects: [{ kind: User, name: alice, apiGroup: rbac.authorization.k8s.io }]
roleRef: { kind: Role, name: lecteur-pods, apiGroup: rbac.authorization.k8s.io }
kubectl auth can-i list pods -n production --as alice          # yes
kubectl auth can-i delete pods -n production --as alice        # no
kubectl auth can-i --list -n production --as alice
kubectl get clusterrolebindings -o wide | grep cluster-admin     # qui est cluster-admin ?
kubectl create rolebinding dev-edit --clusterrole=edit --user=bob -n dev
Common mistake: Only grant cluster-admin to administration accounts. Wildcards (verbs: ["*"]) and access to secrets must remain exceptional.

ServiceAccounts #

kubectl create serviceaccount app -n production
kubectl create role lire-config --verb=get,list --resource=configmaps -n production
kubectl create rolebinding app-lire-config --role=lire-config --serviceaccount=production:app -n production
# Dans le pod : serviceAccountName: app   et   automountServiceAccountToken: false si l'API n'est pas utilisée

3. Isolating pods #

securityContext, Pod Security and NetworkPolicies.

Pod security constraints #

# Pod Security Admission : appliquer le profil "restricted" à un namespace
kubectl label namespace production pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/warn=restricted

# Conteneur durci
securityContext:
  runAsNonRoot: true
  runAsUser: 10001
  readOnlyRootFilesystem: true
  allowPrivilegeEscalation: false
  capabilities: { drop: ["ALL"] }
  seccompProfile: { type: RuntimeDefault }

NetworkPolicies #

# Tout interdire par défaut, puis autoriser web -> api
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: deny-all, namespace: production }
spec: { podSelector: {}, policyTypes: [Ingress, Egress] }
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: web-vers-api, namespace: production }
spec:
  podSelector: { matchLabels: { app: api } }
  policyTypes: [Ingress]
  ingress:
    - from: [{ podSelector: { matchLabels: { app: web } } }]
      ports: [{ port: 8080 }]
Good to know: NetworkPolicies require a CNI that implements them (Calico, Cilium...). With a CNI that doesn't support them, they are accepted but have no effect.

4. Secrets and auditing #

Protecting and controlling.

Best practices #

  • Enable secret encryption in etcd (EncryptionConfiguration) and restrict access to etcd.
  • Prefer External Secrets Operator or the CSI Secrets Store (Vault, AWS/GCP/Azure) over versioned secrets.
  • Scan images (trivy image) and check configurations (kube-bench, kubescape).
  • Enable the API server's audit logs and monitor access to secrets.
trivy image nginx:1.27
trivy k8s --report summary cluster
kubescape scan framework nsa
docker run --rm --pid=host -v /etc:/etc:ro aquasec/kube-bench:latest run --targets node

Related articles

Container network security and Kubernetes networking

Secure the container network: how Docker and Kubernetes networking works, CNI, default-deny NetworkPolicies, …

Container registry: choosing and securing your image registry (Docker Hub, Harbor, GHCR, ECR)

Understand container registries: Docker Hub, GitHub Container Registry, cloud registries (ECR, ACR, Artifact …

Kubernetes: architecture and components (control plane, nodes, objects)

Understand the Kubernetes architecture: API server, etcd, scheduler, controller manager, kubelet, kube-proxy,…

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!