Scenario
Your team has a stable deployment app-v1 running in namespace canary. They want to test a new version (v2) with approximately 1/3 of traffic going to v2 and 2/3 remaining on v1.
Task
Implement canary traffic splitting between both versions using only native Kubernetes resources.
| Property |
Value |
| Namespace |
canary |
| Existing deployment |
app-v1 |
| Canary deployment |
app-v2 |
| Canary image |
nginx:1.25 |
| Service name |
my-app-svc |
| Service port |
80 |
A reference deployment file is available at /home/interview/deployment.yaml.
Step 1: Check existing deployment
kubectl get deployment app-v1 -n canary -o yaml
cat /home/interview/deployment.yaml
Note that app-v1 pods already have label app=my-app.
Step 2: Create canary deployment (v2) with 1 replica
Copy and modify the reference file:
cp /home/interview/deployment.yaml /home/interview/app-v2.yaml
Edit app-v2.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-v2
namespace: canary
spec:
replicas: 1
selector:
matchLabels:
deployment: app-v2
template:
metadata:
labels:
app: my-app # Same common label as v1
version: v2
deployment: app-v2
spec:
containers:
- name: nginx
image: nginx:1.25
kubectl apply -f /home/interview/app-v2.yaml
Step 3: Create service that selects the common label
kubectl create service clusterip my-app-svc -n canary --tcp=80:80 --dry-run=client -o yaml > /home/interview/svc.yaml
Edit /home/interview/svc.yaml to use the common selector:
apiVersion: v1
kind: Service
metadata:
name: my-app-svc
namespace: canary
spec:
selector:
app: my-app # Selects pods from BOTH deployments
ports:
- port: 80
targetPort: 80
kubectl apply -f /home/interview/svc.yaml
Step 4: Verify
kubectl get pods -n canary -l app=my-app -o wide
kubectl get endpoints my-app-svc -n canary
The endpoints should show 3 pod IPs (2 from v1, 1 from v2).