Think 2021: New tools have the developer ecosystem and IBM building together Learn more

Create, scale, upgrade, and rollback an application on Red Hat OpenShift

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:

Estimated Time

It will take you around 30 minutes to complete this tutorial.

Steps

  1. Create an application from an existing Docker image
  2. Scale the application using replicas
  3. Update the application
  4. Rollback the application

Create an application from an existing Docker image

  1. In IBM Cloud Shell, create a new project using the following command:

     oc new-project guestbook-project
    
  2. 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
    
  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
    

    get pods

  4. Expose the deployment on port 3000:

     oc expose deployment myguestbook --type="NodePort" --port=3000
    
  5. To view the service that you just exposed, use the following command:

     oc get service
    

    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 access NodeID:NodePort from that shell.

  6. 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.

  7. 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)

      create route

      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.

      Access the external route

      If you click on the URL, you are redirected to a page that looks like the following:

      guestbook v1 app

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.

  1. In IBM Cloud Shell, 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
    

    rollout

  3. Verify that you have 5 pods running after running the oc scale command:

     oc get pods -n guestbook-project
    

    get pods rollout

    pods rollout

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.

  1. In IBM Cloud Shell, 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
    

    rollout v2

  3. Get a list of pods (newly launched) as part of moving to the v2 image:

    oc get pods -n guestbook-project
    

    get pods v2

  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 following screenshot:

    oc describe pod <pod-name>
    

    describe pod

    Notice that the service and router objects remain unchanged when you change the underlying Docker image version of your pod.

  5. 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.)

    guestbook app

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.

  1. To see the guestbook ReplicaSets, use the following command:

     oc  get replicasets -l app=myguestbook
    

    get replicas

  2. Undo your latest rollout using the following command:

     oc rollout undo deployment/myguestbook
    

    undo deployment

  3. Get the status of Undo deployment to see the newly created pods as part of undoing the rollout:

     oc rollout status deployment/myguestbook
    

    rollout success

  4. Get the list of the newly created pods as part of undoing the rollout:

     oc get pods
    

    rollout 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>
    

    get pods v1

  6. 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
    

    get replicas 2

  7. 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:

    guestbook v1 app

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.