Deploying Java Applications in OpenShift

It would seem that this is so? We connect fabric8-maven-plugin to the project and go: building, running the application in OpenShift. But when I studied, I wanted more understanding of this process, and then I wanted more control and freedom over the process of building and deploying the application in OpenShift. Thus, the following scenario turned out with such features.

  • I build the artifact myself, with my tool (maven, gradle, etc.)
  • I control the creation of Docker Image through the Dockerfile
  • Build and Deployment process in Openshift is configured in the template, i.e. any container specifications, pod are customizable.
  • Thus, the process itself can be transferred to an external assembly, deployment system

Of course, for this I use the available features of OpenShift itself, which are plentiful.
And so I have a prepared java artifact, then I prepare a Dockerfile with the characteristics and functionality I need

Dockerfile

FROM openjdk

MAINTAINER Rylkov Alexander <arylkov@.>

COPY demo.springboot.mvn-0.0.1-SNAPSHOT.jar /deployments/app.jar

ENV JAVA_OPTS="$JAVA_OPTS -Xms500m -Xmx1024m"

EXPOSE 8080
EXPOSE 5005

ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar /deployments/app.jar" ]

Thus, there will be two files in the folder: the artifact and Dockerfile.



The composition of the files is conditional, if for example in Dockerfile additional files for docker image are needed, we add them here. Now, the basic java image and my Spring boot application are specified in the Dockerfile, two ports are open, etc.

At this step, we have the choice / freedom in the tool for building the application and assembling docker image.

Next, I prepare the OpenShift template for the application, this operation can be one-time, it only needs to be parameterized. Since it can contain everything possible to create, launch and operate the application, we will manage these processes through it. It will be quite simple for me and consist of the three most necessary configurations

  • Imagestream
  • Buildconfig
  • DeploymentConfig

Template java-app-sample
kind: Template
apiVersion: v1
metadata:
  labels:
    app: java-app-sample
  name: java-app-sample
  annotations:
    description: >-
      This example shows how to create a simple Java application in openshift
    tags: java
parameters:
  - name: APP_NAME
    description: application name
    value: app-sample
    required: true
objects:
  - kind: ImageStream
    apiVersion: v1
    metadata:
      name: "${APP_NAME}"
    spec:
      tags:
        - from:
            kind: DockerImage
            name: 172.30.1.1:5000/myproject/${APP_NAME}:latest
          name: latest
  - kind: BuildConfig
    apiVersion: v1
    metadata:
      name: "${APP_NAME}"
      labels:
        name: "${APP_NAME}"
    spec:
      source:
        binary: {}
        type: Binary
      strategy:
        type: Docker
      output:
        to:
          kind: DockerImage
          name: 172.30.1.1:5000/myproject/${APP_NAME}:latest
      postCommit: {}
      resources: {}
  - kind: DeploymentConfig
    apiVersion: v1
    metadata:
      name: "${APP_NAME}"
    spec:
      strategy:
        type: Rolling
        rollingParams:
          updatePeriodSeconds: 1
          intervalSeconds: 1
          timeoutSeconds: 120
        resources: {}
      replicas: 1
      selector:
        app: "${APP_NAME}"
      template:
        metadata:
          labels:
            app: "${APP_NAME}"
        spec:
          containers:
            - name: "${APP_NAME}"
              image: 172.30.1.1:5000/myproject/${APP_NAME}:latest
              ports:
                - containerPort: 8080
                  protocol: TCP
                - containerPort: 5005
                  protocol: TCP
              env:
                - name: TZ
                  value: Europe/Moscow
              resources:
                limits:
                  cpu: '0.5'
                  memory: 1000Mi
                requests:
                  cpu: '0.2'
                  memory: 500Mi
              imagePullPolicy: Always
          restartPolicy: Always
          dnsPolicy: ClusterFirst
    triggers:
      - type: ConfigChange
        imageChangeParams:
          automatic: true
          containerNames:
            - "${APP_NAME}"
          from:
            kind: ImageStreamTag
            name: 172.30.1.1:5000/myproject/${APP_NAME}:latest
      - type: ImageChange
    status: {}


In the template, some container parameters are indicated as an example, for example, resources / limits, TZ environment variable, one template parameter is specified - application name (APP_NAME). The registry docker address is specified for the minishift version (I use it locally for development 172.30.1.1/10000/myproject/) and others.

But for my scenario, the most important thing is indicated here

  - kind: BuildConfig
    spec:
      source:
        binary: {}
        type: Binary
      strategy:
        type: Docker

BuildConfig says that the source will be the binary file (s), and to manage the docker image preparation strategy - Dockerfile (type: Docker)

Let's create this template in OpenShift

 oc create -f template.yaml


This template will appear in the templates directory (it will be possible to organize the process from the web UI console, but not here).



Using this template, we’ll create an application (in fact, this is just a name and no more)

oc new-app java-app-sample -p APP_NAME=app-sample

specify the template name and the application name parameter



Three of my configurations have been created.

We start the process of assembly and deployment of the application in the container.

>oc start-build app-sample --from-dir . --follow

It is important to execute from the folder where the binary file and Dockerfile are specified, just the --from-dir parameter indicates that the entire folder will be prepared for loading in OpenShift for building and deploying image in the docker registry. There are other parameters of this command, for example from the archive.



We see that the contents of the folder were loaded into OpenShift, which launched the Docker process to prepare Image for the Dockerfile and finally placed the image in the docker registry.

The application started visible in the OpenShift web console.



The container parameters



specified in the configuration The environment variable specified in the configuration



In the log



Thus, without additional plug-ins, with maximum control and freedom of choice, we managed to place the application in OpenShift.

Materials

Installing Minishift
to learn about OpenShift, I'd recommend starting with Minishift

All Articles