Advanced K3s Security Against Lateral Movement Attacks Using Cilium Network Policy on VPS
A detailed guide to deploying Cilium Network Policy on K3s to prevent lateral movement attacks on VPS environments. Covers configuration, testing, and network security optimization.
Introduction to K3s and Cilium Network Policy
K3s is a lightweight Kubernetes distribution optimized for edge, IoT, and VPS environments. Despite its efficiency, like any distributed system, K3s must be protected against threats such as lateral movement attacks. This technique allows attackers to move from a compromised host to other systems within the internal network.
Cilium Network Policy is a powerful solution to control traffic between pods in K3s. It leverages the Network Policy API of Kubernetes but uses BPF (Berkeley Packet Filter) for high performance and more granular control. With Cilium, you can define rules to allow or deny traffic based on pod, namespace, IP, port, and even application-level criteria.
Setting Up a Test Environment on VPS
To follow this guide, you need a VPS (e.g., Ubuntu 22.04) with at least 2GB RAM and 2 CPUs. First, install K3s and Cilium:
curl -sfL https://get.k3s.io | sh -
Start Cilium by installing the Cilium Operator:
kubectl create namespace cilium
kubectl -n cilium create secret generic cilium-config --from-literal=enable-ipv4=true --from-literal=enable-ipv6=false
kubectl apply -f https://raw.githubusercontent.com/cilium/cilium/v1.13.0/install/kubernetes/quick-install.yaml
After installation, check the status:
kubectl -n cilium get pods
Wait until all pods in the cilium namespace are in the Running state.
Configuring Cilium Network Policy to Prevent Lateral Movement
Assume you have two pods: frontend and backend. You want to allow only frontend to connect to backend on port 8080, while blocking all other pods from accessing backend.
Create a file named network-policy.yaml:
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: allow-frontend-to-backend
spec:
endpointSelector:
matchLabels:
app: backend
ingress:
- fromEndpoints:
- matchLabels:
app: frontend
toPorts:
- ports:
- port: "8080"
protocol: TCP
Apply the policy:
kubectl apply -f network-policy.yaml
Now, only pods labeled app: frontend can connect to the backend pod on port 8080. All other traffic is denied.
Testing and Optimization
To test, deploy a test pod:
kubectl run test --image=busybox --command -- sleep 3600
kubectl exec -it test -- ping backend
If you do not receive a response, the policy is working correctly. To monitor traffic, use:
kubectl -n cilium exec -it cilium-xxxxx -- cilium policy get
kubectl -n cilium exec -it cilium-xxxxx -- cilium monitor
For optimization, ensure policies only allow necessary traffic. Avoid using allow-all or fromEntities: cluster unless required.
Troubleshooting
- Pod cannot connect: Check pod labels and Cilium policies. Use
kubectl describe pod <name>to inspect errors. - Cilium not working: Review logs with
kubectl logs -n cilium <cilium-pod>. - Policy not visible: Ensure Cilium is correctly installed and running.
References
Recommended NVMe Cloud VPS Plans to Deploy This Setup
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-3E
10% VAT included
- CPU: 2 vCPU
- RAM: 4 GB
- SSD: 50 GB NVMe
