This tutorial shows you how to deploy, scale, update and rollback your application on Red Hat OpenShift. All of these features are essential to make sure that the developer can deliver their software safely in case of adding new features, updating images, fixing bugs or rolling back an update.
For this tutorial you will need:
- Red Hat OpenShift Cluster 4.3 on IBM Cloud.
- oc CLI (can be downloaded from this link or you can use it at http://shell.cloud.ibm.com/.
It will take you around 30 minutes to complete this tutorial.
- Create Application From Existing Docker Image
- Scale Applications Using Replicas
- Update Application
- Roll back Application
1- Create a new project using the following command.
oc create namespace guestbook-project
2- Create a new deployment resource using the ibmcom/guestbook:v1 docker image in the project we just created.
oc create deployment myguestbook --image=ibmcom/guestbook:v1
3- This deployment creates the corresponding Pod that's in running state. Use the following command to see the list of pods in your namespace
oc get pods
4- Expose the deployment on port 3000
oc expose deployment myguestbook --type="NodePort" --port=3000
To view the service we just exposed. Use the following command.
oc get service
Note that the service inside the pod is accessible using the :, but in case of OpenShift on IBM Cloud the NodeIP is not publicly accessible. One can use the built-in kubernetes terminal available in IBM Cloud which spawns a kubernetes shell which is part of the OpenShift cluster network and NodeID:NodePort is accessible from that shell.
5- To make the exposed service publicly accessible, you will need to create a public router. First, go to Networking → Routes from the Administrator Perspective on the web console, then click 'Create Route'.
Fill in the information as follows:
Name:myguestbook
Service:myguestbook
Target Port:3000→3000(TCP)
Then click 'Create', you can leave the rest of the fields empty.
Once created, you will be redirected to 'Route Overview' where you can access the external route from the URL under Location as shown in the screenshot. The Route has been created successfully, and it provides a publicly accessible endpoint URL using which we can access our guestbook application.
If you click on the URL, you will be redirected to a page that looks like the following.
Now that you have successfully deployed the application using existing docker image, you will be learning how to scale and rollback your application
In this section, you will be scaling applications by creating replicas of the pod you created in the first section. Having multiple replicas of a pod can help you ensure that your deployment has the available resources to handle increasing load on your application.
1- Increase the capacity from a single running instance of guestbook up to 5 instances.
oc scale --replicas=5 deployment/myguestbook
2- Check the status of the deployment
oc rollout status deployment myguestbook
3- Verify that you have 5 pods running post the oc scale
command.
oc get pods -n guestbook-project
Red Hat OpenShift allows you to do rolling upgrade of your application to a new container image. This allows you to easily update the running image and to easily undo a rollout if a problem is discovered during or after the deployment. In this section, you will be upgrading the image with v1 tag to a new version with v2 tag.
1- Update your deployment using the v2 image.
oc set image deployment/myguestbook guestbook=ibmcom/guestbook:v2
2- Check the status of the rollout.
oc rollout status deployment/myguestbook
3- Get a list of pods (newly launched) as part of moving to the v2 image.
oc get pods -n guestbook-project
4- Copy the name of one of the new pods and use oc describe pod
to confirm that the pod is using the v2 image like the screenshot shown below.
oc describe pod <pod-name>
Notice that the service and router objects remain unchanged when you changed the underlying docker image version of your Pod.
5- Do a hard refresh on the public router URL to see that the Pod is now using the v2 image of the guestbook application.
When doing a rollout, you can see references to old replicas and new replicas. In this project, the old replicas are the original 5 pods you deployed in the second section when you scaled the application. The new replicas come from the newly created pods with the new image. All these pods are owned by the deployment. The deployment manages these two sets of pods with a resource called ReplicaSet.
1- To see the guestbook ReplicaSets use the following command.
oc get replicasets -l app=myguestbook
2- Undo your latest rollout using the following command.
oc rollout undo deployment/myguestbook
3- Get the status of undo rollout to see the newly created Pods as part of undoing the rollout.
oc rollout status deployment/myguestbook
4- Get the list of the newly created pods as part of undoing the rollout.
oc get pods
5- Copy the name of one of the pods and use it in the oc describe pod
command to view the image and its version used in the pod.
oc describe pod <pod-name>
6- After undoing the rollout, you can check the ReplicaSets and will notice that the old replica set is now active and manages the 5 pods using the following command.
oc get replicasets -l app=myguestbook
7- To view changes, make sure to hard refresh your browser and pointing and the URL to see if v1 version of the application running.
In this tutorial, you learned how to deploy an application on Red Hat OpenShift, scale an application using replicas, update the application with new image, and how to rollback your updates.