Get started
Quick start with odo v3
This guide will run through creating a simple hello world devfile project using odo
version 3. The purpose of this guide is to provide a practical introduction to those just starting out with devfiles.
Prerequisites
odo
v3- Access to a Kubernetes or OpenShift cluster
Procedure
Access or setup your target cluster
- (Optional) You can use
minikube
to run your cluster locally, follow these steps to get started
- (Optional) You can use
Install
odo
version 3 if you do not already have it by following the Installation guideOnce installed, use
odo login
to login with your cluster credentials- This step is not needed if using
minikube
- This step is not needed if using
Create a directory to store a simple hello world Express.js application
package.json file
package.json{ "name": "helloworld-example", "version": "1.0.0", "description": "", "main": "app.js", "type": "module", "dependencies": { "@types/express": "^4.17.17", "express": "^4.18.2" }, "scripts": { "start": "node app.js" } }
Application source code
app.jsimport express from "express" const PORT = 3000 const app = express() app.get("/", (req, res) => { res.send("Hello world!") }) app.listen(PORT, () => { console.log(`Listening on port ${PORT}..`) })
(Optional) Normally it is recommended to use
odo init
to start your project from a devfile registry stack, see Developing with Node.JS and Command Reference: odo init for details, steps 6-11 will go through the process of making the devfile from scratch- For this project, run
odo init --name helloworld-example --devfile nodejs
, to test this devfile in action, skip to step 12
- For this project, run
Create a devfile with the filename
.devfile.yaml
. Add theschemaVersion
field with the desired devfile specification version to use.devfile.yamlschemaVersion: 2.2.2
Next, create the first component to serve as the runtime for the project, for this use the
container
component with the nameruntime
and thenode:18-alpine
imagename
is the identifier used to refer to the componentimage
is the container image to use for the component.devfile.yamlschemaVersion: 2.2.2 +components: + - name: runtime + container: + image: node:18-alpine
The
runtime
container hosts the expressjs app created which listens on port3000
, define this port in the component by specifying an entry underendpoints
Each endpoint has at least a
name
to identify them and thetargetPort
to specify the port number to forward.devfile.yamlschemaVersion: 2.2.2 components: - name: runtime container: image: node:18-alpine + endpoints: + - name: http-3000 + targetPort: 3000
Now that the
runtime
container is defined,commands
are needed to tellodo
what to do during the step of the development runtime (odo dev
). Define the command to install the dependencies needed to run the application (npm install
)- The
id
field identifies the command by a label which can be used to specify which command to run by the dev tool- Example:
odo dev --build-command install
- Example:
- An
exec
command specifies explicit shell command(s) to run on a givencomponent
commandLine
defines the shell command(s) to execute as part of that devfile command- The
group
specifies whatkind
of command it is or if it is the default of its kind,isDefault
build
commands runs beforerun
commands.devfile.yamlschemaVersion: 2.2.2 components: - name: runtime container: image: node:18-alpine endpoints: - name: http-3000 targetPort: 3000 +commands: + - id: install + exec: + commandLine: npm install + component: runtime + workingDir: ${PROJECT_SOURCE} + group: + isDefault: true + kind: build
- The
Next, define the command to run the application (
node app.js
).devfile.yamlschemaVersion: 2.2.2 components: - name: runtime container: image: node:18-alpine endpoints: - name: http-3000 targetPort: 3000 commands: - id: install exec: commandLine: npm install component: runtime workingDir: ${PROJECT_SOURCE} group: isDefault: true kind: build + - id: run + exec: + commandLine: node app.js + component: runtime + workingDir: ${PROJECT_SOURCE} + group: + isDefault: true + kind: run
Now the devfile is ready to be used to run the application
Devfile for Hello World application
.devfile.yamlschemaVersion: 2.2.2 components: - name: runtime container: image: node:18-alpine endpoints: - name: http-3000 targetPort: 3000 commands: - id: install exec: commandLine: npm install component: runtime workingDir: ${PROJECT_SOURCE} group: isDefault: true kind: build - id: run exec: commandLine: node app.js component: runtime workingDir: ${PROJECT_SOURCE} group: isDefault: true kind: run
Run
odo dev
and you should see the following outputodo dev output
__ / \__ Developing using the "helloworld-devfile" Devfile \__/ \ Namespace: default / \__/ odo version: v3.9.0 \__/ ⚠ You are using "default" namespace, odo may not work as expected in the default namespace. ⚠ You may set a new namespace by running `odo create namespace <name>`, or set an existing one by running `odo set namespace <name>` ↪ Running on the cluster in Dev mode • Waiting for Kubernetes resources ... ⚠ Pod is Pending ✓ Pod is Running ✓ Syncing files into the container [401ms] ✓ Building your application in container (command: install) [1s] • Executing the application (command: run) ... - Forwarding from 127.0.0.1:20001 -> 3000 ↪ Dev mode Status: Watching for changes in the current directory /path/to/project Keyboard Commands: [Ctrl+c] - Exit and delete resources from the cluster [p] - Manually apply local changes to the application on the cluster
The application port
3000
served in the cluster gets routed to your host on a different port (in this case20001
). Runcurl http://localhost:20001
and you should see the following outputResponse content returned by curl
Hello world!%
Congratulations! You have written your first devfile project with
odo
!