{"id":169,"date":"2017-04-12T22:33:16","date_gmt":"2017-04-12T14:33:16","guid":{"rendered":"http:\/\/www.jsjs.org\/?p=169"},"modified":"2017-04-12T22:33:16","modified_gmt":"2017-04-12T14:33:16","slug":"quick-guide-to-developing-microservices-on-kubernetes-and-docker","status":"publish","type":"post","link":"https:\/\/blog.jsjs.org\/?p=169","title":{"rendered":"Quick Guide to Developing Microservices on Kubernetes and Docker"},"content":{"rendered":"<p>http:\/\/www.eclipse.org\/community\/eclipse_newsletter\/2017\/january\/article2.php<\/p>\n<p>&nbsp;<\/p>\n<p>As Java developers we\u2019re often really busy with large backlogs, customer issues and countless disruptions. It can be quite daunting finding the time to learn all about things like <a href=\"http:\/\/kubernetes.io\/\" target=\"_blank\" rel=\"noopener\">Kubernetes<\/a> and its associated tools and technologies (<a href=\"http:\/\/kubernetes.io\/docs\/user-guide\/kubectl-overview\/\" target=\"_blank\" rel=\"noopener\">kubectl<\/a>, <a href=\"https:\/\/www.openshift.org\/\" target=\"_blank\" rel=\"noopener\">OpenShift<\/a>, <a href=\"https:\/\/docs.openshift.org\/latest\/cli_reference\/get_started_cli.html\" target=\"_blank\" rel=\"noopener\">oc<\/a>, <a href=\"http:\/\/docker.com\/\" target=\"_blank\" rel=\"noopener\">Docker<\/a> and <a href=\"https:\/\/coreos.com\/rkt\/\" target=\"_blank\" rel=\"noopener\">Rkt<\/a> and standards like <a href=\"https:\/\/cncf.io\/\" target=\"_blank\" rel=\"noopener\">CNCF<\/a> and <a href=\"https:\/\/www.opencontainers.org\/\" target=\"_blank\" rel=\"noopener\">OCI<\/a>). Its well worth doing if you have the time mind you! We recommend you dive into Kubernetes and see where that takes you whenever you get some spare time :).<\/p>\n<p align=\"center\"><a href=\"https:\/\/fabric8.io\/\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"img-responsive\" src=\"http:\/\/www.eclipse.org\/community\/eclipse_newsletter\/2017\/january\/images\/fabric8.png\" alt=\"fabric 8 io logo\" \/><\/a><\/p>\n<p>On the <a href=\"https:\/\/fabric8.io\/\" target=\"_blank\" rel=\"noopener\">fabric8<\/a> project we\u2019ve been focused on making it easy for developers to create, build, deploy and manage microservices on top of Kubernetes for some time.<\/p>\n<p>Up to now we\u2019ve mostly focused on the end to end lifecycle of creating new microservices, then changing code through continuous delivery then having continuous improvement. Lately we\u2019ve been focused more on the <i>pre-git-push<\/i> phase. Where you\u2019re hacking code on your laptop and trying things out before you want to commit and push into your git repository.<\/p>\n<p>Lots of Java developers are used to using application servers, creating deployment units (jars\/wars), deploying them and running them in app servers using Maven. Then they can use Maven from the command line or easily inside their IDE to do most of the work.<\/p>\n<p>So we figured, why not make Kubernetes look and feel like an application server to a Java developer? So you build and deploy your application from maven like you would with other maven plugins like spring-boot, tomcat, jetty, wildfly, karaf et al. Then you can get started quickly by just treating Kubernetes as a kind of application server.<\/p>\n<p>Kubernetes is actually way more awesome than an application server; its more like an application <i>cloud<\/i> as:<\/p>\n<ul>\n<li>kubernetes can keep running multiple instances of each of your apps including automatic restarts on software or hardware failures<\/li>\n<li>automatic load balancing when invoking your apps<\/li>\n<li>each app instance is isolated as a separate process so its much easier to monitor metrics and logs<\/li>\n<\/ul>\n<h2>Setup your maven project<\/h2>\n<p>Do you have an existing maven project for your Java app? Maybe its a Spring Boot or WildFly Swarm app; or a flat classpath, fat jar? If you don\u2019t have a maven app to hand, visit <a href=\"http:\/\/start.spring.io\/\" target=\"_blank\" rel=\"noopener\">start.spring.io<\/a> and create one.<\/p>\n<p>To enable fabric8 on your project just type this <a href=\"https:\/\/maven.fabric8.io\/#fabric8:setup\" target=\"_blank\" rel=\"noopener\">fabric8:setup<\/a> command which adds the <a href=\"https:\/\/maven.fabric8.io\/\" target=\"_blank\" rel=\"noopener\">fabric8-maven-plugin<\/a> to your <i>pom.xml<\/i> like this <a href=\"https:\/\/maven.fabric8.io\/#fabric8:setup\" target=\"_blank\" rel=\"noopener\">example<\/a>.<\/p>\n<pre>mvn io.fabric8:fabric8-maven-plugin:3.2.8:setup<\/pre>\n<p>Note that all the <a href=\"https:\/\/github.com\/fabric8-quickstarts\/\" target=\"_blank\" rel=\"noopener\">fabric8 quickstarts<\/a> already have the fabric8 maven plugin enabled so you don\u2019t need to do the above!<\/p>\n<p>Now that we have enabled the fabric8 maven plugin for our project we can have some fun!<\/p>\n<h2>Starting and stopping a local kubernetes cluster<\/h2>\n<p>To develop on kubernetes you will need a kubernetes cluster; just like you need an instance of an application server to deploy your app. If someone in your organisation has provided you with a kubernetes cluster then feel free to use that; you just need to connect to it via the <a href=\"http:\/\/kubernetes.io\/docs\/user-guide\/kubectl-overview\/\" target=\"_blank\" rel=\"noopener\">kubectl<\/a> too.<\/p>\n<p>However if you don\u2019t have any kubernetes clusters to hand its very easy to spin up your own local cluster. Using a local kubernetes cluster is really handy for development; it lets you build your app and run it locally before you do a git commit and trigger the CI \/ CD pipeline.<\/p>\n<p>Before you start please make sure you have the <b><\/b><a href=\"https:\/\/maven.fabric8.io\/#fabric8:cluster-start\" target=\"_blank\" rel=\"noopener\">prerequisites installed<\/a> for your platform (basically a recent Apache Maven distro and have a working hypervisor for your platform like xhyve, hyper-v or kvm).<\/p>\n<p>Now with the fabric8-maven-plugin you just use the goal <a href=\"https:\/\/maven.fabric8.io\/#fabric8:cluster-start\" target=\"_blank\" rel=\"noopener\">fabric8:cluster-start<\/a> to spin up a local cluster:<\/p>\n<pre>mvn fabric8:cluster-start<\/pre>\n<p>Wait a few minutes for some stuff to download and then you\u2019ve now got a local installation of a Kubernetes cluster running on your machine! No installation of Docker, VirtualBox or Vagrant is required!<\/p>\n<p>Under the covers this downloaded <a href=\"https:\/\/github.com\/fabric8io\/gofabric8\/\" target=\"_blank\" rel=\"noopener\">gofabric8<\/a> and ran <i>gofabric8<\/i> start which then downloaded <a href=\"https:\/\/github.com\/kubernetes\/minikube\/\" target=\"_blank\" rel=\"noopener\">minikube<\/a> and <a href=\"http:\/\/kubernetes.io\/docs\/user-guide\/kubectl-overview\/\" target=\"_blank\" rel=\"noopener\">kubectl<\/a> binaries and installs them into <i>~\/.fabric8\/bin<\/i><\/p>\n<p>Want to use OpenShift instead of Kubernetes? Use <a href=\"https:\/\/maven.fabric8.io\/#using-openshift\" target=\"_blank\" rel=\"noopener\">this command<\/a> instead:<\/p>\n<pre>mvn fabric8:cluster-start -Dfabric8.cluster.kind=openshift<\/pre>\n<p>You can now use the <a href=\"http:\/\/kubernetes.io\/docs\/user-guide\/kubectl-overview\/\" target=\"_blank\" rel=\"noopener\">kubectl<\/a> command line to get pods and whatnot:<\/p>\n<pre>kubectl get pod<\/pre>\n<p>When you\u2019re done and want to stop the kubernetes cluster just use <a href=\"https:\/\/maven.fabric8.io\/#fabric8:cluster-stop\" target=\"_blank\" rel=\"noopener\">fabric8:cluster-stop<\/a><\/p>\n<pre>mvn fabric8:cluster-stop<\/pre>\n<p>you can restart it again at any time via <a href=\"https:\/\/maven.fabric8.io\/#fabric8:cluster-start\" target=\"_blank\" rel=\"noopener\">fabric8:cluster-start<\/a><\/p>\n<h2>Running your app<\/h2>\n<p>The simplest way to run your app is via<a href=\"https:\/\/maven.fabric8.io\/#fabric8:run\" target=\"_blank\" rel=\"noopener\">fabric8:run<\/a><\/p>\n<pre>mvn fabric8:run<\/pre>\n<p>This will build your app (compile code, run tests, generate the jar and package it up into an immutable docker image), generate the kubernetes manifest and deploy it then tail the log output so you can see how your app behaves. If you hit <i>Ctrl-C<\/i> it then terminates the app.<\/p>\n<p>So that the <i>fabric8:run<\/i> goal works kinda like other run goals in other maven projects like <i>spring-boot<\/i>, <i>tomcat<\/i>, <i>jetty<\/i>, <i>karaf<\/i> and <i>wildfly<\/i>.<\/p>\n<h2>Deploy or undeploy your app<\/h2>\n<p>Deploying your app is a little like running <a href=\"https:\/\/maven.fabric8.io\/#fabric8:run\" target=\"_blank\" rel=\"noopener\">fabric8:run<\/a> in the background.<\/p>\n<p>To deploy your app into kubernetes you use the <a href=\"https:\/\/maven.fabric8.io\/#fabric8:deploy\" target=\"_blank\" rel=\"noopener\">fabric8:deploy<\/a> goal<\/p>\n<pre>mvn fabric8:deploy<\/pre>\n<p>This will build your java code and run your unit tests, generate the docker image, create the kubernetes manifest and deploy them into kubernetes.<\/p>\n<p>To remove our app from kubernetes just use the <a href=\"https:\/\/maven.fabric8.io\/#fabric8:undeploy\" target=\"_blank\" rel=\"noopener\">fabric8:undeploy<\/a> goal<\/p>\n<pre>mvn fabric8:undeploy<\/pre>\n<h2>Viewing logs<\/h2>\n<p>Once you\u2019ve deployed your app you probably want to check its logs. So you can type:<\/p>\n<pre>mvn fabric8:log<\/pre>\n<p>then hit <i>Ctrl-C<\/i> to terminate tailing the log.<\/p>\n<p>If you don\u2019t want to keep tailing the log and just want to see the current log output try this:<\/p>\n<pre>mvn fabric8:log -Dfabric8.log.follow=false<\/pre>\n<h2>Start and stop<\/h2>\n<p>If you have deployed your app you sometimes want to stop it running so that you can later on start it again. e.g. since its using up resources on your laptop or its invoking some other microservice that you wish to take offline.<\/p>\n<p>To stop your application just use the <a href=\"https:\/\/maven.fabric8.io\/#fabric8:stop\" target=\"_blank\" rel=\"noopener\">fabric8:stop<\/a> and <a href=\"https:\/\/maven.fabric8.io\/#fabric8:start\" target=\"_blank\" rel=\"noopener\">fabric8:start<\/a> goal:<\/p>\n<pre>mvn fabric8:stop\n\u2026\nmvn fabric8:start<\/pre>\n<p>All the <a href=\"https:\/\/maven.fabric8.io\/#fabric8:start\" target=\"_blank\" rel=\"noopener\">fabric8:start<\/a> and <a href=\"https:\/\/maven.fabric8.io\/#fabric8:stop\" target=\"_blank\" rel=\"noopener\">fabric8:stop<\/a> goals do is to scale your application on Kubernetes\u200a\u2014\u200aas in set the number of expected replicas\u200a\u2014\u200athe number of running processes of your app.<\/p>\n<p>You can also use <a href=\"https:\/\/maven.fabric8.io\/#fabric8:start\" target=\"_blank\" rel=\"noopener\">fabric8:start<\/a> to scale up your app to more than one pod if you wish<\/p>\n<pre>mvn fabric8:start -Dfabric8.replicas=2<\/pre>\n<p>You can also use the <a href=\"http:\/\/kubernetes.io\/docs\/user-guide\/kubectl-overview\/\" target=\"_blank\" rel=\"noopener\">kubectl<\/a> command line too:<\/p>\n<pre>kubectl scale\u200a\u2014\u200areplicas=2 deployment myappname<\/pre>\n<h2>Debugging<\/h2>\n<p>Outside of unit tests we highly recommend you try and run your apps and do all your integration and system testing inside Kubernetes. Then you are spending most of your time using a production-like environment (rather than your laptop with its different operating system, networks and storage etc). It also means you can easily deploy multiple microservices into the same namespace and test combinations of microservices easily.<\/p>\n<p>To debug your app, just run the <a href=\"https:\/\/maven.fabric8.io\/#fabric8:debug\" target=\"_blank\" rel=\"noopener\">fabric8:debug<\/a> goal<\/p>\n<pre>mvn fabric8:debug<\/pre>\n<p>This will use the default Java remote debugging port of <i>5005<\/i>. You can specify a different port if you wish<\/p>\n<pre>mvn fabric8:debug -Dfabric8.debug.port=8000<\/pre>\n<p>Once that goal is running the debug port will be open on your laptop (<i>localhost<\/i>) which then connects using port forwarding to your most recent running pod for your app.<\/p>\n<p>So you can now just start a Remote debug execution in your IDE (like you do to debug remote application server instances) and you can set breakpoints and debug your pod while its inside kubernetes.<\/p>\n<p>e.g. in IntelliJ here\u2019s how you add a Run\/Debug execution:<\/p>\n<p align=\"center\"><img decoding=\"async\" class=\"img-responsive\" src=\"http:\/\/www.eclipse.org\/community\/eclipse_newsletter\/2017\/january\/images\/debug.png\" alt=\"debug and run configuration\" \/><\/p>\n<p align=\"center\">add a new Remote kind of Run\/Debug configuration<\/p>\n<p align=\"center\"><img decoding=\"async\" class=\"img-responsive\" src=\"http:\/\/www.eclipse.org\/community\/eclipse_newsletter\/2017\/january\/images\/debugtitle.png\" alt=\"debug title\" \/><\/p>\n<p align=\"center\">give the Run\/Debug configuration a title<\/p>\n<p>To see this in action check out the video below.<\/p>\n<h2>Continuous Delivery and Releasing<\/h2>\n<p>Once you are ready to start releasing your code run this command:<\/p>\n<pre>mvn fabric8:import<\/pre>\n<p>This will import your local project into fabric8.<\/p>\n<p>If your code is not already hosted in a git repository it will create a new git repository and import your code. Then it\u2019ll show you a link to open the project in the fabric8 console so you can add a Continuous Delivery pipeline to your project to enable a Jenkins build for full CI \/ CD<\/p>\n<h2>Demo<\/h2>\n<p>To see all of these maven goals in action check out this short video:<\/p>\n<div class=\"embed-responsive embed-responsive-16by9\"><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/CYrCW1SjpOI\" width=\"560\" height=\"150\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\" data-mce-fragment=\"1\"><\/iframe><\/div>\n<p align=\"center\">local java development with kubernetes and docker using fabric8 maven plugin<\/p>\n<h2>Summary<\/h2>\n<p>So once you\u2019ve started a cluster (<i>cluster-start<\/i>) the fabric8 maven goals are very similar to other application server based maven plugins; run, <i>deploy\/undeploy, start\/stop<\/i> are the main <i>goals<\/i> with <i>log<\/i> and <i>debug<\/i> pretty handy too.<\/p>\n<p>Once you are familiar with those and you\u2019ve done some development on kubernetes with the fabric8 maven plugin you might start to try out using the <a href=\"http:\/\/kubernetes.io\/docs\/user-guide\/kubectl-overview\/\" target=\"_blank\" rel=\"noopener\">kubectl<\/a> tool also which is handy for querying or watching the state of kubernetes, performing updates to them. e.g. to watch pods start and stop type:<\/p>\n<pre>kubectl get pod -w<\/pre>\n<p>Please try out the <a href=\"https:\/\/maven.fabric8.io\/\" target=\"_blank\" rel=\"noopener\">fabric8 maven plugin<\/a> and let us know what you think via the <a href=\"http:\/\/fabric8.io\/community\/\" target=\"_blank\" rel=\"noopener\">community<\/a> or the <a href=\"https:\/\/github.com\/fabric8io\/fabric8-maven-plugin\/issues\/\" target=\"_blank\" rel=\"noopener\">issue tracker<\/a>! Can you think of any way to make it even more awesome? If so <a href=\"https:\/\/github.com\/fabric8io\/fabric8-maven-plugin\/issues\/\" target=\"_blank\" rel=\"noopener\">let us know<\/a>!<\/p>\n<p>For a live introduction to Fabric8, attend my Devoxx US talk in San Jose, California on March 21. The talk is called <a href=\"http:\/\/cfp.devoxx.us\/2017\/talk\/CYO-6114\/develop_microservices_faster_with_an_open_source_platform_based_on_docker,_kubernetes_and_jenkins\" target=\"_blank\" rel=\"noopener\">develop microservices faster with an open source platform based on docker, kubernetes and jenkins&#8221;<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/www.eclipse.org\/community\/eclipse_newsletter\/2017\/january\/article2.php &nbsp; As Java developers we\u2019re often really busy with large backlogs, customer issues and countless disruptions. It can be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-169","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=169"}],"version-history":[{"count":0,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/169\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}