Configure Traffic Split between Two Services
We will now demonstrate how to balance traffic between two Kubernetes services, commonly known as a traffic split. We will be splitting the traffic directed to the root bookstore
service between the backends bookstore
service and bookstore-v2
service.
Deploy bookstore v2 application
To demonstrate usage of SMI traffic access and split policies, we will now deploy version v2 of the bookstore application (bookstore-v2
) - remember that if you are using openshift, you must add the security context constraint to the bookstore-v2 service account as specified in the installation guide.
# Contains the bookstore-v2 Kubernetes Service, Service Account, Deployment and SMI Traffic Target resource to allow
# `bookbuyer` to communicate with `bookstore-v2` pods
kubectl apply -f https://raw.githubusercontent.com/openservicemesh/osm-docs/release-v1.2/manifests/apps/bookstore-v2.yaml
Wait for the bookstore-v2
pod to be running in the bookstore
namespace. Next, exit and restart the port-forward scripts in order to access v2 of bookstore:
bash <<EOF
./scripts/port-forward-bookbuyer-ui.sh &
./scripts/port-forward-bookstore-ui.sh &
./scripts/port-forward-bookstore-ui-v2.sh &
./scripts/port-forward-bookthief-ui.sh &
wait
EOF
- http://localhost:8082 - bookstore-v2
The counter should not be incrementing because no traffic is flowing yet to the bookstore-v2
service.
Create SMI Traffic Split
Deploy the SMI traffic split policy to direct 100 percent of the traffic sent to the root bookstore
service to the bookstore
service backend. This is effectively the same as not having a TrafficSplit
configuration as we are directing 100 percent of traffic from the root service to itself. However, the TrafficSplit
configuration will be subsequently updated to direct a percentage of traffic to version v2
of the bookstore service using the bookstore-v2
leaf service. For this reason, it is important to ensure client applications always communicate with the root service if a traffic split is desired later on, otherwise the client application will need to be updated to communicate with the root service when a traffic split is desired.
kubectl apply -f https://raw.githubusercontent.com/openservicemesh/osm-docs/release-v1.2/manifests/split/traffic-split-v1.yaml
Note: The root service is a Kubernetes service whose selector needs to match the pods backing the leaf services. In this demo, the root service bookstore
has the selector app:bookstore
, which matches both the labels app:bookstore,version:v1
and app:bookstore,version=v2
on the bookstore (v1)
and bookstore-v2
deployments respectively. The root service can be referred to in the SMI Traffic Split resource as the name of the service with or without the .<namespace>.svc.cluster.local
suffix.
The count for the books sold from the bookstore-v2
browser window should remain at 0. This is because the current traffic split policy is currently weighted 100 for bookstore
in addition to the fact that bookbuyer
is sending traffic to the bookstore
service and no application is sending requests to the bookstore-v2
service. You can verify the traffic split policy by running the following and viewing the Backends properties:
kubectl describe trafficsplit bookstore-split -n bookstore
Split Traffic to Bookstore v2
Update the SMI Traffic Split policy to direct 50 percent of the traffic sent to the root bookstore
service to the bookstore
service and 50 perfect to bookstore-v2
service by adding the bookstore-v2
backend to the spec and modifying the weight fields.
kubectl apply -f https://raw.githubusercontent.com/openservicemesh/osm-docs/release-v1.2/manifests/split/traffic-split-50-50.yaml
Wait for the changes to propagate and observe the counters increment for bookstore
and bookstore-v2
in your browser windows. Both
counters should be incrementing:
- http://localhost:8084 - bookstore
- http://localhost:8082 - bookstore-v2
Split All Traffic to Bookstore v2
Update the bookstore-split
TrafficSplit to configure all traffic to go to bookstore-v2
:
kubectl apply -f https://raw.githubusercontent.com/openservicemesh/osm-docs/release-v1.2/manifests/split/traffic-split-v2.yaml
Wait for the changes to propagate and observe the counters increment for bookstore-v2
and freeze for bookstore
in your
browser windows:
- http://localhost:8082 - bookstore-v2
- http://localhost:8083 - bookstore
Now, all traffic directed to the bookstore
service is flowing to bookstore-v2
.
Next Steps
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.