Git Product home page Git Product logo

odo-demo's Introduction

Using vscode and odo to develop python applications on Openshift

Setup

Install the Client Tools

Install the version 3 odo client into a directory in your $PATH.

Install the latest oc client into a directory in your $PATH.

Create and change to a new directory. Launch vscode and open the folder that you created.

mkdir demo
cd demo
code .

Login to Openshift

Open 2 terminals in vscode, launch a web browser and login to the Openshift web console.

In the upper right menu under your name in the Openshift web console, choose Copy Login Command -> Display Token then copy the server and token arguments from the example oc command string.

Perform the following steps using the odo client from a vscode terminal.

Login to the Openshift API server.

odo login --token=sha256~logintoken --server=https://api.ocp.mysandbox.com:6443

Creating the Application

Create a new project.

odo create project odo-demo

Example output.

 ✓  Project "odo-demo" is ready for use
 ✓  New project created and now using project: odo-demo

Create a new python flask application named flask-example.

odo init --name flask-example --starter=flask-example --devfile=python

Start developer mode to allow for local interactive development.

odo dev

Example output.

  __
 /  \__     Developing using the flask-example Devfile
 \__/  \    Namespace: odo-demo
 /  \__/    odo version: v3.0.0-rc2
 \__/

↪ Deploying to the cluster in developer mode
 •  Waiting for Kubernetes resources  ...
 ⚠  Pod is Pending
 ✓  Pod is Running
 ✓  Syncing files into the container [891ms]
 ✓  Building your application in container on cluster (command: pip-install-requirements) [3s]
 •  Executing the application (command: run-app)  ...
 -  Forwarding from 127.0.0.1:40001 -> 8080

↪ Dev mode
 Status:
 Watching for changes in the current directory /Users/bkozdemb/demo

 Keyboard Commands:
[Ctrl+c] - Exit and delete resources from the cluster
     [p] - Manually apply local changes to the application on the cluster
Pushing files...

File /Users/bkozdemb/demo/.odo changed
 •  Waiting for Kubernetes resources  ...
 ✓  Syncing files into the container 

↪ Dev mode
 Status:
 Watching for changes in the current directory /Users/bkozdemb/demo

 Keyboard Commands:
[Ctrl+c] - Exit and delete resources from the cluster
     [p] - Manually apply local changes to the application on the cluster

The odo client should set up a port-forwarding rule that will allow access to your application endpoint at http://127.0.0.1:40001. Even though the python application is running in Openshift, it can be accessed via a local IP and port.

curl http://127.0.0.1:40001

Hello World!

In VSCode, edit the app.py file and make a simple code change.

Save the file and odo developer mode will automatically push any changes to the running pod in Openshift. Another curl command may be used to verify the change.

curl http://127.0.0.1:40001

Hello Python World!

Adding a database

Create a postgresql database using a built-in Openshift template. This may be done via the oc CLI or Openshift console.

CLI example

oc new-app --template=postgresql-ephemeral -p POSTGRESQL_USER=postgres -p POSTGRESQL_PASSWORD=postgres -p POSTGRESQL_DATABASE=postgres --dry-run=False

Glueing the Pieces Together

Replace the contents of app.py that was created by the odo template with the version in this repo. It contains basic code to connect to the postgresql database and return the connection IP address.

Initially, the app will error because the psycopg library can't be found. Running curl and examining the logs will reveal this.

curl 127.0.0.1:40001
curl: (52) Empty reply from server
oc logs flask-example-app-<pod-id>
...
File "/projects/app.py", line 3, in <module>
   import psycopg
ModuleNotFoundError: No module named 'psycopg'

Fix this by replacing adding psycopg[binary] to the requirements.txt file that was created by odo init.

We're getting closer. The logs should reveal that the app runs but fails to connect to the database server.

oc logs flask-example-app-<pod-id>
...
INFO:root:POSTGRESQL_DATABASE = None
INFO:root:POSTGRESQL_USER = None
INFO:root:POSTGRESQL_PASSWORD = None
INFO:root:DB Connection Failed!
INFO:root:Exiting.

Notice the environment variables needed for authentication are null. We'll fix this by adding the environment variables to devfile.yaml.

Fixed that by the replacing the contents of devfile.yaml that was created by the odo template with the version in this repo.

After modifying devfile.yaml you should notice that a new pod will get built and deployed. Now the curl should succeed.

curl 127.0.0.1:40001
Connected to postgresql at 172.30.224.110

Congratulations on orchestrating your first Openshift application!

This concludes the demo.

odo-demo's People

Contributors

bkoz avatar

Stargazers

Christopher Chase avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.