Building Java/Spring Apps in the Cloud

With Kubernetes Cloud Manager and Tekton Pipelines Article
Docs EInnovator Posted 14 Dec 20

Building Java/Spring Apps in the Cloud

Deploying apps to the cloud has never been so easy. Now, you can build them in the cloud too. Kubernetes provides you the runtime needed to deploy your apps in a uniform way, between your local development-test environment, a on-premises cluster, and any cloud provider of your choosing. Tekton is an extension to Kubernetes to make it possible to setup CI/CD pipelines that run inside a Kubernetes cluster. Dealing with Tekton configuration can be a bit challenging though, but fortunately the tool Cloud Manager can make running builds in a cluster as simple as deploying an application.

Cloud Manager allows you to setup a build pipeline for an application with minimal configuration. And once this setup is in place, a new build can be triggered with a single-click. In fact, you can even have the builds start automatically whenever a commit is done in a GIT repository. An option heralded as a best-practice of Continuous Integration/Continuous Delivery (CI/CD). If you are in a dev-test environment, you may also want to have the (re)deployment of the app to be done automatically after every successful build. If in production, you may prefer to have auto-deploy only for tagged commits and specific app versions.

In the following, I will guide you through some easy-quick steps required to setup a CI/CD pipeline for your application. I will use as example a Java/Spring app build with Maven, but similar steps can be worked out for other languages, stacks, and build tools, as well. I start with some background on CI/CD, Kubernetes, Tekton, and Cloud Manager, in case you are new to any of these topics and technologies. If you are already familiar with the essentials of it, you can skip the first two sections and jump directly to the practical hands-ons sections.

CI/CD Pipelines with Kubernetes Cloud Manager & Tekton

Why Continuous Delivery (CI/CD) ?

Being able to quick iterate and deploy new versions of applications is considered a key asset for the delivery of quality software and being able to respond quickly to market pressures or user demands. Be it an annoying bug that needs to be fixed, a critical security patch, a highly requested feature, or the implementation of a new business idea that you want to validate, all require you to be able to respond quickly and enact change. You will need to be able to build, test, bundle, and deploy updates with minimal fuss and do it frequently. There is nothing better for this, than having it be done automatically for you.

This is the motivation and principle behind Continuous Integration/Delivery (CI/CD) pipelines. The approach is to setup a well-defined sequence of steps — the pipeline — that can be run automatically and whose outcome is the production of the desired artifacts and/or execution of required actions. A common and useful type of pipeline involves the following steps: clone/pull source code from a GIT repository, run a build tool to produce binary artifacts, run unit and integration tests, bundle the binary artifacts as a new Docker image, push the create image to a registry, and possibly (re)deploy the app with new image.

About Tekton and Cloud Manager

Tekton is an extension to Kubernetes to allow pipelines (CI/CD or other) to be defined and executed in a cluster. Pipelines are defined from a set of reusable Tasks, and executed with the creation of an instance of a PipelineRun. Tekton runtime is easy to setup, but the configuration details required to execute the pipelines is somewhat overwhelming. You also need to make sure that you have the required Pipeline and Task definitions installed in our cluster, and manage the versions of that.

Cloud Manager is feature rich web UI for Kubernetes, designed from the ground-up to support all the workflows needed by developers when deploying cloud-native and microservice applications. It avoids the use of command-line tools like kubectl for most common tasks, and can be used ultimately to replace Kubernetes dashboard. While there other dashboard type tools, Cloud Manager is unique in its scope and focus on development. In particular, and for the case at hand, Cloud Manager provides simplified support for CI/CD pipelines with Tekton. Rather than requiring the writing/updating of a complex YAML manifest file to setup PipelineRuns and support resources, it only need you to provide minimal details in a convenient UI.

Cloud Manager also simplifies the setup of Kubernetes clusters to work with Tekton. Including installing the runtime, and installing Pipeline and Task definitions from Catalogs.

Starting Cloud Manager

Before jumping into CI/CD details, you need to setup a Kubernetes cluster and install Cloud Manager. This is pretty straightforward, and you have several options. If you have Docker Desktop installed in you laptop, and you enable the pseudo (one-node) local Kubernetes cluster, you are half done. You need next to start Cloud Manager, with the command:

docker run -p5005:2500 einnovator/einnovator-devops cm -d

This runs the latest version of Cloud Manager in a docker container named cm. Open the web browser in URL http://localhost:5005 to access the console and get started. You are asked to setup the admin account. Type admin as username, admin123 (or your favorite) as password, and type your email (case you forget the pass).

If you don’t have Docker and are not planning to install, but have already a Kubernetes cluster, you can deploy Cloud Manager with Helm. The details how to install Cloud Manager with Helm and different options for running with Docker have already been covered in other articles. So I delegate you to one of those other posts to keep this one shorter.

