Sub topic 14
Hardening the Cluster
This chapter covers three controls on a running EKS cluster: network policies, security groups, and Kubernetes Secrets encryption. It also covers the API endpoint and namespace-level pod restrictions. It assumes you can create a cluster with eksctl, write Kubernetes manifests, and understand IAM roles and VPC security groups from the earlier chapters.
Where hardening fits in the shared responsibility model
AWS secures the managed control plane. You secure everything the data plane exposes: node configuration, pod traffic, who can reach the API server, and how sensitive data is stored. A new cluster is usable but permissive. By default, any pod can talk to any other pod, and the API endpoint is public.
You can think of the controls as layers, each of which narrows what an attacker can reach.
Network policies
A Kubernetes NetworkPolicy is a rule that selects pods and says which ingress (incoming) and egress (outgoing) traffic they accept. The API object exists in every cluster, but something has to enforce it. The Amazon VPC CNI can enforce it natively on recent versions, using eBPF on the node. You turn it on through the add-on configuration:
aws eks update-addon \
--cluster-name demo \
--addon-name vpc-cni \
--configuration-values '{"enableNetworkPolicy":"true"}'Check that the aws-node pods restarted and are healthy before you apply policies:
kubectl -n kube-system rollout status daemonset/aws-nodeStart with default deny
Policies are additive. Once any policy selects a pod for a direction, only traffic that some policy allows is permitted in that direction. The usual pattern is to deny everything in a namespace and then open specific paths.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
namespace: default
spec:
podSelector: {}
policyTypes:
- Ingress
- EgressAn empty podSelector selects every pod in the namespace. Applied alone, this policy also blocks DNS lookups, so most applications break immediately. Add an egress rule for CoreDNS, then allow the traffic your application needs.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns-egress
namespace: default
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: web-allow-from-client
namespace: default
spec:
podSelector:
matchLabels:
app: web
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: client
ports:
- protocol: TCP
port: 80The second policy lets only pods labeled app: client reach the web pods on port 80.
Gotchas
- If you expose
webthrough an ALB withtarget-type: ip, the load balancer sends traffic straight to pod IPs. Those sources are not pods, so apodSelectorrule will not match them. Allow the VPC or subnet range with anipBlockrule instead. - Each policy is evaluated per direction. A pod with an egress deny and no ingress policy still accepts all incoming traffic.
- Policies that only exist in a manifest repository protect nothing if the CNI feature is off. Test with a real connection attempt, for example
kubectl execinto a pod that should be blocked and runcurlagainst the target.
Security groups
Network policies work at the pod level inside the cluster. Security groups work at the VPC level on network interfaces. EKS uses several of them:
| Security group | Attached to | Purpose |
|---|---|---|
| Cluster security group | Control plane network interfaces and managed node group nodes | Allows control plane and node communication |
| Additional cluster security group | Control plane network interfaces | Optional rules you supply |
| Node security group | Self-managed nodes you create | Whatever you define |
| Load balancer security group | ALB or NLB | Who may reach the load balancer |
The cluster security group created by EKS permits all traffic between its members. Do not delete or narrow its rules without testing, because the control plane needs to reach the kubelet on each node.
By default, all pods on a node share that node's security groups. To give a specific pod its own, enable security groups for pods in the VPC CNI and create a SecurityGroupPolicy. This feature is most useful when a pod must reach a database whose security group only accepts traffic from a specific source group.
aws eks update-addon \
--cluster-name demo \
--addon-name vpc-cni \
--configuration-values '{"env":{"ENABLE_POD_ENI":"true"}}'apiVersion: vpcresources.k8s.aws/v1beta1
kind: SecurityGroupPolicy
metadata:
name: web-sg-policy
namespace: default
spec:
podSelector:
matchLabels:
app: web
securityGroups:
groupIds:
- sg-0123456789abcdef0This feature requires supported Nitro-based instance types, and each instance type limits how many pods can get their own network interface. Check the limits before you rely on it across a large node group.
Restricting the API endpoint
The cluster API endpoint is public by default, and anyone on the internet can attempt to authenticate to it. Authentication and RBAC still apply, but you should reduce exposure. You have three options:
- Public only, optionally limited to a list of CIDR ranges.
- Public and private, so nodes and in-VPC tools reach the API through the VPC.
- Private only, reachable from the VPC or a connected network such as a VPN or Direct Connect.
With eksctl you can declare this in the cluster config:
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
name: demo
region: us-east-1
vpc:
clusterEndpoints:
publicAccess: true
privateAccess: true
publicAccessCIDRs:
- 203.0.113.0/24Private-only access is the strongest option, but you then need a path into the VPC for kubectl and for CI pipelines. Plan that path first. Otherwise you lock yourself out.
Encrypting Secrets
A Kubernetes Secret is only base64-encoded, not encrypted, in the API. What matters is how it is stored. EKS encrypts etcd data at rest for you. To control the key, enable envelope encryption with an AWS KMS key you own. EKS then encrypts each Secret with a data key, and your KMS key protects that data key. Every use of the key shows up in CloudTrail, and you can revoke access to it.
Add the key when you create the cluster:
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
name: demo
region: us-east-1
secretsEncryption:
keyARN: arn:aws:kms:us-east-1:111122223333:key/11111111-2222-3333-4444-555555555555For an existing cluster, use the CLI:
aws eks associate-encryption-config \
--cluster-name demo \
--encryption-config '[{"resources":["secrets"],"provider":{"keyArn":"arn:aws:kms:us-east-1:111122223333:key/11111111-2222-3333-4444-555555555555"}}]'The key must be a symmetric key in the same Region as the cluster. Once enabled, you cannot turn it off. Existing Secrets are re-encrypted in the background, and you can speed this up by rewriting them:
kubectl get secrets --all-namespaces -o json | kubectl replace -f -Restricting what pods may do
Network rules do not stop a pod from running as root or mounting the host filesystem. Pod Security Admission, built into Kubernetes, enforces the Pod Security Standards per namespace through labels:
kubectl label namespace default \
pod-security.kubernetes.io/enforce=baseline \
pod-security.kubernetes.io/warn=restrictedThis rejects pods that violate the baseline profile and prints a warning for pods that would fail restricted. Use warn first to see what would break, then raise enforce once your workloads comply.
Common errors
- Pods cannot resolve names after a deny policy: You forgot DNS egress.
- Policy has no effect: Network policy support is not enabled in the VPC CNI, or the add-on has not restarted.
- Locked out of a private cluster: Your workstation is not inside or connected to the VPC.
- Pods stay Pending after enabling security groups for pods: The instance type does not support branch network interfaces, or it has hit its interface limit.
Exercise
A namespace has a default-deny policy for both ingress and egress. You add a policy that allows ingress to the web pods from app: client on port 80, but the client's request still times out. Name two things you should check.
Show answer
Check that the client pod's egress is allowed, because the default-deny policy also blocks its outgoing traffic, so you need an egress rule from the client to web on port 80 and an egress rule for DNS to CoreDNS. Also check that the labels on the client pod match app: client and that the policy sits in the same namespace as the web pods.