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) toolkit 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 noDockerfile
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.
Clone the GitHub repo by running the following command:
$ git clone https://github.com/IBM/deploy-python-openshift-tutorial.git
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 .
Open the OpenShift web console:
Log in to OpenShift using the CLI and create a new project:
$ oc login https://c100-e.us-east.containers.cloud.ibm.com:30682 --token=xxxxxxxxxxxxxxx $ oc new-project exampleproject
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
Run the following command to create a route for the Docker registry:
$ oc create route reencrypt --service=docker-registry
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
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.
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
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
Push the Docker image to the OpenShift Docker registry:
docker push docker-registry-default.<cluster_name>-<ID_string>.<region>.containers.appdomain.cloud/exampleproject/helloworldpython
Deploy the image to OpenShift and expose the route:
$ oc project exampleproject $ oc new-app --image-stream=helloworldpython --name=helloworldpython $ oc expose svc/helloworldpython
Click the Open Url icon on the OpenShift console for the
helloworldpython
application:Hello, world!
is displayed in the browser window:
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.
Hello, world!
is displayed in the browser window:
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:
Hello, world!
is displayed in the browser window:
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.
Watch this video to learn more about Source-to-Image.
Next steps
Perhaps you’re ready to use Tekton pipelines to build and deploy Docker images.