Enterprise Java

OpenShift v3: Getting Started with Java EE 7 using WildFly and MySQL

openshift-originOpenShift is Red Hat’s open source PaaS platform. OpenShift v3 (due to be released this year) will provide a holistic experience on running your microservices using Docker and Kubernetes. In a classic Red Hat way, all the work is done in the open source at OpenShift Origin. This will also drive the next major release of OpenShift Online and OpenShift Enterprise.

OpenShift v3 uses a new platform stack that is using plenty of community projects where Red Hat contributes such as Fedora, Centos, Docker, Project Atomic, Kubernetes, and OpenStack. OpenShift v3 Platform Combines Docker, Kubernetes, Atomic and More explain this platform stack in detail.

openshift-stack

This tech tip will explain how to get started with OpenShift v3, lets get started!

Getting Started with OpenShift v3

Pre-built binaries for OpenShift v3 can be downloaded from Origin at GitHub. However the simplest way to get started is to run OpenShift Origin as a Docker container.

OpenShift Application Lifecycle provide complete details on what it takes to run a sample application from scratch. This blog will use those steps and adapt them to run using boot2docker VM on Mac. And in the process we’ll also deploy a Java EE 7 application on WildFly which will be accessing database on a separate MySQL container.

Here is our deployment diagram:

techtip73-openshift-v3-wildfly-mysql-deployment

  • WildFly and MySQL are running on separate pods.
  • Each of them is wrapped in a Replication Controller to enable simplified scaling.
  • Each Replication Controller is published as a Service.
  • WildFly talks to the MySQL service, as opposed to directly to the pod. This is important as Pods, and IP addresses assigned to them, are ephemeral.

Lets get started!

Configure Docker Daemon

  1. Configure the docker daemon on your host to trust the docker registry service you’ll be starting. This registry will be used to push images for build/test/deploy cycle.
    • Log into boot2docker VM as:
      boot2docker ssh
    • Edit the file
      sudo vi /var/lib/boot2docker/profile

      This will be an empty file.

    • Add the following name/value pair:
      EXTRA_ARGS="--insecure-registry 172.30.17.0/24"

      Save the file, and quit the editor.

    This will instruct the docker daemon to trust any docker registry on the 172.30.17.0/24 subnet.

Check out OpenShift v3 and Java EE 7 Sample

  1. Download and Install Go and setup GOPATH and PATH environment variable. Check out OpenShift origin directory:
    openshift> git clone https://github.com/openshift/origin.git
    Cloning into 'origin'...
    remote: Counting objects: 21451, done.
    remote: Compressing objects: 100% (76/76), done.
    remote: Total 21451 (delta 26), reused 15 (delta 1)
    Receiving objects: 100% (21451/21451), 35.11 MiB | 1.24 MiB/s, done.
    Resolving deltas: 100% (11392/11392), done.
    Checking connectivity... done.
    

    Note the directory where its checked out. In this case, its ~/workspaces/openshift.

    Build the workspace:

    make release
    origin> make release
    rm -rf _output Godeps/_workspace/pkg
    hack/build-release.sh
    Unable to find image 'openshift/origin-release' locally
    Pulling repository openshift/origin-release
    c59f040a4054: Download complete 
    511136ea3c5a: Download complete 
    5b12ef8fd570: Download complete 
    dade6cb4530a: Download complete 
    bd195712b86c: Download complete 
    4aae29c1c846: Download complete 
    d8738da3ed4c: Download complete 
    7f7491063665: Download complete 
    b5cfa9eebdb5: Download complete 
    2f6297477756: Download complete 
    dc98bd93df2f: Download complete 
    1be2a7550a3d: Download complete 
    42fd088f8c55: Download complete 
    b6f6ad4012df: Download complete 
    Status: Downloaded newer image for openshift/origin-release:latest
    
  2. Check out javaee7-hol workspace that has been converted to a Kubernetes application:
    origin> git clone https://github.com/bparees/javaee7-hol
    Cloning into 'javaee7-hol'...
    remote: Counting objects: 1159, done.
    remote: Compressing objects: 100% (49/49), done.
    remote: Total 1159 (delta 35), reused 37 (delta 21)
    Receiving objects: 100% (1159/1159), 20.35 MiB | 1.78 MiB/s, done.
    Resolving deltas: 100% (605/605), done.
    Checking connectivity... done.
    

    This is also done in ~/workspaces/openshift directory.

