Cấu hình mạng Calico và nút Worker cho Kubernetes HA

Giới thiệu

Bài viết này trình bày cách cấu hình mạng Calico và cài đặt các nút Worker để xây dựng một cụm Kubernetes cao cấp sử dụng kubeadm. Chúng ta sẽ tìm hiểu về kiến trúc mạng, thiết lập BGP, quản lý nút Worker và chiến lược điều độ Pod.

Kiến trúc mạng Calico cao cấp

Mạng Calico hỗ trợ ba chế độ chính:

  • IPIP (cross-subnet): Tăng thêm ~10% chi phí đóng gói, trễ 1.5ms, băng thông 9Gbps.
  • VXLAN (complex networks): Chi phí đóng gói ~15%, trễ 2ms, băng thông 8Gbps.
  • BGP (high performance): Không có chi phí đóng gói, trễ 1ms, băng thông 10Gbps.

Sơ đồ kiến trúc


┌─────────────────────────────────────────────────────────┐
│  Kiến trúc mạng Calico cao cấp                          │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  Lớp Control Plane                                      │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐             │
│  │ Master 1 │  │ Master 2 │  │ Master 3 │             │
│  │ + BIRD   │  │ + BIRD   │  │ + BIRD   │             │
│  └────┬─────┘  └────┬─────┘  └────┬─────┘             │
│       │             │             │                    │
│       └─────────────┼─────────────┘                    │
│                     │                                  │
│              Bộ phản chiếu BGP                           │
│              (Route Reflector)                          │
│                                                         │
│  Lớp Node Worker                                        │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐             │
│  │  Node 1  │  │  Node 2  │  │  Node N  │             │
│  │ + Felix  │  │ + Felix  │  │ + Felix  │             │
│  │ + BIRD   │  │ + BIRD   │  │ + BIRD   │             │
│  └──────────┘  └──────────┘  └──────────┘             │
└─────────────────────────────────────────────────────────┘

Cài đặt Calico trong chế độ BGP

Bước 1: Cài đặt Calico Operator

#!/bin/bash

echo "=== Cài đặt Calico trong chế độ BGP ==="

# Cài đặt operator
curl https://docs.projectcalico.org/manifests/tigera-operator.yaml | kubectl apply -f -

# Thiết lập tài nguyên tùy chỉnh
cat > calico-bgp.yaml << EOF
apiVersion: operator.tigera.io/v1
kind: Installation
metadata:
  name: default
spec:
  variant: Calico
  registry: quay.io
  
  calicoNetwork:
    ipPools:
    - blockSize: 26
      cidr: 10.244.0.0/16
      encapsulation: None
      natOutgoing: Enabled
      nodeSelector: all()
    
    nodeAddressAutodetectionV4:
      firstFound: true
    
    bgp: Enabled
  
  controlPlaneNodeSelector:
    node-role.kubernetes.io/control-plane: ""
  
  componentResources:
  - componentName: Node
    resourceRequirements:
      requests:
        cpu: 150m
        memory: 64Mi
      limits:
        cpu: 2000m
        memory: 512Mi
  - componentName: Typha
    resourceRequirements:
      requests:
        cpu: 100m
        memory: 32Mi
      limits:
        cpu: 500m
        memory: 256Mi
EOF

kubectl apply -f calico-bgp.yaml

Bước 2: Cấu hình đồng đẳng BGP

apiVersion: projectcalico.org/v3
kind: BGPPeer
metadata:
  name: peer-to-rack
spec:
  peerIP: 192.168.1.254
  asNumber: 64512
  nodeSelector: rack == 'rack1'
---
apiVersion: projectcalico.org/v3
kind: BGPPeer
metadata:
  name: peer-to-leaf
spec:
  peerSelector: role == 'leaf'
  asNumber: 64513

Cấu hình nút Worker cao cấp

Thêm nút Worker vào cụm

#!/bin/bash

echo "=== Thêm nút Worker ==="

kubeadm join 192.168.1.200:6443 \
  --token abcdef.0123456789abcdef \
  --discovery-token-ca-cert-hash sha256:1234567890abcdef...

kubectl get nodes

Quản lý nhãn và vết bẩn

kubectl label node node1 node-type=worker
kubectl label node node1 environment=production
kubectl label node node1 zone=us-east-1a

kubectl taint node node1 dedicated=worker:NoSchedule

kubectl get nodes --show-labels

Chiến lược điều độ Pod cao cấp

Triển khai nhiều bản sao Pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: high-availability-app
  namespace: production
spec:
  replicas: 3
  selector:
    matchLabels:
      app: ha-app
  template:
    metadata:
      labels:
        app: ha-app
        version: v1
    spec:
      containers:
      - name: ha-app
        image: ha-app:v1
        resources:
          requests:
            cpu: "100m"
            memory: "128Mi"
          limits:
            cpu: "500m"
            memory: "512Mi"
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
          initialDelaySeconds: 10
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /ready
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 5

Phân tán Pod giữa các nút

affinity:
  podAntiAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - labelSelector:
        matchExpressions:
        - key: app
          operator: In
          values:
          - ha-app
      topologyKey: kubernetes.io/hostname
    preferredDuringSchedulingIgnoredDuringExecution:
    - weight: 100
      podAffinityTerm:
        labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - ha-app
        topologyKey: topology.kubernetes.io/zone

Lựa chọn phân bố theo vùng

topologySpreadConstraints:
- maxSkew: 1
  topologyKey: topology.kubernetes.io/zone
  whenUnsatisfiable: ScheduleAnyway
  labelSelector:
    matchLabels:
      app: ha-app
- maxSkew: 1
  topologyKey: kubernetes.io/hostname
  whenUnsatisfiable: DoNotSchedule
  labelSelector:
    matchLabels:
      app: ha-app

Chính sách mạng và bảo mật

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny
  namespace: production
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  - Egress
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-frontend-to-backend
  namespace: production
spec:
  podSelector:
    matchLabels:
      app: backend
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: frontend
    ports:
    - protocol: TCP
      port: 8080
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dns
  namespace: production
spec:
  podSelector: {}
  policyTypes:
  - Egress
  egress:
  - to:
    - namespaceSelector: {}
      podSelector:
        matchLabels:
          k8s-app: kube-dns
    ports:
    - protocol: UDP
      port: 53

Tối ưu hóa hiệu suất mạng

Tối ưu MTU

ip link show | grep mtu

cat > calico-mtu.yaml << EOF
apiVersion: projectcalico.org/v3
kind: Installation
metadata:
  name: default
spec:
  calicoNetwork:
    mtu: 1500
EOF

kubectl apply -f calico-mtu.yaml

Tối ưu kết nối

cat >> /etc/sysctl.d/k8s-network.conf << EOF
net.netfilter.nf_conntrack_max = 1000000
net.nf_conntrack_max = 1000000
net.netfilter.nf_conntrack_tcp_timeout_established = 432000
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 30
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 30
EOF

sysctl --system

Thử nghiệm hiệu suất

kubectl run test-pod-1 --image=busybox --rm -it --restart=Never -- ping -c 10 <target-pod-ip>

kubectl run iperf-server --image=networkstatic/iperf3 --rm -it --restart=Never -- iperf3 -s

kubectl run iperf-client --image=networkstatic/iperf3 --rm -it --restart=Never -- iperf3 -c <server-ip>

kubectl run dns-test --image=busybox --rm -it --restart=Never -- time nslookup kubernetes.default

Thẻ: Kubernetes calico BGP

Đăng vào ngày 24 tháng 9 lúc 13:28