If you don’t have a cluster already, you can create one in a variety of cloud providers (e.g. DigitalOcean, Linode, and Scaleway, are good low-cost easy to setup options). Some providers also offer free trials (e.g. GCP, Azure, IBM, etc.).

With Cloud Manager in place, you need next to import your cluster. Click Add Cluster. For clusters hosted in cloud providers, you can import the cluster details from the provider by clicking Import. You need to grab and enter a Personal Access Tokens (or equivalent credentials) from the provider web console for that. For on-premises clusters, including the one-node pseudo-cluster in your laptop, you can get the cluster access information from the file: ~/.kube/config. The simplest way to do the setup is to click button Upload Config. Alternatively, you can manually copy&paste the relevant information from the config file. The relevant data is: cluster name, (master) server URL, CA certificated, and token or user certificate/key pair.

Setup Tekton Pipelines

With the cluster and Cloud Manager in place, you need to do just a couple more preliminary steps before setting up your app and run CI/CD builds:

  • Install the Tekton runtime. The simplest way to do this is to use the Cloud Manager UI. Click button CI/CD Runtime > Tekton Install found in the Cluster > Settings > Runtime tab of the cluster dashboard. If you want to install manually, check the install instructions of Tekton.

  • Create a Space (short for namespace), by pressing Cluster > Spaces > Add New in the cluster dashboard, or Spaces > Add New from the top menubar. Choose any name you want (e.g. dev or your project name). This is where you will deploy you app, and run builds on.

  • You need to install Task and Pipeline definitions suitable for building your type of application and selected build tool. Again, this can be done with the help of Cloud Manager or manually. Cloud Manager comes pre-configured with a marketplace Catalog containing several Tasks and Pipelines. You can install any of these by clicking CI/CD > Pipelines > Install and CI/CD > Tasks > Install in the Space dashboard.

If you are deploying a Java/Spring app build with Maven (from a pom.xml dependency file), you need to install at least the following:

  • Pipeline: JIB Maven
  • Tasks: GIT Clone and JIB Maven (required by the JIB Maven Pipeline)

If your Java/Spring app is built with Gradle you need to install:

  • Pipeline: JIB Gradle
  • Tasks: GIT Clone and JIB Gradle

If your deploying other type of applications, you need to check the Tekton task catalog for appropriate Tasks. We also keep updating Cloud Manager pre-defined Catalog with new Pipelines. With some luck, your type of app is already supported. If not, let us know and we will work on it.

Installing Tekton Pipelines with Cloud Manager

Setup the App

Once you are done with the once per cluster/namespace setup, now comes the good part — setting up your application. For illustration purposes, we assume in that text below that you want to deploy a Java/Spring Boot app named Superheros. An initial image is available in DockerHub public repository einnovator/einnovator-sample-superheros. The source code is available in the public GIT repository https://github.com/einnovator/einnovator-sample-superheros.

Start by creating a deployment for the app in the Space you created, by clicking on Deploy. Specify the following settings:

  • Image: einnovator/einnovator-sample-superheros
  • Name/Display Name: superheros
  • Default Instance Count: 1
  • Default Resources: 1Gb memory, 1Gb ephemeral storage
  • Create Service: Checked
  • Auto-Start: Checked
  • Create Route: Checked
  • Host: superheros

Click Save. Confirm that the app starts, by tracking that the Pod and Deployment status changes to Running (green) after a few seconds. You can also check the logs in tab Instances > Logs, and meta-data in tab Instances > MetaData.

The options Create Route and Host are used to auto-configure a DNS route fot the app. They only show up if you have created before hand at least one Domain, and its not required to setup a CI/CD pipeline which is the focus here. If you want to do this at this time, you can click Domains > Add New from the top toolbar. For secure HTTPS/TLS access you need to setup a certificate or get one free with the help of Cloud Manager. We covered details on how to do this in other article, so we skip the details here. You may also add DNS Routes after the app is deployed.

Creating a Deployment for Superheros App

Setup the Builds

You are now ready to set up a CI/CD pipeline for the app. To recap, the goal is to be able to build a new Docker Image for the app inside the Kubernetes cluster, and push it to a registry. Because, you don’t have write permissions on the currently set image repository: einnovator/einnovator-sample-superheros, you should create a new one.

Setup Repository in Registry

If have an account on DockerHub, or similar registry, press Create Repository and give it a name – say, myuser/superheros. You also need to grab an access token for authentication. Press Account Settings > Security > New Access Token and copy/save the token value.

