Tuesday, March 23, 2021

Build cloud Native Application With Quarkus ---- Session 1

 Basic Quarkus Hands-on Lab (Version 2021-02-10)

2Getting Started with Quarkus

In this step, you will create a straightforward application serving a hello endpoint. To demonstrate dependency injection this endpoint uses a greeting bean.

arch

This IDE is based on Eclipse Che (which is in turn based on MicroSoft VS Code editor).

You can see icons on the left for navigating between project explorer, search, version control (e.g. Git), debugging, and other plugins. You’ll use these during the course of this workshop. Feel free to click on them and see what they do:

cdw

If things get weird or your browser appears, you can simply reload the browser tab to refresh the view.

Many features of CodeReady Workspaces are accessed via Commands. You can see a few of the commands listed with links on the home page (e.g. New File..Git Clone.., and others).

If you ever need to run commands that you don’t see in a menu, you can press F1 to open the command window, or the more traditional Control+SHIFT+P (or Command+SHIFT+P on Mac OS X).

Import Project

Let’s import our project. Click on Git Clone.. (or type F1, enter 'git' and click on the auto-completed Git Clone.. )

cdw

Step through the prompts, using the following value for Repository URL. If you use FireFox, it may end up pasting extra spaces at the end, so just press backspace after pasting:

https://github.com/RedHat-Middleware-Workshops/quarkus-workshop-m1m2-labs
crw

Click on Select Repository Location then click on Open in New Window. It will reload your web browser immediately:

crw

The project is imported into your workspace and is visible in the project explorer (click on the top-most icon for project explorer):

crw

The Terminal window in CodeReady Workspaces. You can open a terminal window for any of the containers running in your Developer workspace. For the rest of these labs, anytime you need to run a command in a terminal, you can use the >_ New Terminal command on the right:

codeready-workspace-terminal

IMPORTANT: Check out proper Git branch

To make sure you’re using the right version of the project files, run this command in a CodeReady Terminal:

cd $CHE_PROJECTS_ROOT/quarkus-workshop-m1m2-labs && git checkout ocp-4.6

The project has

  • The Maven structure

  • An org.acme.people.rest.GreetingResource resource exposed on /hello, along with a simple test

  • A landing page that is accessible on http://localhost:8080 after starting the application

  • The application configuration file

  • Other source files we’ll use later

Navigate to src → main → java → org.acme.people.rest in the project tree and double click on GreetingResource.java.

codeready-workspace-terminal

This class has a very simple RESTful endpoint definition:

@Path("/hello")
public class GreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "hello";
    }
}

It’s a very simple REST endpoint, returning "hello" to requests on /hello.

Compared to vanilla JAX-RS, with Quarkus there is no need to create an Application class. It’s supported but not required. In addition, only one instance of the resource is created and not one per request. You can configure this using the different Scoped annotations (ApplicationScopedRequestScoped, etc).

Running the Application in Live Coding Mode

Live Coding (also referred to as dev mode) allows us to run the app and make changes on the fly. Quarkus will automatically re-compile and reload the app when changes are made. This is a powerful and efficient style of developing that you will use throughout the lab.

You can always use the mvn (Maven) commands to run Quarkus apps, but we’ve created a few helpful shortcuts on the right to run various Maven commands.

Start the app by clicking on Live Coding:

livecoding

This will compile and run the app using mvn compile quarkus:dev in a Terminal window. Leave this terminal window open throughout the lab! You will complete the entire lab without shutting down Quarkus Live Coding mode, so be careful not to close the tab (if you do, you re-run it). This is very useful for quick expermentation.

The first time you build the app, new dependencies may be downloaded via maven. This should only happen once, after that things will go even faster

You may see WARNINGs like Unrecognized configuration key or Duplicate entry. These are configuration values that will take effect later on and can be safely ignored for now.

You should see:

2020-11-03 14:27:00,102 INFO  [io.quarkus] (Quarkus Main Thread) people 1.0-SNAPSHOT on JVM (powered by Quarkus x.x.x) started in 0.972s. Listening on: http://0.0.0.0:8080
2020-11-03 14:27:00,102 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2020-11-03 14:27:00,103 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy]

