After you configure traffic shifting, a request is forwarded to another set of service endpoints specified in advance if the destination service of the request has no available endpoints. This topic describes how to configure traffic shifting.
Prerequisites
The preparations are completed, and the helloworld and sleep services are deployed. For more information, see Preparations.
Procedure
Use the following content to configure a destination rule. For more information, see Manage destination rules.
apiVersion: networking.istio.io/v1beta1 kind: DestinationRule metadata: name: helloworld namespace: default spec: host: helloworld subsets: - labels: version: v1 name: v1 - labels: version: v2 name: v2
Use the following content to create the following virtual service: All requests destined for the helloworld service are sent to the v1 subset of the helloworld service. If the v1 subset has no available endpoints, the requests are shifted to the v2 subset of the helloworld service. For more information, see Manage virtual services.
Check whether the traffic shifting configuration takes effect.
Use kubectl to connect to the Container Service for Kubernetes (ACK) cluster based on the information in the kubeconfig file, and then run the following command:
kubectl exec deploy/sleep -- sh -c "for i in \$(seq 1 100); do curl -s http://helloworld:5000/hello; done"
Expected output:
Hello version: v1, instance: helloworld-v1-9585666c8-8**** Hello version: v1, instance: helloworld-v1-9585666c8-8**** Hello version: v1, instance: helloworld-v1-9585666c8-8**** Hello version: v1, instance: helloworld-v1-9585666c8-8**** ... Hello version: v1, instance: helloworld-v1-9585666c8-8****
The output indicates that when the v1 subset of the helloworld service has available endpoints, all requests are sent to the v1 subset of the helloworld service.
Scale in the number of pods for the v1 subset of the helloworld service to 0 to simulate a fault.
Log on to the ACK console. In the left-side navigation pane, click Clusters.
On the Clusters page, click the name of the cluster that you want to manage and choose in the left-side navigation pane.
On the Deployments page, find the workload for helloworld-v1 and click Scale in the Actions column. In the Scale dialog box, set Desired Number of Pods to 0 and click OK.
Use kubectl to connect to the ACK cluster based on the information in the kubeconfig file, and then run the following command to verify access to the helloworld service:
kubectl exec deploy/sleep -- sh -c "for i in \$(seq 1 100); do curl -s http://helloworld:5000/hello; done"
Expected output:
Hello version: v2, instance: helloworld-v2-68bb8c995b-t**** Hello version: v2, instance: helloworld-v2-68bb8c995b-t**** ... Hello version: v2, instance: helloworld-v2-68bb8c995b-t**** Hello version: v2, instance: helloworld-v2-68bb8c995b-t**** Hello version: v2, instance: helloworld-v2-68bb8c995b-t****
The output indicates that when the v1 subset of the helloworld service has no available endpoints, all requests are sent to the v2 subset of the helloworld service. This indicates that the traffic shifting configuration takes effect.
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: helloworld
spec:
hosts:
- helloworld
http:
- route:
- destination:
host: helloworld
subset: v1
fallback:
target:
host: helloworld
subset: v2