Creating a single-component application with odo

With odo, you can create and deploy applications on clusters.

Prerequisites

Creating a project

Create a project to keep your source code, tests, and libraries organized in a separate single unit.

Procedure

  1. Log in to an OKD cluster:

    1. $ odo login -u developer -p developer
  2. Create a project:

    1. $ odo project create myproject

    Example output

    1. Project 'myproject' is ready for use
    2. New project created and now using project : myproject

Creating a Node.js application with odo

To create a Node.js component, download the Node.js application and push the source code to your cluster with odo.

Procedure

  1. Create a directory for your components:

    1. $ mkdir my_components && cd my_components
  2. Download the example Node.js application:

    1. $ git clone https://github.com/openshift/nodejs-ex
  3. Change the current directory to the directory with your application:

    1. $ cd <directory_name>
  4. Add a component of the type Node.js to your application:

    1. $ odo create nodejs
    By default, the latest image is used. You can also explicitly specify an image version by using odo create openshift/nodejs:8.
  5. Push the initial source code to the component:

    1. $ odo push

    Your component is now deployed to OKD.

  6. Create a URL and add an entry in the local configuration file as follows:

    1. $ odo url create --port 8080
  7. Push the changes. This creates a URL on the cluster.

    1. $ odo push
  8. List the URLs to check the desired URL for the component.

    1. $ odo url list
  9. View your deployed application using the generated URL.

    1. $ curl <url>

Modifying your application code

You can modify your application code and have the changes applied to your application on OKD.

  1. Edit one of the layout files within the Node.js directory with your preferred text editor.

  2. Update your component:

    1. $ odo push
  3. Refresh your application in the browser to see the changes.

Adding storage to the application components

Use the odo storage command to add persistent data to your application. Examples of data that must persist include database files, dependencies, and build artifacts, such as a .m2 Maven directory.

Procedure

  1. Add the storage to your component:

    1. $ odo storage create <storage_name> --path=<path_to_the_directory> --size=<size>
  2. Push the storage to the cluster:

    1. $ odo push
  3. Verify that the storage is now attached to your component by listing all storage in the component:

    1. $ odo storage list

    Example output

    1. The component 'nodejs' has the following storage attached:
    2. NAME SIZE PATH STATE
    3. mystorage 1Gi /data Pushed
  4. Delete the storage from your component:

    1. $ odo storage delete <storage_name>
  5. List all storage to verify that the storage state is Locally Deleted:

    1. $ odo storage list

    Example output

    1. The component 'nodejs' has the following storage attached:
    2. NAME SIZE PATH STATE
    3. mystorage 1Gi /data Locally Deleted
  6. Push the changes to the cluster:

    1. $ odo push

Adding a custom builder to specify a build image

With OKD, you can add a custom image to bridge the gap between the creation of custom images.

The following example demonstrates the successful import and use of the redhat-openjdk-18 image:

Prerequisites

  • The OpenShift CLI (oc) is installed.

Procedure

  1. Import the image into OKD:

    1. $ oc import-image openjdk18 \
    2. --from=registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift \
    3. --confirm
  2. Tag the image to make it accessible to odo:

    1. $ oc annotate istag/openjdk18:latest tags=builder
  3. Deploy the image with odo:

    1. $ odo create openjdk18 --git \
    2. https://github.com/openshift-evangelists/Wild-West-Backend

Connecting your application to multiple services using OpenShift Service Catalog

The OpenShift service catalog is an implementation of the Open Service Broker API (OSB API) for Kubernetes. You can use it to connect applications deployed in OKD to a variety of services.

Prerequisites

  • You have a running OKD cluster.

  • The service catalog is installed and enabled on your cluster.

Procedure

  • To list the services:

    1. $ odo catalog list services
  • To use service catalog-related operations:

    1. $ odo service <verb> <service_name>

Deleting an application

Use the odo app delete command to delete your application.

Procedure

  1. List the applications in the current project:

    1. $ odo app list

    Example output

    1. The project '<project_name>' has the following applications:
    2. NAME
    3. app
  2. List the components associated with the applications. These components will be deleted with the application:

    1. $ odo component list

    Example output

    1. APP NAME TYPE SOURCE STATE
    2. app nodejs-nodejs-ex-elyf nodejs file://./ Pushed
  3. Delete the application:

    1. $ odo app delete <application_name>

    Example output

    1. ? Are you sure you want to delete the application: <application_name> from project: <project_name>
  4. Confirm the deletion with Y. You can suppress the confirmation prompt using the -f flag.