Back to Cloud Manager, press Registries > Add New. Click on the DockerHub icon (or other, if you are using a different registry) to have the registry details automatically filled for the most part. You only need to enter your access credentials – username, email, and access token. Press Save.

Creating a Registry with Credentials for DockerHub

Configure CI/CD Pipeline

Go back to the deployment dashbord for superheros inside the Space you created. Click on tab CI/CD of the app dashboard. In the Options tab, click Add Repository. Type the URL of the GIT repository: https://github.com/einnovator/einnovator-sample-superheros. Click Save.

Next, enter the name of your new image repository: myuser/superheros and select the Registry you defined. As builder, select: jib-maven-pipeline. This is the Tekton pipeline that is used to build the app. For the Workspace Type select Volume Template with size 1Gi. This implies that a temporary volume (virtual disk) is created and used to download the GIT repository and run the build. Press Save, and you are done with setting up the pipeline. If you pre-created a volume and want to use it in stead, select option Volume for Workspace Type, and pick the name of the existing volume.

Running Builds Manually

From now on, you can start a build by pressing the Build toolbar button in the CI/CD panel. Press first time to try out. Confirm that the build is started, and keep pressing refresh to follow progress. When the build is completed, go back to the UI of DockerHub (or other registry you are using) to confirm that a new image for myuser/superheros was pushed.

CI/CD Settings for Superheros App

Private GIT Repositories

In a real project, you will be pulling source code from your own GIT repository — which is possibly private. To cover this case, press ViewRepo toolbar button in the CI/CD panel to go to the current GIT repository. In GitHub web console for the project, press Fork. Take notice of the URL of the new GIT repository, say: https://github.com/myuser/einnovator-sample-superheros, and update the repository URL in the CI/CD panel of the app.

Optionally, you can make the GIT repository private. If so, you need to create a GIT VCS in Cloud Manager with access credentials to clone the repository. In GitHub, press Settings > Developer Settings > Access Tokens and create a new access token. Back in Cloud Manager, press VCS > Add New and click on the GitHub icon to have most details filled up automatically. You only need to enter your access credentials – username, email, and access token. Press Save.

In the CI/CD panel, select the created VCS and press Save. Click Build again to start another build and confirm that the build is completed with success, and a new image is pushed.

Using Webhooks

Rather than having builds being started on demand whenever you click Build, you can use web-hooks to enable automatic builds at commit time.

Auto-Build on GIT Commit

On the CI/CD panel, enable option Webhooks and press Save. Copy the webhook URL. In the repository console on GitHub, click Settings > Webhooks > Add Webhook. Enter as Payload URL the URL of the app’s webhook you copied from the CI/CD panel. Select as Content-Type the value JSON. Optionally, if you entered or generate a Secret in the CI/CD panel, copy the secret and paste it to the corresponding field in GitHub.

To test the webhook, edit the README.md file with some dummy change directly on GitHub console and commit. Confirm that a new build is started at commit time.

TIP: For the webhook to work, you need to have Cloud Manager running in cluster with a public IP / reachable DNS hostname, or more generally reachable from the GIT provider you are using.

Auto-Deploy on GIT Commit

You may also want to have the app automatically re-deployed after a build is completed with success. If so, enable option Deploy After Build. Trigger a new build, and confirm that the app is redeployed after the build is completed.

Tagged Commits

You can also play around in the GitHub console with releases and tags. Create a new tag for the repository, say v1.1. Trigger a commit with that tag, and confirm the the created docker image after the build as the same tag: myuser/superheros:v1.1.

Wrapping Up: Going Microservices and Beyond

I have just scratched the surface of the cool and amazing things you can do with Kubernetes Cloud Manager, focusing on setting up CI/CD pipelines to build Docker images and webhooks. Hopefully, this should give you enough background, motivation, and practical tips to get immediate results. You should be able to have your apps being build and deployed, on demand or automatically, on any Kubernetes cluster you have access to.

What you have just done for one app, because it is so straightforward, can easily be repeated to any numbers of apps. If you are building a fancy microservices architecture with several apps, you want to minimize the setup required for each app. This includes setting up a CI/CD pipeline for each app, while keeping up with the best practices across the full architecture. Furthermore, being able to transition easily from local dev-test environment to a production cluster and the cloud, is also a must when you need to deal with many apps. Cloud Manager is a tool and “force-multiplier” that you and your team can rely on as you scale app complexity, while keeping devops work simple. Rather than dealing with the raw low-level details of Kubernetes and Tekton configuration, you can focus on business issues and bringing your architecture to the next level.

If you want to learn about more tips and tricks on Kubernetes, devops, microservices development, Cloud Manager, and other topics, check out our other posts and subscribe on Medium, or Twitter, if you want to stay updated.

Learning More

Comments and Discussion