1. The security layers #
Defense in depth.
Overview #
2. RBAC #
Roles, ClusterRoles and bindings.
Granting precise rights #
# 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 devCommon mistake: Only grantcluster-adminto administration accounts. Wildcards (verbs: ["*"]) and access tosecretsmust 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ée3. 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