Chapter 8. Deploying an Application on OpenShift
This section describes how to use OpenShift to create containerized applications and deploy them on an OpenShift cluster. The examples in this section use only basic features of the OpenShift service and are for demonstration purposes only. See the CDK Developer Guide (to be published) for more in-depth descriptions.
8.1. Deploying an InstantApp Template as a New Application Copy linkLink copied to clipboard!
In this example, an InstantApp template available from the default openshift project is used to create and deploy a web application. The definition of the template sets all necessary configuration options. A good way to experiment with OpenShift deployments is to clone the example’s repository and modify different parts of the defined template.
8.1.1. Creating a New OpenShift Application from a Template Copy linkLink copied to clipboard!
To use a template to directly create a new application, run the oc new-app command with the --template option specifying the template you want to use as the basis for your application. For example, to use the nodejs-example InstantApp template, run the following command:
8.1.2. Monitoring Deployment Progress Copy linkLink copied to clipboard!
To monitor the progress of the deployment, use the oc status command, optionally with the -v (--verbose) parameter to display information about potential warnings:
8.1.3. Displaying Information about a Deployed Service Copy linkLink copied to clipboard!
The application is automatically deployed when the build completes. Use the oc describe command to display information about different parts of the application (or use the oc get all command to display all information at once).
To see summary information about the service, use:
To display information about the route established for the application, use:
The above command shows that the deployed application can be accessed at nodejs-example-sample-project.rhel-cdk.10.1.2.2.xip.io (the URL is made available through the external xip.io DNS service).
You can point a web browser from your host system at this address to see the page generated by the application. To check whether the application is running properly from the command line, either use a text-mode browser like lynx or download the page using curl:
curl nodejs-example-sample-project.rhel-cdk.10.1.2.2.xip.io 2> /dev/null | grep '<h1>'
~]$ curl nodejs-example-sample-project.rhel-cdk.10.1.2.2.xip.io 2> /dev/null | grep '<h1>'
<h1>Welcome to your Node.js application on OpenShift</h1>
8.1.4. Learning about Service Images and Containers Copy linkLink copied to clipboard!
To check the Docker images and containers that form the application, use the docker images and docker ps commands:
8.2. Deploying a 'Hello World' Application Using Node.js Copy linkLink copied to clipboard!
In this example, a very simple Node.js web application is created. The application is defined using a minimalistic build strategy that is automatically downloaded from a Git repository (in this case, GitHub is used). The built application is deployed on top of a Node.js container pulled from the Red Hat Atomic Registry.
8.2.1. Preparing the Application Source Code Copy linkLink copied to clipboard!
Create a new repository on GitHub. For the purposes of this example, we will assume that the created repository has the following URL: http://github.com/example/nodejs-hello-world.git. Then upload a basic Node.js application to the root directory of the repository. Name the file helloworld.js.
The application creates an HTTP server listening on port 8080 and prints Hello World when accessed. Download the helloword.js file.
8.2.2. Preparing the Definition of an OpenShift Application Copy linkLink copied to clipboard!
Upload the following minimal definition of a Node.js OpenShift application to the GitHub repository. Name the file package.json. Based on the name of the file, OpenShift automatically selects the appropriate builder (nodejs, in this case).
Download the package.json file.
8.2.3. Creating a New OpenShift Application Copy linkLink copied to clipboard!
To create a new application in OpenShift, use the oc new-app command. OpenShift automatically performs the following steps:
- downloads required dependencies, including specified Docker-formatted container images,
- pulls latest source code and application definition from the specified location (a Git repository, in this case),
- builds the application source code,
- builds a container with the resulting application,
- deploys the application container.
In this example, the required Node.js container image from the Red Hat Atomic Registry is specified along with the URL of the Git repository:
8.2.4. Monitoring Build Progress Copy linkLink copied to clipboard!
Use the oc describe build command to display information about the build currently in progress:
To see the build logs, use the oc logs command and specify the build you are interested in:
8.2.5. Displaying Information about the Deployed Service Copy linkLink copied to clipboard!
Use the oc describe command and specify the service you are interested in to show summary information about the created service:
Similarly, you can specify the deployment to show information about it, including the containers that have been created and deployed for the application:
8.2.6. Testing the Running Application Copy linkLink copied to clipboard!
To verify that the deployed HTTP server works correctly and serves the message specified in the application source code, you can use, for example, the curl tool:
curl 172.17.0.4:8080
~]$ curl 172.17.0.4:8080
Hello World
The IP address is shown, for example, in the description of the deployed service or pod. Use the oc get pods to list all pods:
oc get pods
~]$ oc get pods
NAME READY STATUS RESTARTS AGE
nodejs-hello-world-1-1b5xi 1/1 Running 0 2m
nodejs-hello-world-1-build 0/1 Completed 0 2m
Use the name of the pod to display its description and grep for its IP address:
oc describe pod/nodejs-hello-world-1-1b5xi | grep IP
~]$ oc describe pod/nodejs-hello-world-1-1b5xi | grep IP
IP: 172.17.0.4
8.3. Additional Resources Copy linkLink copied to clipboard!
- See the definition of InstantApp OpenShift templates at https://github.com/openshift for inspiration.
- See the OpenShift Enterprise Developer Guide for detailed instructions on how to prepare, build, and deploy containerized applications on OpenShift.
- See the video tutorials and demonstration for developers at the OpenShift YouTube channel.