{"id":837,"date":"2020-03-30T08:00:00","date_gmt":"2020-03-30T08:00:00","guid":{"rendered":"http:\/\/bullyrooks.com\/index.php\/2020\/04\/02\/simple-spring-boot-service-to-kubernetes-application-step-8-3e94686c9a34\/"},"modified":"2021-02-04T01:52:27","modified_gmt":"2021-02-04T01:52:27","slug":"simple-spring-boot-service-to-kubernetes-application-step-8-3e94686c9a34","status":"publish","type":"post","link":"https:\/\/bullyrooks.com\/index.php\/2020\/03\/30\/simple-spring-boot-service-to-kubernetes-application-step-8-3e94686c9a34\/","title":{"rendered":"Containerize the Service With Docker"},"content":{"rendered":"\n<p class=\"graf graf--p graf-after--h3 graf--trailing\" id=\"df0b\">Now we\u2019re going to take our functional service and containerize it. This will allow us to control the deployment environment that our java spring boot service runs in as well as provide some additional security and isolation from application updates that can break our application.<\/p>\n\n\n\n<h3 class=\"graf graf--h3 graf--leading wp-block-heading\" id=\"90ec\">Containerization Overview<\/h3>\n\n\n\n<p class=\"graf graf--p graf-after--h3\" id=\"182c\">If you\u2019ve spent any amount of time in software development you may be familiar with a common scenario: you have an application that \u2018runs\u2019 in production, but it hasn\u2019t been touched in months (or years!). An audit by the operations or security team has determined that an operation system or library is out of date and needs to be upgraded. Of course, they do the upgrade without notifying the application developers and the update somehow breaks the functionality of the application. Now its your responsibility to update your application and since its a key component it is now a high priority. However, the developer that actually wrote the application is long gone and you have no idea what needs to be done to get it back up and running. Part of the reason that this happens is that your application is not just the java code, but is actually the whole stack that the software runs on but that stack is \u2018owned\u2019 by different parts of the organization.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/bullyrooks.com\/wp-content\/uploads\/2021\/02\/1HUvygiErhuMtOg0u1VSt_g.png?w=960\" alt=\"\" data-recalc-dims=\"1\"\/><\/figure>\n\n\n\n<p class=\"graf graf--p graf-after--figure\" id=\"9709\">Since these parts are easily upgraded and ownership is distributed, we can quickly encounter issues. But this is necessary because security is a priority for any application. Operations refers to the \u2018surface area\u2019 of security and in this application there is A LOT of surface area. There\u2019s the server that is deployed, the operating system that is running on that hardware, all of the libraries either installed with the operating system or afterward, the JDK as well as the application that was written. We need a solution that allows us to control the stack (for our functionality purposes) but also secure it (for our security needs).<\/p>\n\n\n\n<h4 class=\"graf graf--h4 graf-after--p wp-block-heading\" id=\"73d6\">Containers<\/h4>\n\n\n\n<p class=\"graf graf--p graf-after--h4\" id=\"d854\">Docker is a solution that allows development teams to \u2018own the stack\u2019 but also provides a high level of security in that containers can be made inaccessible once they are deployed. Additionally, if we\u2019re using a microservice architecture we can easily replace one container with another (possibly on an entirely different stack) so we can have a combination of secure \u2018legacy\u2019 services that do their job and are not prioritized for updates as well as new services or upgraded services. We don\u2019t have to do sweeping OS or JDK (or other library) updates across our organization anymore because the containers lock out any vulnerabilities from being exposed. Additionally, if we get to a point where a service is so far out of date that it would take more time analyzing it and refactoring it than it would be to drop it and write it from scratch we can do that too\u2026 but we can do it on our timeline.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/bullyrooks.com\/wp-content\/uploads\/2021\/02\/1HUvygiErhuMtOg0u1VSt_g.png?w=960\" alt=\"\" data-recalc-dims=\"1\"\/><\/figure>\n\n\n\n<p class=\"graf graf--p graf-after--figure\" id=\"0842\">Additionally, each container (containing the entire stack that is needed to run the application) can be versioned and managed in an artifact repository similarly to maven.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\" id=\"74bf\">These containers can be deployed in a container management system and the best way to secure them is to not allow access to them at all (via SSH, for example). If we\u2019re never going to be able to log into them, then the only way we\u2019re going to be able to deal with a container application that is failing is to spin up a new one and tear down the failing one. This means that our applications need to be stateless. They also need to expose all of their logs somehow (we\u2019ll get into that later). These may be major concerns for your application, so make sure that containerized applications fit your need.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p graf--trailing\" id=\"3e6d\">This is just a really high level overview of containers and containerization. There is much better, more detailed documentation out there and you should really familiarize yourself with the tools you\u2019ll be using.<\/p>\n\n\n\n<h3 class=\"graf graf--h3 graf--leading wp-block-heading\" id=\"9a6b\">Containerizing our&nbsp;Service<\/h3>\n\n\n\n<p class=\"graf graf--p graf-after--h3\" id=\"31fd\">All of these features seem great and thankfully taking advantage of them is very easy.<\/p>\n\n\n\n<h4 class=\"graf graf--h4 graf-after--p wp-block-heading\" id=\"7ad7\">Install Docker<\/h4>\n\n\n\n<p class=\"graf graf--p graf-after--h4\" id=\"4880\">Install the docker tool using the package manager appropriate for your operating system.<\/p>\n\n\n\n<ul class=\"postList wp-block-list\"><li id=\"7777\" class=\"graf graf--li graf-after--p\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/stefanscherer.github.io\/how-to-install-docker-the-chocolatey-way\/\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/stefanscherer.github.io\/how-to-install-docker-the-chocolatey-way\/\">Windows<\/a><\/li><li id=\"740e\" class=\"graf graf--li graf-after--li\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/docs.docker.com\/docker-for-mac\/\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/docs.docker.com\/docker-for-mac\/\">Mac<\/a><\/li><li id=\"a031\" class=\"graf graf--li graf-after--li\"><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/runnable.com\/docker\/install-docker-on-linux\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/runnable.com\/docker\/install-docker-on-linux\">Linux<\/a><\/li><\/ul>\n\n\n\n<h4 class=\"graf graf--h4 graf-after--li wp-block-heading\" id=\"ccc9\">Create a Dockerfile<\/h4>\n\n\n\n<p class=\"graf graf--p graf-after--h4\" id=\"aa28\">A dockerfile is going to specify the stack that is going to be created in the container. All we need is an OS and JDK so our dockerfile is very simple<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\" id=\"fee7\">Create a <code class=\"markup--code markup--p-code\">Dockerfile <\/code>in the root of your application.<\/p>\n\n\n\n<pre id=\"0790\" class=\"wp-block-code graf graf--pre graf-after--p\"><code>FROM openjdk:11.0.6-jdk-slim-buster\nARG <em class=\"markup--em markup--pre-em\">JAR_FILE<\/em>=target\/*.jar\nCOPY ${<em class=\"markup--em markup--pre-em\">JAR_FILE<\/em>} app.jar\nENTRYPOINT &#91;\"java\",\"-jar\",\"\/app.jar\"]<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\" id=\"d5c9\">We can now use docker to build the image<\/p>\n\n\n\n<pre id=\"e134\" class=\"wp-block-code graf graf--pre graf-after--p\"><code>docker build -t medium\/medium-customer<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\" id=\"f78e\">We should see something like this in the logs:<\/p>\n\n\n\n<pre id=\"09f3\" class=\"wp-block-code graf graf--pre graf-after--p\"><code>Successfully built 61c5fd6e8cc4\nSuccessfully tagged medium\/medium-customer:latest<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\" id=\"b0e9\">and we can confirm with docker (the image was build to our local docker repo, just like a <code class=\"markup--code markup--p-code\">mvn install<\/code> would build a jar to our&nbsp;.m2 artifact repository)<\/p>\n\n\n\n<pre id=\"6061\" class=\"wp-block-code graf graf--pre graf-after--p\"><code>docker image list<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\" id=\"6bcc\">should return<\/p>\n\n\n\n<pre id=\"49b0\" class=\"wp-block-code graf graf--pre graf-after--p\"><code>medium\/medium-customer      latest              7f9b379551d8        50 years ago        260MB<\/code><\/pre>\n\n\n\n<h3 class=\"graf graf--h3 graf-after--pre wp-block-heading\" id=\"b970\">Build and&nbsp;Commit<\/h3>\n\n\n\n<pre id=\"0764\" class=\"wp-block-code graf graf--pre graf-after--h3\"><code>git checkout -b docker\nmvn clean install\ngit add .\ngit commit -m \"dockerfile\"\ngit push --set-upstream origin jib\ngit checkout master\ngit merge jib\ngit push<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"entry-summary\">\nNow we\u2019re going to take our functional service and containerize it. This&hellip;\n<\/div>\n<div class=\"link-more\"><a href=\"https:\/\/bullyrooks.com\/index.php\/2020\/03\/30\/simple-spring-boot-service-to-kubernetes-application-step-8-3e94686c9a34\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &ldquo;Containerize the Service With Docker&rdquo;<\/span>&hellip;<\/a><\/div>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[5,74,29,50,42,43],"course":[40],"class_list":["post-837","post","type-post","status-publish","format-standard","hentry","category-software-development","tag-docker","tag-dockerfile","tag-git","tag-maven","tag-spring","tag-spring-boot","course-spring-with-kubernetes","entry"],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":833,"url":"https:\/\/bullyrooks.com\/index.php\/2020\/03\/30\/simple-spring-boot-service-to-kubernetes-application-step-18-9dff659cd334\/","url_meta":{"origin":837,"position":0},"title":"UI Build Pipeline","author":"Bullyrook","date":"March 30, 2020","format":false,"excerpt":"Simple Spring Boot Service to Kubernetes Application: Step\u00a018 Lets quickly update our build pipeline to push an image for our front end application into our repo. This opens the path to using helm to package and deploy our chart. Create a New\u00a0Pipeline Log into codefresh and go into pipelines. Choose\u2026","rel":"","context":"In &quot;Software Development&quot;","block_context":{"text":"Software Development","link":"https:\/\/bullyrooks.com\/index.php\/category\/software-development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":816,"url":"https:\/\/bullyrooks.com\/index.php\/2020\/03\/30\/simple-spring-boot-service-to-kubernetes-application-step-12-c6423261a93a\/","url_meta":{"origin":837,"position":1},"title":"Setting up a Kubernetes Cluster","author":"Bullyrook","date":"March 30, 2020","format":false,"excerpt":"Finally, we\u2019re going to be able to deploy our application. We need to get access to a cluster first. Install Tooling We\u2019re going to need more tools in order to get started. Use your OS package management tool to install these tools: Kubectl (Interact with a k8s instance)Minikube (Run a\u2026","rel":"","context":"In &quot;Software Development&quot;","block_context":{"text":"Software Development","link":"https:\/\/bullyrooks.com\/index.php\/category\/software-development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":826,"url":"https:\/\/bullyrooks.com\/index.php\/2020\/03\/30\/simple-spring-boot-service-to-kubernetes-application-step-9-55fd26c1dffb\/","url_meta":{"origin":837,"position":2},"title":"Docker Registry","author":"Bullyrook","date":"March 30, 2020","format":false,"excerpt":"In the previous article we created a docker image as part of our build process and stored it into our local repository. In order to deploy it in an automated fashion we should create a remote image repository in the cloud. This will expand the types of tools that we\u2026","rel":"","context":"In &quot;Software Development&quot;","block_context":{"text":"Software Development","link":"https:\/\/bullyrooks.com\/index.php\/category\/software-development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":824,"url":"https:\/\/bullyrooks.com\/index.php\/2020\/03\/30\/simple-spring-boot-service-to-kubernetes-application-step-10-e1a3a8e865bb\/","url_meta":{"origin":837,"position":3},"title":"Automated Build Pipeline","author":"Bullyrook","date":"March 30, 2020","format":false,"excerpt":"Lets set up automated build so that we can generate a docker image when a change hits master. Build the\u00a0Pipeline Go in to codefresh and choose Projects. Create a new project in the top right. Call it medium. Click on create pipeline. Call the pipeline medium-customer and choose our git\u2026","rel":"","context":"In &quot;Software Development&quot;","block_context":{"text":"Software Development","link":"https:\/\/bullyrooks.com\/index.php\/category\/software-development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":820,"url":"https:\/\/bullyrooks.com\/index.php\/2020\/03\/30\/simple-spring-boot-service-to-kubernetes-application-step-17-3867eb38d8f1\/","url_meta":{"origin":837,"position":4},"title":"Containerizing our UI","author":"Bullyrook","date":"March 30, 2020","format":false,"excerpt":"We\u2019ve got a simple page that calls one of our endpoints working. Let\u2019s go ahead an containerize it so that we can deploy it into our k8s environment in future steps. Build the Dockerfile I\u2019m following the process laid out here, with some minor differences. Create a Dockerfile # Stage\u2026","rel":"","context":"In &quot;Software Development&quot;","block_context":{"text":"Software Development","link":"https:\/\/bullyrooks.com\/index.php\/category\/software-development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":836,"url":"https:\/\/bullyrooks.com\/index.php\/2020\/03\/30\/simple-spring-boot-service-to-kubernetes-application-step-11-636b842a3c0f\/","url_meta":{"origin":837,"position":5},"title":"Helm for Deployment","author":"Bullyrook","date":"March 30, 2020","format":false,"excerpt":"We\u2019re about ready to deploy into kubernetes. However, deployment is not exactly straightforward. There are a lot of configuration files that we need to create and maintain in order to explain to the container management system how to deploy our application. We can use tools like kubectl to promote these\u2026","rel":"","context":"In &quot;Software Development&quot;","block_context":{"text":"Software Development","link":"https:\/\/bullyrooks.com\/index.php\/category\/software-development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/posts\/837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/comments?post=837"}],"version-history":[{"count":3,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/posts\/837\/revisions"}],"predecessor-version":[{"id":891,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/posts\/837\/revisions\/891"}],"wp:attachment":[{"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/media?parent=837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/categories?post=837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/tags?post=837"},{"taxonomy":"course","embeddable":true,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/course?post=837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}