متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 247Kubernetes · Workloads

How to do a canary release in Kubernetes

A canary release sends only a small share of real users to the new version first — 10 %, then 50 %, then all — while you watch errors and latency. If the canary looks bad, only a few users were affected. This how-to does a simple canary with two Deployments behind one Service (share by replica count), then a precise one with ingress-nginx weights, and names the tools that automate it.

  • 5 الدروس
  • 2 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

247.1

The quick answer

kubectl -n shop apply -f api-canary.yaml
kubectl -n shop scale deploy/api-stable --replicas=9
kubectl -n shop scale deploy/api-canary --replicas=1
kubectl -n shop scale deploy/api-canary --replicas=0

(9 + 1 Pods ≈ 10 % canary traffic; the last line stops the canary.)

الأوامر في هذا الدرس

247.2

Step by step

  1. Two Deployments, shared label — api-stable and api-canary both have app: api (plus track: stable|canary); the Service selects only app: api, so it spreads traffic over all Pods. The canary's share is its share of Pods.
  2. Start small (quick answer) — 1 canary Pod of 10. Watch the canary's metrics separately (by the track label): error rate, latency p95, logs.
  3. Increase — 3 of 10, then 5 of 10, then make the canary the new stable (update api-stable's image, scale the canary to 0).
  4. Abort — scale the canary to 0 (last line); users go back to stable at once.
  5. Exact percentages — Pod counts are rough. ingress-nginx can split by weight with a second Ingress:
ingress-canary.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: api-canary
  annotations:
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "10"
spec:
  ingressClassName: nginx
  rules:
    - host: api.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service: { name: api-canary, port: { number: 80 } }

Here api-canary is a separate Service that selects only track: canary. Change the weight to 25, 50, 100. A header (canary-by-header) lets testers reach the canary on purpose.

  1. Decide with numbers — before the release, write down the rule: "abort if canary error rate > 1 % or p95 > 400 ms for 5 minutes".
  2. Automate — Argo Rollouts or Flagger run the steps, query Prometheus, and roll back automatically when the metrics are bad. Gateway API HTTPRoute weights are the newer standard way to split traffic.

247.3

Check that it worked

kubectl -n shop get pods -l app=api -L track shows 9 stable and 1 canary Pod; the request logs or metrics by track show about 10 % of requests on the canary; with the ingress weight at 10, about 1 in 10 test requests (for i in $(seq 100); do curl -s https://api.example.com/version; done | sort | uniq -c) answers with the new version.

الأوامر في هذا الدرس

247.4

Common problems

ProblemCauseFix
Share is not what you expectedTraffic split by Pods, connections are sticky/long.Use ingress weights; expect rough numbers with replica splits.
Canary metrics hidden in the averageMetrics not split by version.Label metrics/logs with track or version.
Canary ingress ignoredAnnotations on the main Ingress, or a different host/path.Separate Ingress with the same host and path plus canary annotations.
Users switch between versionsEach request may hit another Pod.Session affinity, or make both versions compatible.
Bad canary not noticedNo decision rule, nobody watching.Written abort rule; automated analysis (Argo Rollouts/Flagger).
Schema change breaks stableCanary migrated the database.Expand/contract migrations.

247.5

Go further

1How do two Deployments behind one Service make a canary?

Both share the label the Service selects, so traffic is spread over all their Pods; the canary's share is its share of Pods.

2Why write the abort rule before the release?

A clear rule (for example error rate above 1 % for 5 minutes) makes the stop decision fast and objective.

كل الأوامر في هذا الفصل