Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Network policies are a crucial aspect of Kubernetes security, allowing fine-grained control over pod-to-pod communication. In this hands-on guide, we’ll walk through setting up a Kind cluster with Calico, deploying applications, and implementing network policies.
Step 1: Creating a Kind Cluster with Custom Networking#
First, let’s create a Kind cluster with the default CNI disabled:
1kind: Cluster2apiVersion: kind.x-k8s.io/v1alpha43nodes:4 - role: control-plane5 extraPortMappings:6 - containerPort: 300017 hostPort: 300018 - role: worker9 - role: worker10networking:11 disableDefaultCNI: true12 podSubnet: 192.168.0.0/16Save this as kind-config.yaml and create the cluster:
1kind create cluster --config kind-config.yamlStep 2: Installing Calico#
Follow the official Calico documentation to install it on your Kind cluster:
1kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/tigera-operator.yaml2kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/custom-resources.yamlStep 3: Deploying Applications#
Let’s create deployments for frontend, backend, and db:
1apiVersion: apps/v12kind: Deployment3metadata:4 name: frontend5spec:6 replicas: 17 selector:8 matchLabels:9 app: frontend10 template:11 metadata:12 labels:13 app: frontend14 spec:15 containers:16 - name: nginx17 image: nginx18 ports:19 - containerPort: 8020---21apiVersion: apps/v122kind: Deployment23metadata:24 name: backend25spec:26 replicas: 127 selector:28 matchLabels:29 app: backend30 template:31 metadata:32 labels:33 app: backend34 spec:35 containers:36 - name: nginx37 image: nginx38 ports:39 - containerPort: 8040---41apiVersion: apps/v142kind: Deployment43metadata:44 name: db45spec:46 replicas: 147 selector:48 matchLabels:49 app: db50 template:51 metadata:52 labels:53 app: db54 spec:55 containers:56 - name: mysql57 image: mysql:5.758 ports:59 - containerPort: 330660 env:61 - name: MYSQL_ROOT_PASSWORD62 value: passwordSave this as deployments.yaml and apply:
1kubectl apply -f deployments.yamlStep 4: Exposing Services#
Now, let’s expose these deployments as NodePort services:
1apiVersion: v12kind: Service3metadata:4 name: frontend5spec:6 type: NodePort7 selector:8 app: frontend9 ports:10 - port: 8011 targetPort: 8012 nodePort: 3000113---14apiVersion: v115kind: Service16metadata:17 name: backend18spec:19 type: NodePort20 selector:21 app: backend22 ports:23 - port: 8024 targetPort: 8025 nodePort: 3000226---27apiVersion: v128kind: Service29metadata:30 name: db31spec:32 type: NodePort33 selector:34 app: db35 ports:36 - port: 330637 targetPort: 330638 nodePort: 30003Save this as services.yaml and apply:
1kubectl apply -f services.yamlStep 5: Testing Connectivity#
To test connectivity, you can use temporary pods:
1kubectl run tmp-shell --rm -i --tty --image nicolaka/netshoot -- /bin/bashFrom within this pod, you can use curl to test connections to other services.
Step 6: Implementing Network Policy#
Now, let’s create a network policy that only allows the backend to access the db:
1apiVersion: networking.k8s.io/v12kind: NetworkPolicy3metadata:4 name: db-access-policy5spec:6 podSelector:7 matchLabels:8 app: db9 policyTypes:10 - Ingress11 ingress:12 - from:13 - podSelector:14 matchLabels:15 app: backend16 ports:17 - protocol: TCP18 port: 3306Save this as network-policy.yaml and apply:
1kubectl apply -f network-policy.yamlKey Takeaways#
- CNI Flexibility: Kind allows us to disable the default CNI and use alternatives like Calico.
- Fine-grained Control: Network policies enable precise control over pod-to-pod communication.
- Label-based Selectors: Network policies use labels to select pods, making them flexible and powerful.
- Default Deny: Without explicit allow rules, network policies default to denying all traffic.
- Testing is Crucial: Always test your network policies thoroughly to ensure desired behavior.
Conclusion#
Kubernetes Network Policies provide a powerful tool for securing your cluster’s internal communications. By following this guide, you’ve set up a Kind cluster with Calico, deployed applications, and implemented a basic network policy. This foundation will allow you to create more complex policies tailored to your specific security needs.
Remember, network security is an ongoing process. Regularly review and update your policies as your application architecture evolves.
This blog post template provides a comprehensive, hands-on guide to implementing Kubernetes Network Policies, based on the task you described. It includes step-by-step instructions, explanations of key concepts, and important takeaways. The content is structured to be both informative and practical, suitable for readers who want to understand and implement Network Policies in their Kubernetes environments.