Three ways to deploy a Python app into an OpenShift cluster

This tutorial shows you how to deploy a Python application into an OpenShift cluster on the cloud. You can also use the methodology described here to deploy applications or microservices developed on other runtime environments. The examples in this tutorial use Red Hat® OpenShift® on IBM Cloud™

This tutorial covers three deployment scenarios to build and deploy applications to an OpenShift cluster on IBM Cloud:

  • An existing docker image needs to be pushed to the OpenShift cluster on IBM Cloud, and then deployed: In this scenario, an existing Docker image is in a private registry. The image must be deployed on the OpenShift cluster on IBM Cloud. For example, you might use this deployment scenario in a “lift and shift” approach during application modernization. There is no continuous integration or delivery mechanism that is implemented, because you don’t have access to the sources.

  • There is a GitHub repo with sources as well as a Dockerfile that has instructions to assemble the image: This scenario is applicable when you want complete control and flexibility to assemble the image with the only the required dependencies and versions. The code is always compatible with the dependencies, because you specify them. You need to maintain the Dockerfile in this scenario, which can be a complex task at times. This scenario allows continuous integration and delivery to the OpenShift cluster, which helps keep the deployed version of the code current.

  • There is a GitHub repo with sources: In this scenario, you rely on the OpenShift Source-to-Image (S2I) tool to create a Docker image. OpenShift S2I uses the sources and a builder image to create a new Docker image. When the oc new-app ... command is used and no Dockerfile exists in the repository, the source code language is auto-detected. The language detection rules are specified here. According to the OpenShift blog, the advantages of using S2I are speed, patchability, user efficiency, and ecosystem. This scenario also allows a continuous integration and delivery to the OpenShift cluster, which helps keep the deployed version of the code current.

Prerequisites

You need an IBM Cloud Account, the OpenShift CLI and Docker.

Estimated time

Completing this tutorial should take about 30 minutes.

Step 1. Create an OpenShift cluster instance

Create a new instance of an OpenShift cluster. Select an appropriate plan and click Create.

Step 2. Deploy the application using a Docker image in a local registry

A GitHub repo with examples at github.com/IBM/deploy-python-openshift-tutorial contains sources and a Dockerfile. This tutorial uses these examples to assemble a Docker image and later deploy it to the OpenShift cluster.

  1. Clone the GitHub repo by running the following command:

     $ git clone https://github.com/IBM/deploy-python-openshift-tutorial.git
    
  2. Build a Docker image of the application that needs to be deployed to an OpenShift cluster:

     $ cd deploy-python-openshift-tutorial
     $ docker build -t helloworldpython:latest .
    
  3. Open the OpenShift web console:

    Open console screen capture

  4. Log in to OpenShift using the CLI and create a new project:

    Copy login command

     $ oc login https://c100-e.us-east.containers.cloud.ibm.com:30682 --token=xxxxxxxxxxxxxxx
     $ oc new-project exampleproject
    
  5. Create a route for the Docker registry:

     $ oc project default
     $ oc get svc
    

    The output looks like the following example:

     NAME               TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)                      AGE
     docker-registry    ClusterIP      172.21.xxx.xx    <none>           5000/TCP                     18h
     kubernetes         ClusterIP      172.21.x.x       <none>           443/TCP,53/UDP,53/TCP        18h
     myfirstosdeploy    ClusterIP      172.21.xx.xxx    <none>           5000/TCP                     17h
     registry-console   ClusterIP      172.21.xxx.xxx   <none>           9000/TCP                     18h
     router             LoadBalancer   172.21.xx.x      169.47.xxx.xxx   80:31297/TCP,443:30385/TCP   18h
    
  6. Run the following command to create a route for the Docker registry:

     $ oc create route reencrypt --service=docker-registry
    
  7. Check the create route details:

     $ oc get route docker-registry
    

    You should get an output that looks like the following example:

     NAME              HOST/PORT                                                                                                        PATH      SERVICES          PORT       TERMINATION   WILDCARD
     docker-registry   docker-registry-default.clustersiteam-5290cxxxxxxxxxxd1b85xxx-0001.us-east.containers.appdomain.cloud             docker-registry   5000-tcp   reencrypt     None
    
  8. Observe the pattern of the registry url – docker-registry-default.<cluster_name>-<ID_string>.<region>.containers.appdomain.cloud.

    Make a note of the Docker registry URL. It is required in subsequent steps.

  9. Log in to the Docker registry using the Docker CLI.

    Note: Use the Docker registry URL noted earlier.

     docker login -u $(oc whoami) -p $(oc whoami -t) docker-registry-default.<cluster_name>-<ID_string>.<region>.containers.appdomain.cloud
    
  10. Tag the Docker image as shown in the following example:

     docker tag helloworldpython:latest docker-registry-default.<cluster_name>-<ID_string>.<region>.containers.appdomain.cloud/exampleproject/helloworldpython:latest
    
  11. Push the Docker image to the OpenShift Docker registry:

     docker push docker-registry-default.<cluster_name>-<ID_string>.<region>.containers.appdomain.cloud/exampleproject/helloworldpython
    
  12. Deploy the image to OpenShift and expose the route:

     $ oc project exampleproject
     $ oc new-app --image-stream=helloworldpython --name=helloworldpython
     $ oc expose svc/helloworldpython
    
  13. Click the Open Url icon on the OpenShift console for the helloworldpython application:

    Open url for helloworldpython screen capture

    Hello, world! is displayed in the browser window:

    HelloWorld from helloworldpython screen capture

Step 3. Deploy the application using a GitHub repo with sources and a Dockerfile

This step uses the same GitHub repo github.com/IBM/deploy-python-openshift-tutorial for the deployment. The Dockerfile contains instructions to assemble the image.

The examples in this step use the exampleproject you created earlier.

$ oc project exampleproject

Run the following command to deploy the application:

$ oc new-app https://github.com/IBM/deploy-python-openshift-tutorial
$ oc expose svc/deploy-python-openshift-tutorial

Click the Open Url icon on the OpenShift console for the deploy-python-openshift-tutorial application.

Open url for deploy-python-openshift-tutorial

Hello, world! is displayed in the browser window:

HelloWorld from deploy-python-openshift-tutorial

Step 4. Deploy the application using a GitHub repo with sources

This step uses the same GitHub repo github.com/IBM/deploy-python-openshift-tutorial for the deployment. Here, the OpenShift S2I uses a Builder image and its sources to create a new Docker image that is deployed to the OpenShift cluster:

$ oc new-app https://github.com/IBM/deploy-python-openshift-s2i-tutorial
$ oc expose svc/deploy-python-openshift-s2i-tutorial

Click the Open Url icon on the OpenShift console for the deploy-python-openshift-s2i-tutorial application:

Open url for deploy-python-openshift-s2i-tutorial

Hello, world! is displayed in the browser window:

HelloWorld from deploy-python-openshift-s2i-tutorial

Summary

Now you know three different ways to build and deploy applications to an OpenShift cluster. You can try your own deployments with Red Hat OpenShift on IBM Cloud.

Balaji Kadambi