在服務部署後,常會對服務進行修正與更新,產生出一版又一版的新服務。在這情況下,會希望能夠測試確認新服務沒有問題再進行更新。測試的方法有很多種,包括前面介紹的金絲缺部屬,不過這邊我想介紹另一個功能-鏡像流量
鏡像流量是一種將生產環境的真實流量複製一份到新版本,用以測試新版本的穩定與功能是否正常,或者將流量導至一個有安全檢查或監控的環境,作為導入前的測試,以此在實際部屬前,得知新加入功能的效果。
kubectl apply -f samples/sleep/sleep.yaml
apiVersion: v1
kind: Service
metadata:
name: helloworld-service
spec:
type: ClusterIP
selector:
app: helloworld
ports:
- protocol: TCP
name: http
port: 5000
targetPort: 5000
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: helloworld-v1
labels:
app: helloworld
spec:
replicas: 1
selector:
matchLabels:
app: helloworld
version: v1
template:
metadata:
labels:
app: helloworld
version: v1
spec:
containers:
- name: helloworld-v1
image: allenku0/hello-api:v1
ports:
- containerPort: 5000
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: helloworld-v2
labels:
app: helloworld
spec:
replicas: 1
selector:
matchLabels:
app: helloworld
version: v2
template:
metadata:
labels:
app: helloworld
version: v2
spec:
containers:
- name: helloworld-v2
image: allenku0/hello-api:v2
ports:
- containerPort: 5000
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: test-to-hello
spec:
hosts:
- helloworld-service
http:
- match:
- uri:
prefix: /api
route:
- destination:
host: helloworld-service
subset: v1
port:
number: 5000
mirror:
host: helloworld-service
subset: v2
mirrorPercentage:
value: 100.0
其中的mirror 是代表要複製流量的目標服務,這邊指定v2的service,並且設定mirrorPercentage來決定鏡像流量的比例,這邊為了方便測試,鏡像了100%的流量。
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: test-to-hello
spec:
host: helloworld-service
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
kubectl exec -it sleep-7656cf8794-rxvs5 -- sh
curl -s http://helloworld-service:5000/api/details