-
Using the PowerShell session, remove existing bookservice deployment and service with kubectl by executing the following commands:
+++kubectl delete deployment bookservice ; kubectl delete service bookservice+++
that will return
deployment.extensions "bookservice" deleted service "bookservice" deleted
-
Wait few seconds and then double check the results of the delete operation by executing:
+++kubectl get pod; kubectl get service+++
that confirm the lack of references to the bookservice pods and service
NAME READY STATUS RESTARTS AGE bookinfo-spa-57bdd84f98-92r2q 2/2 Running 0 39m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE bookinfo-spa LoadBalancer 10.0.75.9 104.42.174.161 80:30654/TCP 2d5h kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 3d2h
-
Execute the following command
+++kubectl apply -f C:\Labs\k8sconfigurations\blue-green\bookservice-blue-green.yaml+++
that will return
deployment.apps/bookservice-1.0 created deployment.apps/bookservice-2.0 created service/bookservice created
-
Double check the results of the apply operation by executing:
+++kubectl get deployment ; kubectl get service+++
Right now both environments, green (bookervice-v1.0) and blue (bookservice-2.0) are deployed, but only one will receive live traffic. In our case, the bookservice is configured to aim to the green environment. -
Take note of the External-IP reported in the output, it will be used in the next step
At this point we need to generate some HTTP traffic against the BookService API using the poller.ps1 PowerShell script; this will highlight the effects of the blue \ green deployment strategy once we proceed to execute the swap between green and blue version.
-
Start a NEW PowerShell as administrator and execute the following command to allow the execution of the poller.ps1 script
+++Set-ExecutionPolicy Unrestricted+++
as the following screenshot
-
Type "Yes" and then hit Enter to confirm the operation
-
Set the PowerShell $publicIP variable to the external IP reported by kubectl in the previous step by executing, in that case:
+++$publicIP = "[external-ip-of-bookinfospa-service]"+++
replacing "[external-ip-of-bookinfospa-service]" with the external IP of your bookinfo-spa service as well.
-
The poller.ps1 script will make two requests on the BookService WebAPI, first calling the /reviews/1 (all the reviews of the Book with ID 1) then the /review/2 endpoint (all the reviews of the Book with ID 2), by executing:
+++C:\Labs\Lab_Modules\Tools\Poller.ps1 -PublicIP $publicIP+++
As you can see below
the script runs a loop that provides two requests each seconds until you terminates it using Ctrl+C shortcut
Don't terminate the script, so you can easily view the effects of the blue version deployment in the next steps.
K8s use selectors to bind a service to a deployment. Looking at the file \k8sconfigurations\blue-green\bookservice-blue-green.yaml we can notice that bookservice will forward traffic the deployment marked as green.
apiVersion: v1
kind: Service
metadata:
name: bookservice
spec:
ports:
- port: 80
selector:
app: bookservice
deployment: green
The blue version of the BookService API contains an error in the application logic that will raise an exception when loading reviews for the BookId = 2 and BookId = 4, while it will work correctly for BookId = 1 and BookId = 3.
In order to switch between the faulty and healthy version we just have to change the deployment property value, replacing green with blue.
Keep the two powershell sessions side by side and execute the following commands to proceed:
-
Prepare the new $spec and $specJson variables by executing the following two commands:
+++$spec = '{"spec":{"selector":{"deployment":"blue"}}}'; $specJson = $spec | ConvertTo-Json+++
then execute kubectl patch command, passing the Json variable as input parameter to the -p switch:
+++kubectl patch service bookservice -p $specJson+++
-
As you can see below, as soon as the service/bookservice aimed to the blue environment, the poller.ps1 script started to receive HTTP 500 (Internal Server Error status - the server encountered an unexpected error) as final status code, indicating a failure
You can notice that with Blue\Green deployment strategy we immediately switched from a version to another and hence is very useful to reduce\remove deployment downtime. Unfortunately, the switch to the new version introduced a bug in the code, but thanks to the Kubernetes infrastructure we can now easily perform a rollback.
-
Having the two environments blue and green still up & running side by side, rolling back to the healthy version (green) is straightforward.
Prepare the new $spec and $specJson variables by executing the following two commands:
+++$spec = '{"spec":{"selector":{"deployment":"green"}}}' ; $specJson = $spec | ConvertTo-Json+++
then execute kubectl patch command, passing the Json variable as input parameter to the -p switch:
+++kubectl patch service bookservice -p $specJson+++
as soon as k8s complete the patch operation, the BookService API started to send again HTTP 200 (Status OK) for the request related to the BookId = 2.