Sub topic 14
Hardening the Cluster
This chapter assumes you can write Kubernetes manifests, know how the Amazon VPC CNI gives pods VPC IP addresses, and understand the IAM and access entry setup from earlier chapters. It covers three controls: network policies, security groups for pods, and secrets encryption.
Layers of Defense
Each control works at a different layer. Use them together.
Network Policies
By default, every pod can talk to every other pod. A NetworkPolicy restricts that traffic. The VPC CNI can enforce policies itself, but you must turn the feature on in the add-on configuration:
aws eks update-addon \
--cluster-name demo \
--addon-name vpc-cni \
--configuration-values '{"enableNetworkPolicy":"true"}'Start with a default-deny rule for incoming traffic in a namespace, then add explicit allows:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-ingress
namespace: default
spec:
podSelector: {}
policyTypes:
- Ingress
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-to-web
namespace: default
spec:
podSelector:
matchLabels:
app: web
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 80Only pods labeled app: frontend can now reach the web pods on port 80. Traffic from other pods is dropped.
Security Groups for Pods
Network policies work at the Kubernetes level. Security groups work at the VPC level. With security groups for pods, a pod gets its own security group instead of sharing the node's. This is useful when a pod must reach an RDS database that only allows traffic from a specific security group.
To use it, set ENABLE_POD_ENI=true on the VPC CNI. Then create a SecurityGroupPolicy that selects pods and lists the group IDs. Not every instance type supports this, so check the supported list before you rely on it.
Secrets Encryption
Kubernetes Secrets are stored in etcd, the cluster database. Envelope encryption wraps them with a key in AWS KMS (Key Management Service). You can enable it on an existing cluster:
eksctl utils enable-secrets-encryption \
--cluster demo \
--region us-east-1 \
--key-arn arn:aws:kms:us-east-1:111122223333:key/EXAMPLE-KEY-IDOnce enabled, you cannot turn it off. Secrets that already exist are not re-encrypted until they are rewritten, so touch them after enabling:
kubectl get secrets --all-namespaces -o json | kubectl replace -f -The IAM principal that creates the cluster needs permission to use the key, so scope the key policy carefully.
Common Mistakes
| Mistake | Effect |
|---|---|
| Writing NetworkPolicy objects without enabling the feature in the VPC CNI | Policies are accepted but nothing is enforced |
| Blocking egress without allowing DNS | Pods cannot resolve names |
| Assuming secrets encryption hides values from `kubectl` | Users with RBAC access still read decoded values |
| Broad `cluster-admin` access entries | Defeats the other controls |
Exercise
You created a default-deny ingress NetworkPolicy, but pods can still reach each other freely. What is the most likely cause, and how do you fix it?
Show answer
The VPC CNI is not enforcing network policies because the feature is off. Enable it with aws eks update-addon --cluster-name demo --addon-name vpc-cni --configuration-values '{"enableNetworkPolicy":"true"}', then test again.