🌍 Networking

Calico

Project Calico

A container networking solution that supports NetworkPolicy and BGP-based routing as a Kubernetes CNI plugin. Implements fine-grained security policies between Pods through microsegmentation.

📖 Detailed Explanation

Calico (Project Calico) is an open-source networking and security solution for containers, virtual machines, and bare-metal workloads. Developed and maintained by Tigera, it is one of the most widely used CNI (Container Network Interface) plugins in Kubernetes. It is offered as a default or optional CNI in major cloud Kubernetes services such as AWS EKS, Google GKE, and Azure AKS.

Calico's key feature is pure Layer 3 networking based on BGP (Border Gateway Protocol). Unlike typical overlay networks (VXLAN, Geneve), it routes directly without packet encapsulation, so network overhead is low and performance is excellent. It also supports VXLAN mode when needed, providing flexibility in cloud environments.

Calico fully supports Kubernetes NetworkPolicy and additionally provides extended policy resources such as GlobalNetworkPolicy and NetworkSet. This makes it possible to implement microsegmentation and finely control traffic between Pods based on labels. For example, a policy such as "frontend Pods can access only port 8080 of backend Pods" is defined declaratively.

In large-scale production environments, Calico supports tens of thousands of Pods, and enabling eBPF mode provides further improved performance by handling network processing at the kernel level instead of using iptables. It is an essential tool for implementing Zero Trust networking in security-critical environments such as finance and healthcare.

💻 Code Example

# Kubernetes NetworkPolicy - frontend에서 backend로만 접근 허용
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: backend-allow-frontend
  namespace: production
spec:
  podSelector:
    matchLabels:
      app: backend
  policyTypes:
    - Ingress
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: frontend
      ports:
        - protocol: TCP
          port: 8080

---
# 기본 거부 정책 - 모든 ingress 차단 (화이트리스트 방식)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-ingress
  namespace: production
spec:
  podSelector: {}  # 모든 Pod에 적용
  policyTypes:
    - Ingress

---
# 데이터베이스 접근 제한 - backend에서만 MySQL 접근
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: mysql-allow-backend
  namespace: production
spec:
  podSelector:
    matchLabels:
      app: mysql
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: backend
      ports:
        - port: 3306
# Calico 설치 (Kubernetes 클러스터)
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/calico.yaml

# Calico 상태 확인
kubectl get pods -n calico-system
kubectl get nodes -o wide

# calicoctl 설치 (Calico CLI)
curl -L https://github.com/projectcalico/calico/releases/download/v3.27.0/calicoctl-linux-amd64 -o calicoctl
chmod +x calicoctl && sudo mv calicoctl /usr/local/bin/

# Calico 노드 상태 확인
calicoctl node status

# NetworkPolicy 목록 조회
kubectl get networkpolicy -A

# NetworkPolicy 상세 확인
kubectl describe networkpolicy backend-allow-frontend -n production

# Calico IPAM 정보 확인
calicoctl ipam show

# Pod IP 할당 확인
kubectl get pods -o wide -n production

# BGP Peer 상태 확인
calicoctl get bgpPeer -o yaml

# 네트워크 정책 적용 테스트
kubectl run test-pod --image=busybox --rm -it -- wget -qO- http://backend:8080
# Calico GlobalNetworkPolicy - 클러스터 전체에 적용
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
  name: deny-external-egress
spec:
  selector: all()
  types:
    - Egress
  egress:
    # 내부 Pod 통신 허용
    - action: Allow
      destination:
        nets:
          - 10.0.0.0/8
    # DNS 허용 (UDP/TCP 53)
    - action: Allow
      protocol: UDP
      destination:
        ports:
          - 53
    # 나머지 외부 통신 차단
    - action: Deny

---
# Calico NetworkSet - IP 범위 그룹화
apiVersion: projectcalico.org/v3
kind: GlobalNetworkSet
metadata:
  name: trusted-external-services
spec:
  nets:
    - 52.94.0.0/16      # AWS API
    - 35.190.0.0/16     # GCP API
    - 13.107.0.0/16     # Azure API

---
# NetworkSet 사용하는 정책
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
  name: allow-cloud-apis
spec:
  selector: app == 'api-gateway'
  types:
    - Egress
  egress:
    - action: Allow
      destination:
        selector: projectcalico.org/name == 'trusted-external-services'
      protocol: TCP
      destination:
        ports:
          - 443

🗣️ Say It Like This in Practice

💬 In a Kubernetes cluster design meeting
"We'll use Calico as the CNI. NetworkPolicy is fully supported, and by routing directly without an overlay in BGP mode, we can expect a 10-15% performance improvement compared to VXLAN. We only need to coordinate the BGP peering setup with the on-premises network team."
💬 In response to a security audit
"We implemented microsegmentation with Calico NetworkPolicy. The default policy is deny-all, and only the necessary communication is allowed through a whitelist. The frontend can access only port 8080 of the backend, and the backend can access only port 3306 of the DB. We are monitoring the policy enforcement status in real time with calicoctl."
💬 During Incident Response
"If Pod-to-Pod communication fails, first check the NetworkPolicy. Use kubectl describe networkpolicy to verify that the selector matches and the port is open, and also check the BGP connection status with calicoctl node status. Please check the Felix logs as well."

⚠️ Common Mistakes & Cautions

❌
Operating without NetworkPolicy

Without a NetworkPolicy, all Pods can communicate with each other. For security, apply a default-deny policy first and use a whitelist approach that allows only the necessary traffic.

❌
podSelector typo

If the podSelector labels in a NetworkPolicy do not match the actual Pod labels, the policy will not be applied. Use kubectl get pods --show-labels to verify the labels accurately.

❌
Blocked DNS traffic

When an Egress deny-all policy is applied, DNS (UDP 53) traffic is also blocked. Service name resolution fails, causing communication to fail. Be sure to allow DNS traffic going out to CoreDNS in kube-system.

✅
The right way to operate Calico

Allow only the necessary traffic after a default-deny policy. Always allow DNS egress. Regularly verify policies with calicoctl. Kernel 5.3+ is required when using eBPF mode. Set up monitoring of Felix and BIRD logs.

🔗 Related Terms

📚 Learn More