Note the amazingly fast startup time! The app is now running "locally" (within the Che container in which the workspace is also running). localhost refers to the Kubernetes pod, not "your" laptop (so therefore opening localhost:8080 in your browser will not do anything).

CodeReady will also detect that the Quarkus app opens port 5005 (for debugging) and 8080 (for web requests). Do not open port 5005, but when prompted, open the port 8080, which opens a small web browser in CodeReady:

port

You should see the default Quarkus welcome page (you may need to click the reload icon):

port

Open a new CodeReady Workspaces Terminal:

livecoding

and invoke the hello endpoint using the following curl command:

curl http://localhost:8080/hello

You can also click on the URL link at the upper right to open the same default page in a separate browser tab:

page

Add /hello in your browser to see the same result as the curl command:

page

Now, let’s exercise the live reload capabilities of Quarkus. In CodeReady, open the GreetingResource.java file (in src/main/java/org/acme/people/rest) and change return "hello"; to return "hola"; in the editor. After making this change, reload the same brower tab that was showing hello. It should now show hola.

Wow, how cool is that? Supersonic Subatomic live reload! Go ahead and change it a few more times and access the endpoint again. And we’re just getting started. Leave the app running so we can continue to change it on the fly in the next section.

quarkus:dev runs Quarkus in development mode. This enables live reload with background compilation, which means that when you modify your Java files your resource files and refresh your browser these changes will automatically take effect.

This will also listen for a debugger on port 5005. If you want to wait for the debugger to attach before running you can pass -Ddebug on the command line. If you don’t want the debugger at all you can use -Ddebug=false. We’ll use this later.

Package the app

Quarkus apps can be packaged as an executable JAR file or a native binary. We’ll cover native binaries later, so for now, let’s package as an executable JAR.

Click on 'Package app for OpenShift':

livecoding

This produces an executable jar file in the target/ directory:

jar
  • people-1.0-SNAPSHOT-runner.jar - being an executable jar. Be aware that it’s not an über-jar as the dependencies are copied into the target/lib directory.

Run the executable JAR

Run the packaged application. In a Terminal, run the following command:

java -Dquarkus.http.port=8081 -jar $CHE_PROJECTS_ROOT/quarkus-workshop-m1m2-labs/target/*-runner.jar

We use -Dquarkus.http.port=8081 to avoid conflicting with port 8080 used for Live Coding mode

With the app running, open a separate terminal window, and ensure the app is running by executing a curl command:

curl http://localhost:8081/hello

You should see:

hola

Cleanup

Go back to the terminal in which you ran the app with java -jar and stop the app by pressing CTRL+CBe sure not to close the "Live Coding" terminal!

Congratulations!

You’ve seen how to build a basic app, package it as an executable JAR and start it up very quickly. The JAR file can be used like any other executable JAR file (e.g. running it as-is, packaging as a Linux container, etc.)

In the next step we’ll inject a custom bean to showcase Quarkus' CDI capabilities.

Saturday, January 2, 2021

2020-1

    This year has been unusual to say at the least. The calendar and its components have never been this irrelevant. The subway ride, smiling to random strangers and small talks in between seems bygone now. The rush in those  walks for a few blocks down the street after getting off the metro as if everyone has another train to catch and the rush as expected increases during the way back home since everyone literally has a train to catch.

That one dude at the corner seat with headphones on but with so much audio bleed, anyone ten feet around can clearly hear that bass thud or for better or for worse, if not audio bleeding headphone, someone would literally have loudspeaker playing Drake's God's Plan. Despite that audio bleed or loudspeaker, there would be a lady indulged in a book with undivided attention on the same cart.


    The reminiscence of often ignored trivial metro events when things were back “normal” is just one of many fragmented parts from the year 2020. We often use ‘gratefulness’ as part of our conversation to the extent that it feels it’s being way too overused.  Paradox is we never embraced and lived up to its meaning till the date. Nevertheless this year was able to change that as well. When people saw hundreds of acquaintances dying in their county/city, they started to become grateful for their lives. When there were thousands of mass layoffs and unemployment surged, those who still had it became grateful for their jobs. Our health, of which we often undermine the importance got its place back on the priority list. It was not just health that gots its place back; Human emotions like love, kindness and compassion also paved their way back. We saw and heard stories about bravery and selfless services of health care professionals all over the world. Several humanitarian organizations campaigned to deliver food and shelter for daily wage workers and anyone who needed it. The governments across the world, at least those who were affluent enough, promised and delivered stimulus checks and unemployment benefits to support people in this distressed time. 


    On a personal note, this year felt like a bad day-dream for me. When 2020 first started, I was really looking forward to the year. It had been a few months since I had  started my first job right after college. I had started to adapt to the company, my co-workers and its culture. That transition from a college dude who used to submit his assignments right 5 minutes before midnight deadline, go to bed at 3 and again get late for 9 AM class next day or even escape it if it was of XYZ professor who doesn’t include attendance as part of the grade to  a 9 to 5 employee ( thankfully my manager was flexible enough to let me slide my schedule to 10-6) was going slow but  smooth and steady. I was almost there; had started going to bed at midnight latest and waking up at 7.  My resolution for the year  was to become a much healthier and sorted person, read more books and travel to new places ( I guess travelling is everyone’s and every year’s resolution) . I tried making my bed every morning, organizing my closet, cooking every other day, cleaning my apartment and doing laundry once a week and going to the gym on weekends.  The journey was not as smooth as it sounds now and I fought a good battle with my laziness and inconsistency but I was pulling my shits together gradually until lockdown happened. Although the virus started to become a talk at the company meetings around December, it was in March they decided to make everything virtual aka “work from home”.  Before this pandemic and when I first started around September, I still remember I used to often wonder how it will feel to work from home and when I would be allowed to do so, seeing senior research engineers and developers work remotely twice a week. Fast forward now, I don’t think I like it as much as I expected. Perhaps I would have liked it if it was just twice a week. Anyways, all the progress I made on myself till March was gone just like this. Gyms were closed, I started working at night rather than during the day. Obviously, I still had to stay online during the daytime though) which made my work hours feel like 9 AM - 12 AM and I became a mess once again like I used to be in college. 


    There were upsides to this year as well. Since i was working remotely and doing most of my work at night, I had more free time during the day. I started calling back to my  parents more often  and this time it was not them advising me to take care of their health but rather the opposite . Still they used to worry and advise me to stay cautious given the ever increasing cases and deaths in the US and mainstream media all over the world flashing it every day as their headline. Oh Parents , they are strange creatures, aren’t they ? I also reached out to friends from school, high school and college.  We talked about the old days, the pandemic, current socio-economic, political phenomena of both back home and of the US and also expressed how weird the things around us has gotten for all of us after being deemed as an “adult”.  


    Overall, I cannot clearly fathom how soon this year passed by. Given all this distress and probably considering it as one of the worst years in history, this should have felt as the longest year as we often say times move slow when things are bad and vice versa. However, for me, this year felt like it went on in a blink of an eye, probably, because in retrospect I don’t have any memories or events to remember except the pandemic for this year. We probably had never been this ready to say goodbye to any year and hopefully 2021 would be a better year by far. The big pharmas have deployed their best brains ( scientists) to the work and they have delivered as well. Let’s hope the insurgency brought by this pandemic would be placed on rest by these vaccines and 2021 would be about hope,resurgence,faith and more importantly about happiness, togetherness and celebration once again. 

    Resolution ? I don’t have anything except making myself a little more happier than I was this past year because one thing 2020 has taught us is you don’t know when you can no longer smile :)  So as the beatles say “ Life is very short, there is no time for fussing and fighting my friend”. Also remember the roaring 20s brought a big cultural shift in life in America and several parts of the world  after the 1918 pandemic. I am curious to see what happens after we all get over from this one . Would the world still remain the same or we will be part of some change that history remembers ? Time will tell I guess !


To  hope,faith, health, friends and families and to new beginning

Cheers !

HAPPY NEW YEAR 2021



Tuesday, November 3, 2020

Commands and Arguments - Kubernetes

1. Create a pod with the ubuntu image to run a container to sleep for 5000 seconds. Modify the file ubuntu-sleeper-2.yaml. Note: Only make the necessary changes. Do not modify the name.

Pod Name: ubuntu-sleeper-2 Command: sleep 5000

---> Edit the file as follows: 


The run kubectl create -f ubuntu-sleeper-2.yml