ENع
→K8s networking
الدرس 10 · الحاويات وKubernetes

Kubernetes networking

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

Every Pod in Kubernetes gets its own IP and can reach every other Pod directly, with no address translation. But Pods are replaced all the time, so their IPs keep changing. A Service fixes that with one stable virtual IP and a DNS name. Follow one request from a Python client: DNS turns api into the Service's ClusterIP, kube-proxy's rules on the node rewrite it to a ready Pod, and the reply comes back looking as if the Service answered.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 10 · تفاعلي
01 / 10

Every Pod gets an IP

A Pod is the smallest thing Kubernetes runs: one or more containers that share one network address. Each Pod gets its own IP. Pods on node 1 get 10.244.1.x and Pods on node 2 get 10.244.2.x. Containers inside one Pod talk over localhost.

cluster Pod network   10.244.0.0/16
  node-1  10.244.1.0/24
  node-2  10.244.2.0/24

  web Pod    10.244.1.3
  api Pod 1  10.244.1.4
  api Pod 2  10.244.2.5

containers in one Pod → localhost
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Every Pod gets an IP

    A Pod is the smallest thing Kubernetes runs: one or more containers that share one network address. Each Pod gets its own IP. Pods on node 1 get 10.244.1.x and Pods on node 2 get 10.244.2.x. Containers inside one Pod talk over localhost.

    cluster Pod network   10.244.0.0/16
      node-1  10.244.1.0/24
      node-2  10.244.2.0/24
    
      web Pod    10.244.1.3
      api Pod 1  10.244.1.4
      api Pod 2  10.244.2.5
    
    containers in one Pod → localhost
  2. Pod to Pod, no NAT

    Any Pod can reach any other Pod at its IP, even on another node, with no NAT (address rewriting) on the way. Kubernetes requires this; a network plugin (CNI) builds it. Here web sends straight to api Pod 2 at 10.244.2.5.

    web 10.244.1.3  →  api Pod 2 10.244.2.5
      node-1 → node-2, address unchanged
    
    the Kubernetes network model:
      every Pod has its own IP
      every Pod reaches every Pod, no NAT
      built by a CNI plugin (Flannel, Calico, Cilium…)
  3. Pods come and go

    Pods are disposable. When api Pod 2 is deleted, its node fails, or it is updated, a fresh Pod replaces it and gets a new IP: 10.244.2.5 becomes 10.244.2.6. Code that saved the old address now fails. Clients need a name that stays put.

    api Pod 2   10.244.2.5   ✗ deleted
    new Pod     10.244.2.6   ✓ running
    
    web still sends to 10.244.2.5 → no answer
    Pod IPs are not stable: never hard-code them
  4. A Service: one stable address

    A Service is a fixed front door for a group of Pods. This one, named api, picks every Pod labelled app: api. It gets a virtual IP, its ClusterIP (here 10.96.0.20), which stays the same for as long as the Service exists.

    kubectl apply -n shop -f api-svc.yaml
    
    Service api   ClusterIP 10.96.0.20   (from 10.96.0.0/12)
      selector  app: api   → api Pod 1, api Pod 2
      port 80   → targetPort 8080 on each Pod
      type: ClusterIP (the default, inside the cluster only)
  5. EndpointSlice: who is ready

    Kubernetes keeps a live list of the ready Pods behind each Service, called an EndpointSlice. When a Pod is replaced, or fails its readiness check, the list updates by itself. Nobody edits it by hand.

    EndpointSlice for Service api
      10.244.1.4:8080   ready
      10.244.2.6:8080   ready
    
    Pod replaced or not ready → list changes
    kept up to date by the control plane
  6. DNS turns api into an IP

    Line 2 asks for http://api/items. Inside a Pod, the short name api expands to api.shop.svc.cluster.local. The cluster's DNS server, CoreDNS, answers with the Service's ClusterIP, 10.96.0.20.

    urlopen("http://api/items")
    
    /etc/resolv.conf search: shop.svc.cluster.local …
      api → api.shop.svc.cluster.local
      CoreDNS: api.shop.svc.cluster.local = 10.96.0.20
  7. Send to the ClusterIP

    The web Pod sends its request to 10.96.0.20, port 80. No machine owns that address: it is virtual, and nothing listens on it. Before the packet leaves node 1, the node's own packet rules catch it.

    packet  10.244.1.3 → 10.96.0.20:80
    
    the ClusterIP is virtual: no Pod, no machine
    node-1 rules match it on the way out
  8. kube-proxy picks a Pod

    kube-proxy, a helper on every node, turned the EndpointSlice into these rules. They pick one ready Pod at random and rewrite the destination: 10.96.0.20:80 becomes 10.244.2.6:8080. This rewrite is called DNAT.

    kube-proxy (iptables mode, the Linux default)
      EndpointSlice → rules for 10.96.0.20:80
      pick one ready endpoint at random
    
    DNAT  10.96.0.20:80  →  10.244.2.6:8080
  9. Delivered, and back

    Now it is a plain Pod-to-Pod packet, so it crosses to node 2 and api Pod 2 answers. On the way back the rules undo the rewrite, so web sees a reply from 10.96.0.20. Line 3 prints 200.

    10.244.1.3 → 10.244.2.6:8080   (Pod to Pod)
    reply 10.244.2.6 → rules → web
      source rewritten back to 10.96.0.20:80
    
    print(r.status)  →  200
  10. The next call may differ

    Each new connection is a fresh random pick, so the load spreads over both Pods. Start a third Pod labelled app: api and it joins the EndpointSlice: the Service grows with no change to the code in web.

    new connection → 10.96.0.20:80
      this time → 10.244.1.4:8080 (api Pod 1)
    
    more Pods with app: api → more endpoints
    web's code never changes

اختبار يتاح بعد الخطوة الأخيرة.