Sobes.tech
Middle+

How to manually implement canary deployment in Kubernetes without additional tools?

sobes.tech AI

Answer from AI

For manual implementation of canary deployment in Kubernetes without additional tools, you can use a phased update strategy with traffic control through the number of replicas.

Main idea:

  1. Create a new Deployment with the new application version, but with a small number of replicas (e.g., 1 out of 5).
  2. Configure a Service to balance traffic between the old and new version pods (usually, the Service selects all pods with the required selector).
  3. Monitor the behavior of the new version (logs, metrics, errors).
  4. If everything is fine — gradually increase the number of replicas of the new version and decrease the old one.
  5. Ultimately, replace the old version entirely with the new one.

Example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 4 # old version
  selector:
    matchLabels:
      app: myapp
      version: stable
  template:
    metadata:
      labels:
        app: myapp
        version: stable
    spec:
      containers:
      - name: myapp
        image: myapp:stable
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-canary
spec:
  replicas: 1 # canary
  selector:
    matchLabels:
      app: myapp
      version: canary
  template:
    metadata:
      labels:
        app: myapp
        version: canary
    spec:
      containers:
      - name: myapp
        image: myapp:canary
---
apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  selector:
    app: myapp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080

In this example, the Service directs traffic to all pods with the label app: myapp, including both stable and canary versions. By adjusting the number of replicas, you can control the traffic share to the new version.

Thus, without complex tools, you can manually manage canary deployment by gradually increasing the load on the new version and decreasing it on the old one.