{"id":207,"date":"2018-10-16T03:32:14","date_gmt":"2018-10-16T03:32:14","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw93\/?p=207"},"modified":"2018-10-16T03:33:42","modified_gmt":"2018-10-16T03:33:42","slug":"deploying-a-spring-boot-app-with-mysql-on-openshift","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw93\/index.php\/2018\/10\/16\/deploying-a-spring-boot-app-with-mysql-on-openshift\/","title":{"rendered":"Deploying a Spring Boot App with MySQL on OpenShift"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/developers.redhat.com\/blog\/wp-content\/uploads\/2017\/11\/Logotype_RH_OpenShiftContainerPlatform_wLogo_CMYK_Black-1024x263.jpg\" alt=\"\" width=\"640\" height=\"164\" \/><\/p>\n<p>This article shows how to take an existing <a href=\"http:\/\/projects.spring.io\/spring-boot\/\">Spring Boot <\/a>standalone project that uses MySQL and deploy it on <a href=\"https:\/\/developers.redhat.com\/products\/openshift\/overview\/\">Red Hat OpenShift<\/a>, In the process, we\u2019ll create docker images which can be deployed to most container\/cloud platforms. I\u2019ll discuss creating a Dockerfile, pushing the container image to an OpenShift registry, and finally creating running pods with the Spring Boot app deployed.<\/p>\n<p>To develop and test using OpenShift on my local machine, I used <a href=\"https:\/\/developers.redhat.com\/products\/cdk\/overview\/\"><em>Red Hat Container Development Kit (CDK),<\/em><\/a> which provides a single-node OpenShift cluster running in a Red Hat Enterprise Linux VM, based on minishift. You can run CDK on top of Windows, macOS, or Red Hat Enterprise Linux. For testing, I used Red Hat Enterprise Linux Workstation release 7.3. It should work on macOS too.<\/p>\n<p>To create the Spring Boot app I used <a href=\"https:\/\/spring.io\/guides\/gs\/accessing-data-mysql\/\">this article<\/a> as a guide. I\u2019m using an existing <a href=\"https:\/\/docs.openshift.com\/container-platform\/3.3\/using_images\/db_images\/mysql.html\">openshift\/mysql-56-centos7<\/a> docker image to deploy MySQL to OpenShift.<\/p>\n<p>You can download the code used in this article from my personal <em><a href=\"https:\/\/github.com\/1984shekhar\/POC\/tree\/master\/mysql-springboot-docker-openshift\">github repo. <\/a><\/em> In this article, I\u2019ll be building container images locally, so you\u2019ll need to be able to build the project locally with Maven. This example exposes a rest service using: <em>com.sample.app.MainController.java<\/em>.<\/p>\n<p>In the repository, you\u2019ll find a Dockerfile in src\/main\/docker-files\/. The <em><a href=\"https:\/\/github.com\/1984shekhar\/POC\/blob\/master\/mysql-springboot-docker-openshift\/src\/main\/docker-files\/dockerfile_springboot_mysql\">dockerfile_springboot_mysql<\/a><\/em> file creates a docker image, having the Spring Boot application based on <em>java8<\/em> <em>docker<\/em> image as a base . While this is ok for testing, for production deployment you\u2019d want to use images that are based on Red Hat Enterprise Linux.<\/p>\n<p>Building the application:<\/p>\n<p>1. Use mvn clean install to build the project.<\/p>\n<p>2. Copy the generated jar in the target folder to src\/main\/docker-files. When creating the docker image, the application jar can be found at the same location.<\/p>\n<p>3. Set the database username, password, and URL in src\/main\/resources\/application.properties. Note: For OpenShift, it is recommended to pass these parameters into the container as environment variables.<\/p>\n<p>Now start the CDK VM to get your local OpenShift cluster running.<\/p>\n<p>1. Start the CDK VM using <em>minishift start<\/em>:<\/p>\n<p>$ minishift start<\/p>\n<p>2. Set your local environment for docker and the oc CLI:<\/p>\n<p>$ eval $(minishift oc-env)<br \/>\n$ eval $(minishift docker-env)<\/p>\n<p>Note: the above eval commands will not work on Windows. See the CDK documentation for more information.<\/p>\n<p>3. Login to OpenShift and Docker using the <em>developer<\/em> account:<\/p>\n<p>$ oc login<br \/>\n$ docker login -u developer -p $(oc whoami -t) $(minishift openshift registry)<\/p>\n<p>Now we\u2019ll build the container images.<\/p>\n<p>1. Change the directory location to src\/main\/docker-files within the project. Then, execute the following commands to build the <em>container<\/em> images. Note: The period (.) is required at the end of the docker build command to indicate the current directory:<\/p>\n<p>$ docker build -t springboot_mysql -f .\/dockerfile_springboot_mysql .<\/p>\n<p>Use the following command to view the container images that were created:<\/p>\n<p>$ docker images<\/p>\n<p>2. Add the tag springboot_mysql to the image, and push it to the OpenShift registry:<\/p>\n<p>$ docker tag springboot_mysql $(minishift openshift registry)\/myproject\/springboot_mysql<br \/>\n$ docker push $(minishift openshift registry)\/myproject\/springboot_mysql<\/p>\n<p>3. Next, pull the OpenShift<em> MySQL<\/em> image, and create it as an OpenShift application which will initialize and run it. Refer to <a href=\"https:\/\/docs.openshift.com\/container-platform\/3.3\/using_images\/db_images\/mysql.html\">the documentation<\/a> for more information:<\/p>\n<p>docker pull openshift\/mysql-56-centos7<br \/>\noc new-app -e MYSQL_USER=root -e MYSQL_PASSWORD=root -e MYSQL_DATABASE=test openshift\/mysql-56-centos7<\/p>\n<p>4. Wait for the pod running MySQL to be ready. You can check the status with oc get pods:<\/p>\n<p>$ oc get pods<br \/>\nNAME READY STATUS RESTARTS AGE<br \/>\nmysql-56-centos7-1-nvth9 1\/1 Running 0 3m<\/p>\n<p>5. Next, ssh to the <em>mysql<\/em> pod and create a MySQL root user with full privileges:<\/p>\n<p>$ oc rsh mysql-56-centos7-1-nvth9<br \/>\nsh-4.2$ mysql -u root<br \/>\nCREATE USER &#8216;root&#8217;@&#8217;%&#8217; IDENTIFIED BY &#8216;root&#8217;;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<\/p>\n<p>GRANT ALL PRIVILEGES ON *.* TO &#8216;root&#8217;@&#8217;%&#8217; WITH GRANT OPTION;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<\/p>\n<p>FLUSH PRIVILEGES;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<\/p>\n<p>exit<\/p>\n<p>6. Finally, initialize the Spring Boot app using <em>imagestream<\/em> springboot_mysql:<\/p>\n<p>$ oc get svc<br \/>\nNAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE<br \/>\nmysql-56-centos7 172.30.145.88 none 3306\/TCP 8m<\/p>\n<p>$ oc new-app -e spring_datasource_url=jdbc:mysql:\/\/172.30.145.88:3306\/test springboot_mysql<br \/>\n$ oc get pods<br \/>\nNAME READY STATUS RESTARTS AGE<br \/>\nmysql-56-centos7-1-nvth9 1\/1 Running 0 12m<br \/>\nspringbootmysql-1-5ngv4 1\/1 Running 0 9m<\/p>\n<p>7. Check the pod logs:<\/p>\n<p>oc logs -f springbootmysql-1-5ngv4<\/p>\n<p>8. Next, expose the service as route:<\/p>\n<p>$ oc get svc<br \/>\nNAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE<br \/>\nmysql-56-centos7 172.30.242.225 none 3306\/TCP 14m<br \/>\nspringbootmysql 172.30.207.116 none 8080\/TCP 1m<\/p>\n<p>$ oc expose svc springbootmysql<br \/>\nroute &#8220;springbootmysql&#8221; exposed<\/p>\n<p>$ oc get route<br \/>\nNAME HOST\/PORT PATH SERVICES PORT TERMINATION WILDCARD<br \/>\nspringbootmysql springbootmysql-myproject.192.168.42.182.nip.io springbootmysql 8080-tcp None<\/p>\n<p>9. Test the application using curl. You should see a list of all entries in the database table:<\/p>\n<p>$ curl -v http:\/\/springbootmysql-myproject.192.168.42.182.nip.io\/demo\/all<\/p>\n<p>10. Next, use curl to create an entry in the db:<\/p>\n<p>$ curl http:\/\/springbootmysql-myproject.192.168.42.182.nip.io\/demo\/add?name=SpringBootMysqlTest<br \/>\nSaved<\/p>\n<p>11. View the updated list of entries in the database:<\/p>\n<p>$ curl http:\/\/springbootmysql-myproject.192.168.42.182.nip.io\/demo\/all<\/p>\n<p>[{&#8220;name&#8221;:&#8221;UBUNTU 17.10 LTS&#8221;,&#8221;lastaudit&#8221;:1502409600000,&#8221;id&#8221;:1},{&#8220;name&#8221;:&#8221;RHEL 7&#8243;,&#8221;lastaudit&#8221;:1500595200000,&#8221;id&#8221;:2},{&#8220;name&#8221;:&#8221;Solaris 11&#8243;,&#8221;lastaudit&#8221;:1502582400000,&#8221;id&#8221;:3},{&#8220;name&#8221;:&#8221;SpringBootTest&#8221;,&#8221;lastaudit&#8221;:1519603200000,&#8221;id&#8221;:4},{&#8220;name&#8221;:&#8221;SpringBootMysqlTest&#8221;,&#8221;lastaudit&#8221;:1519603200000,&#8221;id&#8221;:5}<\/p>\n<p>That\u2019s it!<\/p>\n<p>I hope this article is helpful to you for migrating an existing <em>spring-boot<\/em> application to OpenShift. Just a note, in production environments, one should use Red Hat Supportable Images. This document is intended for development purposes only. It should assist you in creating <em>spring-boot<\/em> applications that run in containers; and in how to set up <i>MySQL<\/i> connectivity for <em>spring-boot<\/em> in OpenShift.<\/p>\n<p><a href=\"https:\/\/developers.redhat.com\/blog\/2018\/03\/27\/spring-boot-mysql-openshift\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to take an existing Spring Boot standalone project that uses MySQL and deploy it on Red Hat OpenShift, In the process, we\u2019ll create docker images which can be deployed to most container\/cloud platforms. I\u2019ll discuss creating a Dockerfile, pushing the container image to an OpenShift registry, and finally creating running pods &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw93\/index.php\/2018\/10\/16\/deploying-a-spring-boot-app-with-mysql-on-openshift\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Deploying a Spring Boot App with MySQL on OpenShift&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-207","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/comments?post=207"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/207\/revisions"}],"predecessor-version":[{"id":208,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/207\/revisions\/208"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/media?parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/categories?post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/tags?post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}