study.

Select Canary Backends with Labels and a Service · CKAD

Let one Service select ready Pods from two Deployments.

← CKAD

Learn with progress and quizzes

First, understand the concepts.

Separate deployment groups

Use a track label so stable and canary Deployment selectors do not overlap. A shared app label lets one Service select both groups.

Replicas and traffic

Three stable Pods and one canary change the backend mix, but do not guarantee exactly 75:25 request distribution. Connection reuse and networking behavior matter.

Change the selection

Adding track: stable to the Service selector excludes canary backends. Treat application deployment and traffic selection as separate concerns.

Why it matters

To include a new version among backends, separate versioned workloads from traffic selection. Labels classify; selectors choose which classifications receive traffic.

Work through the reasoning

Compare selector scope

Use an app label for shared identity and track for stable versus canary. A stable track selector limits backends; selecting only the shared app can include ready Pods from both versions.

Separate counts from traffic shares

EndpointSlice addresses describe the backend set. Connection reuse, affinity, and implementation affect request distribution, so two stable Pods and one canary do not guarantee a 2:1 request split.

Learn by doing.

Use kubectl, a personal practice Kubernetes cluster, and a POSIX shell. Each lab uses its own namespace. Use example values only in Secrets. Cleanup removes the entire practice namespace.

Create three stable and one canary replica, select both with the shared app label, then select only stable and compare EndpointSlices.

kubectl config current-context
kubectl create namespace study-ckad-canary --dry-run=client -o yaml | kubectl apply -f -
kubectl apply -n study-ckad-canary -f - <<'YAML'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: shop-stable
spec:
  replicas: 3
  selector:
    matchLabels: {app: shop, track: stable}
  template:
    metadata:
      labels: {app: shop, track: stable}
    spec:
      containers:
      - name: web
        image: nginx:stable-alpine
        command: ["sh", "-c", "echo stable > /usr/share/nginx/html/index.html; exec nginx -g 'daemon off;'"]
        readinessProbe:
          httpGet: {path: /, port: 80}
          periodSeconds: 2
YAML
kubectl apply -n study-ckad-canary -f - <<'YAML'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: shop-canary
spec:
  replicas: 1
  selector:
    matchLabels: {app: shop, track: canary}
  template:
    metadata:
      labels: {app: shop, track: canary}
    spec:
      containers:
      - name: web
        image: nginx:stable-alpine
        command: ["sh", "-c", "echo canary > /usr/share/nginx/html/index.html; exec nginx -g 'daemon off;'"]
        readinessProbe:
          httpGet: {path: /, port: 80}
          periodSeconds: 2
YAML
kubectl apply -n study-ckad-canary -f - <<'YAML'
apiVersion: v1
kind: Service
metadata:
  name: shop
spec:
  selector: {app: shop}
  ports:
  - {port: 80, targetPort: 80}
YAML
kubectl rollout status deployment/shop-stable -n study-ckad-canary --timeout=120s
kubectl rollout status deployment/shop-canary -n study-ckad-canary --timeout=120s
kubectl get pods -n study-ckad-canary --show-labels
kubectl get endpointslices -n study-ckad-canary -l kubernetes.io/service-name=shop -o yaml
kubectl patch service shop -n study-ckad-canary --type=merge -p '{"spec":{"selector":{"app":"shop","track":"stable"}}}'
for study_attempt in 1 2 3 4 5 6 7 8 9 10; do
  study_count=$(kubectl get endpointslices -n study-ckad-canary -l kubernetes.io/service-name=shop -o jsonpath='{range .items[*].endpoints[*]}{.targetRef.name}{"\n"}{end}' | wc -l)
  test "$study_count" -eq 3 && break
  sleep 1
done
test "$study_count" -eq 3
kubectl get endpointslices -n study-ckad-canary -l kubernetes.io/service-name=shop -o yaml

How to check your result

Expect four ready backends initially and three stable backends after the selector change. EndpointSlice updates can take a moment. This lab verifies backend selection, not statistical traffic percentages.

Clean up practice resources

Run after checking the result. Confirm these resources belong to your practice run and contain no data you need to keep.

kubectl delete namespace study-ckad-canary

Apply what you learned

If exactly 10% of requests should reach the new version, are replica counts sufficient?

Reveal the reasoning after trying

No. Consider weighted routing in a suitable gateway or proxy and verify actual request metrics. This lab teaches backend selection rather than precise traffic weighting.

Ready to check your understanding?

What cannot be guaranteed by a 3:1 Pod ratio alone?

  1. Different track labels
  2. Separate Deployments
  3. Exactly 75:25 request distribution
  4. Selecting backends with labels
Reveal the reasoning after trying

Exactly 75:25 request distribution

A replica ratio does not guarantee an exact per-request traffic split. Use a routing layer with the required control if precise distribution is needed.

Official documentation · Select Canary Backends with Labels and a Service