Creating components using odo
Component is the most basic unit of operation for odo. And the way to create one is using odo create
(short for odo component create
) command.
In simplest terms, when you "create" an odo component, you populate your current working directory with the file devfile.yaml
. A Devfile is a manifest file that contains information about various resources (URL, Storage, Services, etc.) that correspond to your component, and will be created on the Kubernetes cluster when you execute odo push
command. Most odo commands will first modify (add or remove configuration from) this file, and then subsequent odo push
will create or delete the resources from the Kubernetes cluster.
However, odo users are not expected to know how the devfile.yaml
is organized; it is the odo commands that would create, update, or delete it.
One final thing to keep in mind - there can be only one odo component in a directory. Nesting odo components is not expected to work well. In other terms, if you have multiple parts (components), say frontend and backend, of your microservices application that you want to create odo components for, you should put them in separate directories and not try to nest them. Take a look at example structure below:
In this guide, we are going to create a Spring Boot component and a Nodejs component to deploy parts of the odo quickstart project to a Kubernetes cluster.
Let's clone the project first:
Next, create a project on the Kubernetes cluster in which we will be creating our component. This is to keep our Kubernetes cluster clean of the tasks we perform (this step is optional):
Alternatively, you could also use one of the existing projects on the cluster:
Now, set the project in which you want to create the component:
odo supports interactive and non-interactive ways of creating a component. We will create the Spring Boot component interactively and Nodejs component non-interactively. The Spring Boot component is in backend
directory. It contains code for the REST API that our Nodejs component will be interacting with. This Nodejs component is in frontend
directory.
#
Creating a component interactivelyTo interactively create the Spring Boot component, cd
into the cloned project (already done if you copy-pasted the command above), then cd
into backend
directory, and execute:
You will be prompted with a few questions one after the another. Go through each one of them to create a component.
First question is about selecting the component type:
By default,
java-maven
is selected for us. Since this is a Spring Boot application, we should be selectingjava-springboot
.We can either scroll down to
java-springboot
using the arrow key, or start typingspring
on the prompt. Typingspring
will lead to odo filtering the component type based on your input.Next, odo asks you to name the component:
Name it
backend
.Next, odo asks you for the project in which you would like to create the component. Use the project
myproject
that we created earlier or the one you had set usingodo project set
commandNow you will have a
devfile.yaml
in your current working directory. But odo is not done asking you questions yet.Lastly, odo asks you if you would like to download a "starter project". Since we already cloned the odo-quickstart project, we answer in No by typing
n
and hitting the return key. We discuss starter projects later in this document:
Your Spring Boot component is now ready for use.
#
Creating a component non-interactivelyTo non-interactively create the Nodejs component to deploy our frontend code, cd
into the cloned frontend
directory and execute:
Here nodejs
is the type of the component, frontend
is the name of the component, and -n myproject
tells odo to use the project myproject
for the mentioned odo create
operation.
#
Starter projectsBesides creating a component for an existing code, you could also use "starter project" when creating a component.
Starter projects are example projects developed by the community to showcase the usability of devfiles. An odo user can use these starter projects by running odo create
command in an empty directory.
#
Starer projects in interactive modeTo interactively create a Java Spring Boot component using the starter project, you can follow the below steps:
In the questions that odo asks you next, provide answers like below:
#
Starter projects in non-interactive modeTo non-interactively create a Java Spring Boot component using the starter project, you can follow the below steps:
#
Push the component to Kubernetesodo follows a "create & push" workflow for almost all the commands. Meaning, most odo commands won't create resources on Kubernetes cluster unless you run odo push
command.
Among the various ways described above, irrespective of how you created the component, the next step to create the resources for our component on the cluster would be to run odo push
.
Execute below command from the component directory of both the frontend
and backend
components: