Kubernetes Networking Overview¶
Kubernetes networking enables communication between pods, services, and external clients.
Kubernetes Networking Model¶
Core Principles¶
- Every pod gets its own IP address
- Pods can communicate with all other pods without NAT
- Agents on a node can communicate with all pods on that node
- Pods see themselves with the same IP that others see them with
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CLUSTER NETWORK β
β β
β ββββββββββββββββ ββββββββββββββββ β
β β NODE 1 β β NODE 2 β β
β β β β β β
β β ββββββββββ β β ββββββββββ β β
β β βPod A β β β βPod C β β β
β β β10.1.1.1ββββΌβββββββββββββββΌββΊβ10.1.2.1β β β
β β ββββββββββ β β ββββββββββ β β
β β β β β β
β β ββββββββββ β β ββββββββββ β β
β β βPod B β β β βPod D β β β
β β β10.1.1.2ββββΌβββββββββββββββΌββΊβ10.1.2.2β β β
β β ββββββββββ β β ββββββββββ β β
β ββββββββββββββββ ββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Network Types¶
1. Pod-to-Pod Communication¶
Direct communication using pod IPs:
apiVersion: v1
kind: Pod
metadata:
name: client-pod
spec:
containers:
- name: client
image: busybox
command: ['sh', '-c', 'wget -O- http://10.1.2.1:8080']
Characteristics: - Flat network space - No NAT required - Direct IP communication - Implemented by CNI plugins
2. Pod-to-Service Communication¶
Using service abstraction:
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
selector:
app: backend
ports:
- port: 80
targetPort: 8080
Access Methods: - ClusterIP (internal) - DNS name resolution - Environment variables - Service discovery
3. External-to-Service Communication¶
Exposing services externally:
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
type: LoadBalancer
selector:
app: frontend
ports:
- port: 80
targetPort: 8080
Methods: - NodePort - LoadBalancer - Ingress - ExternalName
Network Namespaces¶
Container Network Namespace¶
βββββββββββββββββββββββββββββββββββββββ
β POD β
β β
β ββββββββββββββββββββββββββββββββ β
β β Network Namespace β β
β β β β
β β eth0: 10.1.1.1 β β
β β lo: 127.0.0.1 β β
β β β β
β β ββββββββββββ ββββββββββββ β β
β β βContainer1β βContainer2β β β
β β βlocalhost β βlocalhost β β β
β β ββββββββββββ ββββββββββββ β β
β ββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββ
Shared in Pod: - Network interface - IP address - Port space - Routing table
CNI (Container Network Interface)¶
What is CNI?¶
Standard interface between container runtime and network plugins.
ββββββββββββββββ
β Kubelet β
ββββββββ¬ββββββββ
β
β CNI API
β
ββββββββΌββββββββ
β CNI Plugin β
ββββββββ¬ββββββββ
β
β Configure
β
ββββββββΌββββββββ
β Network β
ββββββββββββββββ
Popular CNI Plugins¶
Calico:
# Install Calico
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
# Check Calico status
kubectl get pods -n kube-system -l k8s-app=calico-node
Flannel:
# Install Flannel
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
Weave Net:
# Install Weave Net
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
Cilium:
CNI Plugin Comparison¶
| Plugin | Network Model | Encryption | Network Policy | Performance |
|---|---|---|---|---|
| Calico | L3 BGP | Yes | Yes | High |
| Flannel | Overlay (VXLAN) | No | No | Medium |
| Weave | Overlay | Yes | Yes | Medium |
| Cilium | eBPF | Yes | Yes | Very High |
DNS in Kubernetes¶
CoreDNS¶
Default DNS server in Kubernetes:
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
DNS Records¶
Service DNS:
Pod DNS:
Headless Service Pod DNS:
DNS Resolution Examples¶
apiVersion: v1
kind: Pod
metadata:
name: dns-test
spec:
containers:
- name: test
image: busybox
command: ['sh', '-c', 'sleep 3600']
# Test DNS resolution
kubectl exec -it dns-test -- nslookup kubernetes.default
# Test service DNS
kubectl exec -it dns-test -- nslookup my-service.default.svc.cluster.local
# Test external DNS
kubectl exec -it dns-test -- nslookup google.com
Custom DNS Configuration¶
apiVersion: v1
kind: Pod
metadata:
name: custom-dns
spec:
dnsPolicy: "None"
dnsConfig:
nameservers:
- 8.8.8.8
- 8.8.4.4
searches:
- my-namespace.svc.cluster.local
- svc.cluster.local
- cluster.local
options:
- name: ndots
value: "2"
containers:
- name: app
image: nginx
DNS Policies: - Default: Inherit from node - ClusterFirst: Use cluster DNS (default for pods) - ClusterFirstWithHostNet: For pods with hostNetwork - None: Custom DNS configuration
Network Plugins Architecture¶
Overlay Network (VXLAN)¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Physical Network β
β β
β ββββββββββββββββ ββββββββββββββββ β
β β Node 1 β β Node 2 β β
β β 10.0.1.10 β β 10.0.1.11 β β
β β β β β β
β β ββββββββββ β VXLAN β ββββββββββ β β
β β βVTEP ββββΌββTunnelβββββββΌββΊβVTEP β β β
β β βββββ¬βββββ β β βββββ¬βββββ β β
β β β β β β β β
β β βββββΌβββββ β β βββββΌβββββ β β
β β βPod β β β βPod β β β
β β β10.1.1.1β β β β10.1.2.1β β β
β β ββββββββββ β β ββββββββββ β β
β ββββββββββββββββ ββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
BGP Network (Calico)¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Physical Network β
β β
β ββββββββββββββββ ββββββββββββββββ β
β β Node 1 β β Node 2 β β
β β β β β β
β β ββββββββββ β BGP β ββββββββββ β β
β β βBGP ββββΌββPeeringββββββΌββΊβBGP β β β
β β βSpeaker β β β βSpeaker β β β
β β βββββ¬βββββ β β βββββ¬βββββ β β
β β β β β β β β
β β βββββΌβββββ β β βββββΌβββββ β β
β β βPod β β β βPod β β β
β β β10.1.1.1β β β β10.1.2.1β β β
β β ββββββββββ β β ββββββββββ β β
β ββββββββββββββββ ββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Network Troubleshooting¶
Debug Pod¶
apiVersion: v1
kind: Pod
metadata:
name: netshoot
spec:
containers:
- name: netshoot
image: nicolaka/netshoot
command: ['sh', '-c', 'sleep 3600']
Common Commands¶
# Test connectivity
kubectl exec -it netshoot -- ping 10.1.1.1
# Test DNS
kubectl exec -it netshoot -- nslookup kubernetes.default
# Test service connectivity
kubectl exec -it netshoot -- curl http://my-service
# Check routes
kubectl exec -it netshoot -- ip route
# Check interfaces
kubectl exec -it netshoot -- ip addr
# Trace route
kubectl exec -it netshoot -- traceroute 10.1.1.1
# Port scan
kubectl exec -it netshoot -- nc -zv my-service 80
# DNS lookup
kubectl exec -it netshoot -- dig my-service.default.svc.cluster.local
Network Debugging¶
# Check CNI plugin
kubectl get pods -n kube-system | grep -E 'calico|flannel|weave|cilium'
# Check kube-proxy
kubectl get pods -n kube-system -l k8s-app=kube-proxy
# View kube-proxy logs
kubectl logs -n kube-system -l k8s-app=kube-proxy
# Check iptables rules
kubectl exec -it <kube-proxy-pod> -n kube-system -- iptables-save
# Check service endpoints
kubectl get endpoints
# Describe service
kubectl describe service my-service
Network Performance¶
Bandwidth Testing¶
apiVersion: v1
kind: Pod
metadata:
name: iperf-server
spec:
containers:
- name: iperf
image: networkstatic/iperf3
command: ['iperf3', '-s']
ports:
- containerPort: 5201
---
apiVersion: v1
kind: Pod
metadata:
name: iperf-client
spec:
containers:
- name: iperf
image: networkstatic/iperf3
command: ['sh', '-c', 'sleep 3600']
Latency Testing¶
# Ping test
kubectl exec -it netshoot -- ping -c 10 <target-ip>
# HTTP latency
kubectl exec -it netshoot -- curl -w "@curl-format.txt" -o /dev/null -s http://my-service
Best Practices¶
- Choose Appropriate CNI Plugin
- Consider network requirements
- Evaluate performance needs
-
Check feature requirements (encryption, network policies)
-
Network Segmentation
- Use namespaces
- Implement network policies
-
Separate environments
-
DNS Configuration
- Optimize DNS caching
- Monitor DNS performance
-
Use appropriate ndots value
-
Monitoring
- Monitor network traffic
- Track latency metrics
-
Alert on connectivity issues
-
Security
- Enable network policies
- Use encryption where needed
- Limit external access