Start OpenShift v3 Container

  1. Start OpenShift Origin as Docker container:
    ~> docker run -d --name "openshift-origin" --net=host --privileged \
      -v /var/run/docker.sock:/var/run/docker.sock  \
      -v /Users/arungupta/workspaces/openshift:/workspaces/openshift \
      -v /usr/local/bin/docker:/usr/bin/docker \
      openshift/origin start --public-master=$(boot2docker ip)
    
    The VM's Host only interface IP address is: 
    
    0ae85091e7ad7e85697575369f008273a6673d112b49860f5557a61d9e82d908
    

    Note ~/workspaces/openshift directory is mounted as /workspaces/openshift volume in the container. Some additional volumes are mounted as well.

    Check that the container is running:

    ~> docker ps
    CONTAINER ID        IMAGE                    COMMAND                CREATED             STATUS              PORTS               NAMES
    0ae85091e7ad        openshift/origin:beta1   "/usr/bin/openshift    2 minutes ago       Up 2 minutes                            openshift-origin
    
  2. Log into the container as:
    openshift> docker exec -it `docker ps | grep origin:beta1 | awk '{ print $1 }'` bash
    [root@boot2docker openshift]#
    
  3. Install Docker registry in the container by giving the following command:
    export CERT_DIR=/var/lib/openshift/openshift.local.certificates/openshift-client && \
    export KUBECONFIG=/var/lib/openshift/openshift.local.certificates/admin/.kubeconfig && \
    cd /workspaces/openshift/origin && hack/install-registry.sh
    
  4. Confirm that the registry is running by getting the list of pods:
    [root@boot2docker origin]# osc get pods
    POD                       IP                  CONTAINER(S)         IMAGE(S)                    HOST                    LABELS                                                                                                             STATUS
    docker-registry-1-gfkbz   172.17.0.14         registry-container   openshift/docker-registry   boot2docker/127.0.0.1   deployment=docker-registry-1,deploymentconfig=docker-registry,name=registrypod,template=docker-registry-template   Running
    

    osc is OpenShift Client CLI and allows to create and manage OpenShift projects. Some of the kubectl commands can also be using this script.

  5. Confirm the registry service is running. Note the actual IP address may vary:
    [root@boot2docker origin]# osc get services
    NAME                LABELS                                    SELECTOR            IP                  PORT
    docker-registry     template=docker-registry-template         name=registrypod    172.30.17.142       5001
    kubernetes          component=apiserver,provider=kubernetes   <none>              172.30.17.2         443
    kubernetes-ro       component=apiserver,provider=kubernetes   <none>              172.30.17.1         80
    
  6. Confirm that the registry service is accessible:
    curl `osc get services docker-registry -o template --template="{{ .portalIP}}:{{ .port }}"`

    And look for the output:

    "docker-registry server (dev) (v0.9.0)"

Access OpenShift v3 Web Console

  1. OpenShift Origin server is now up and running. Find out the host’s IP address using boot2docker ip and open http://<IP addresss of boot2docker host>:8444 to view OpenShift Web Console in your browser.For example, the console is accessible at https://192.168.59.103:8444/ on this machine.

    techtip73-openshift-origin-browser-cert-1007x1024

    You will need to have the browser accept the certificate at https://<host>:8444 before the console can consult the OpenShift API. Of course this would not be necessary with a legitimate certificate.

  2. OpenShift Origin login screen shows up. Enter the username/password as admin/admin:

    techtip73-openshift-origin-login-screen

    and click on the “Log In” button. The default web console looks like:

    techtip73-openshift-v3-default-login-screen

Create OpenShift v3 Project

  1. Use project.json from github.com/openshift/origin/blob/master/examples/sample-app/project.json in the OpenShift v3 container and create a test project as:
    [root@boot2docker origin]# osc create -f /workspaces/openshift/origin/examples/sample-app/project.json 
    test
    

    Refreshing the web console now shows:

    techtip73-openshift-origin-test-project

    Clicking on “OpenShift 3 Sample” shows an empty project description:

    techtip73-openshift-v3-empty-project

  2. Request creation of the application template:
    [root@boot2docker openshift]# cd javaee7-hol/
    [root@boot2docker javaee7-hol]# osc process -n test -f application-template-jeebuild.json | osc create -n test -f -
    frontend
    origin-jee-sample
    wildfly-8-centos
    jee-sample-build
    frontend
    mysql
    database
    
  3. Web Console automatically refreshes and shows:

    techtip73-openshift-v3-javaee7-project-default

    The list of services running can be seen as:

    techtip73-openshift-v3-javaee7-project-services

