Introduction
This tutorial shows you how to manage a basic application lifecycle on Red Hat® OpenShift®. You learn to create and deploy an application from an existing Docker image, scale it according to workload demands, update it to a newer version, and finally rollback the application to a previous version.
Prerequisites
For this tutorial, you need:
- Red Hat OpenShift Cluster 4.3 or above on IBM Cloud
- IBM Cloud Shell
Estimated Time
It will take you around 30 minutes to complete this tutorial.
Steps
- Create an application from an existing Docker image
- Scale the application using replicas
- Update the application
- Rollback the application
Create an application from an existing Docker image
In IBM Cloud Shell, create a new project using the following command:
oc new-project guestbook-project
Create a new deployment resource using the ibmcom/guestbook:v1 Docker image in the project that you just created:
oc create deployment myguestbook --image=ibmcom/guestbook:v1
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
Expose the deployment on port 3000:
oc expose deployment myguestbook --type="NodePort" --port=3000
To view the service that you just exposed, use the following command:
oc get service
Note that you can access the service inside the pod using the
<Node IP>:<NodePort>
, but in the case of OpenShift on IBM Cloud, the NodeIP is not publicly accessible. You can use the built-in Kubernetes terminal available in IBM Cloud, which spawns a Kubernetes shell that is part of the OpenShift cluster network; you can accessNodeID:NodePort
from that shell.To make the exposed service publicly accessible, you need to create a public router. First, go to Networking > Routes from the Administrator Perspective on the web console, and then click Create Route.
Fill in the information as follows and click Create (you can leave all but the following fields empty):
- Name: myguestbook
- Service: myguestbook
Target Port: 3000→3000(TCP)
Once you create the route, you are redirected to the Route Overview, where you can access the external route from the URL under Location as shown in the following screenshot. You have created the Route successfully, and you have a publicly accessible endpoint URL that you can use to access your guestbook application.
If you click on the URL, you are redirected to a page that looks like the following:
Now that you have successfully deployed the application using the existing Docker image, you are ready to scale and rollback your application.
Scale the application using replicas
In this section, you scale the application by creating replicas of the pod you created in the first section. By having multiple replicas of a pod, you can ensure that your deployment has the available resources to handle an increasing load on your application.
In IBM Cloud Shell, increase the capacity from a single running instance of guestbook up to 5 instances:
oc scale --replicas=5 deployment/myguestbook
Check the status of the deployment:
oc rollout status deployment myguestbook
Verify that you have 5 pods running after running the
oc scale
command:oc get pods -n guestbook-project
Update the application
With OpenShift, you can perform a rolling upgrade of your application to a new container image. With a rolling upgrade, you can update the running image and undo a rollout if you discover a problem during or after the deployment. In this section, you upgrade the image with a v1 tag to a new version with a v2 tag.
In IBM Cloud Shell, update your deployment using the v2 image:
oc set image deployment/myguestbook guestbook=ibmcom/guestbook:v2
Check the status of the rollout:
oc rollout status deployment/myguestbook
Get a list of pods (newly launched) as part of moving to the v2 image:
oc get pods -n guestbook-project
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 following screenshot:oc describe pod <pod-name>
Notice that the service and router objects remain unchanged when you change the underlying Docker image version of your pod.
Perform a hard refresh on the public router URL to see that the Pod is now using the v2 image of the guestbook application. (In most browsers, pressing Ctrl+F5 performs a hard refresh.)
Rollback the application
When performing 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.
To see the guestbook ReplicaSets, use the following command:
oc get replicasets -l app=myguestbook
Undo your latest rollout using the following command:
oc rollout undo deployment/myguestbook
Get the status of Undo deployment to see the newly created pods as part of undoing the rollout:
oc rollout status deployment/myguestbook
Get the list of the newly created pods as part of undoing the rollout:
oc get pods
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>
After undoing the rollout, check the ReplicaSets, and you 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
To view changes, make sure to hard refresh your browser pointing at the router URL (as before) to see if v1 version of the application is running:
Summary
Now that you know how to deploy, scale, update, and rollback your application on OpenShift, you can use this knowledge to deploy applications from any existing Docker images, scale them to handle an increasing workload, update them as newer versions of Docker images are released, and rollback to older versions in case of any bugs/issues in the newer version.