Build the Project

  1. Trigger an initial build of your project:
    [root@boot2docker javaee7-hol]# osc start-build -n test jee-sample-build
    cee73fbc-b32d-11e4-a0f2-5282784576db
    
  2. Monitor the builds and wait for the status to go to “complete” (this can take a few minutes):
    [root@boot2docker javaee7-hol]#  osc get -n test builds
    NAME                                   TYPE                STATUS              POD
    cee73fbc-b32d-11e4-a0f2-5282784576db   STI                 Running             build-cee73fbc-b32d-11e4-a0f2-5282784576db
    

    You can add the –watch flag to wait for updates until the build completes:

    [root@boot2docker javaee7-hol]#  osc get -n test builds --watch
    NAME                                   TYPE                STATUS              POD
    cee73fbc-b32d-11e4-a0f2-5282784576db   STI                 Running             build-cee73fbc-b32d-11e4-a0f2-5282784576db
    NAME                                   TYPE                STATUS              POD
    cee73fbc-b32d-11e4-a0f2-5282784576db   STI                 Complete            build-cee73fbc-b32d-11e4-a0f2-5282784576db
    

    Wait for the STATUS column to show Complete. It will take a few minutes as all the components (WIldFly, MySQL, Java EE 7 application) are provisioned. Effectively, their new Docker images are created and pushed to the local registry that was started earlier.

    Hit Ctrl+C to stop watching builds after the status changes to Complete.

  3. Complete log of the build can be seen as:
    [root@boot2docker javaee7-hol]# osc build-logs -n test cee73fbc-b32d-11e4-a0f2-5282784576db
    2015-02-13T03:10:07.065718606Z Cloning into '/tmp/sti552730161/upload/src'...
    2015-02-13T03:11:03.184576532Z I0213 03:11:03.184326       1 build.go:160] Successfully built 172.30.17.142:5001/test/origin-jee-sample
    2015-02-13T03:11:07.261489310Z The push refers to a repository [172.30.17.142:5001/test/origin-jee-sample] (len: 1)
    2015-02-13T03:11:07.261489310Z Sending image list
    2015-02-13T03:11:07.275256699Z Pushing repository 172.30.17.142:5001/test/origin-jee-sample (1 tags)
    2015-02-13T03:11:07.307836081Z Pushing
    Buffering to disk07.314345045Z Buffering to disk 1.024 kB
    2015-02-13T03:11:07.315452728Z Buffering to disk
    Pushing-13T03:11:07.323973430Z Pushing [=========================>                         ]    512 B/1.024 kB 0
    
    . . .
    
    Pushing-13T03:13:05.932725921Z Pushing [================================================>  ]  6.56 MB/6.704 MB 0
    Pushing-13T03:13:05.942989165Z Pushing [=================================================> ] 6.635 MB/6.704 MB 0
    Pushing-13T03:13:05.943025109Z Pushing [=================================================> ] 6.702 MB/6.704 MB 0
    Pushing-13T03:13:05.947665528Z Pushing [==================================================>] 6.704 MB/6.704 MB
    2015-02-13T03:13:05.953476634Z Image successfully pushed
    2015-02-13T03:13:05.953476634Z Pushing tag for rev [b0a7868377c1] on {http://172.30.17.142:5001/v1/repositories/test/origin-jee-sample/tags/latest}
    
  4. Check for the application pods to start:
    [root@boot2docker javaee7-hol]# osc get -n test pods    
    POD                                          IP                  CONTAINER(S)        IMAGE(S)                                                                                                     HOST                    LABELS                                                                              STATUS
    build-cee73fbc-b32d-11e4-a0f2-5282784576db   172.17.0.17         sti-build           openshift/origin-sti-builder:v0.3                                                                            boot2docker/127.0.0.1   <none>                                                                              Succeeded
    database-1-axe6d                             172.17.0.16         jee-database        mysql                                                                                                        boot2docker/127.0.0.1   deployment=database-1,deploymentconfig=database,name=database,template=jee-sample   Running
    frontend-1-pkcia                             172.17.0.20         jee-sample          172.30.17.142:5001/test/origin-jee-sample:b0a7868377c10a50b40019be70e22d040b9a2bb2d87fa3932b700494e50a4de6   boot2docker/127.0.0.1   deployment=frontend-1,deploymentconfig=frontend,name=frontend,template=jee-sample   Running
    

    Note, that the “frontend” and “database” pods are now running.

  5. Determine IP of the “frontend” service:
    [root@boot2docker javaee7-hol]# osc get -n test services
    NAME                LABELS                SELECTOR            IP                  PORT
    frontend            template=jee-sample   name=frontend       172.30.17.115       8080
    mysql               template=jee-sample   name=database       172.30.17.75        3306
    
  6. Access the application at http://<IP address of “frontend”>:8080/movieplex7-1.0-SNAPSHOT should work. Note the IP address may (most likely will) vary. In this case, it would be http://172.30.17.115:8080/moviexplex7-1.0-SNAPSHOT.The app would not be accessible yet, as some further debugging is required to configure firewall on Mac when OpenShift v3 is used as Docker container. Until we figure that out, you can do docker ps in your boot2docker VM to see the list of all the containers:
    origin> docker ps
    CONTAINER ID        IMAGE                                                                                                       COMMAND                CREATED             STATUS              PORTS               NAMES
    c776cabb72f8        172.30.17.17:5001/test/origin-jee-sample:91d1b5a5ed58e1642aafbd652eba0b7eae3a0ac08c17729d205ae88204f64449   "/tmp/scripts/run"     2 hours ago         Up 2 hours                              k8s_jee-sample.7af25557_frontend-2-9ciae.test.api_12a0d77e-b3b5-11e4-81f6-5282784576db_53f91f55                      
    e7f87112c473        openshift/origin-pod:beta1                                                                                  "/pod"                 2 hours ago         Up 2 hours                              k8s_POD.9cbfdefa_frontend-2-9ciae.test.api_12a0d77e-b3b5-11e4-81f6-5282784576db_64263b61                             
    7e7579db5f82        openshift/origin-pod:beta1                                                                                  "/pod"                 2 hours ago         Up 2 hours                              k8s_POD.123cbdd6_build-e075047d-b3b4-11e4-81f6-5282784576db.test.api_e07bcdb2-b3b4-11e4-81f6-5282784576db_376c0e63   
    74082d7791ec        openshift/origin-pod:beta1                                                                                  "/pod"                 2 hours ago         Up 2 hours                              k8s_POD.123cbdd6_build-76364bb6-b3b2-11e4-81f6-5282784576db.test.api_7639e3a5-b3b2-11e4-81f6-5282784576db_f653c7fd   
    3e42dacb0da7        mysql:latest                                                                                                "/entrypoint.sh mysq   3 hours ago         Up 3 hours                              k8s_jee-database.f12628ae_database-1-bqg8v.test.api_56093bac-b3b2-11e4-81f6-5282784576db_193dddf7                    
    cb551c8cf7bf        openshift/origin-pod:beta1                                                                                  "/pod"                 3 hours ago         Up 3 hours                              k8s_POD.95f2def6_database-1-bqg8v.test.api_56093bac-b3b2-11e4-81f6-5282784576db_295fc320                             
    e8719152df42        openshift/docker-registry:latest                                                                            "\"sh -c 'echo \"$OP   3 hours ago         Up 3 hours                              k8s_registry-container.4e338487_docker-registry-1-bt49s.default.api_177a691c-b3b0-11e4-81f6-5282784576db_a6f4f83b    
    d3ecc5841c3b        openshift/origin-pod:beta1                                                                                  "/pod"                 3 hours ago         Up 3 hours                              k8s_POD.8a29deef_docker-registry-1-bt49s.default.api_177a691c-b3b0-11e4-81f6-5282784576db_ba53d62b                   
    a506222fae91        openshift/origin:beta1                                                                                      "/usr/bin/openshift    3 hours ago         Up 3 hours                              openshift-origin
    

    And then login to the container associated with frontend as:

    docker exec -it `docker ps | grep origin-jee-sample | awk '{ print $1 }'` bash

    This will log in to the Docker container where you can check that the application is deployed successfully by giving the following command:

    bash-4.2$ curl http://localhost:8080/movieplex7-1.0-SNAPSHOT/

    This will print the index.html page from the application which has license at the top and rest of the page after that.

    Now once the firewall issue is resolved, this page will then be accessible on host Mac as well.

Lets summarize:

  • Cloned the OpenShift Origin and Java EE 7 sample repo
  • Started OpenShift v3 as Docker container
  • Loaded the OpenShift v3 Web Console
  • Create an OpenShift v3 project
  • Loaded Java EE 7 application template
  • Triggered a build, which deployed the application

Enjoy!

Arun Gupta

Arun is a technology enthusiast, avid runner, author of a best-selling book, globe trotter, a community guy, Java Champion, JavaOne Rockstar, JUG Leader, Minecraft Modder, Devoxx4Kids-er, and a Red Hatter.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button