9.2.2 Binary Deployment

Building images from binary using Binary Build.

9.2.2.1 Lab

Uses cases of binary builds

  • Build and test code local
  • Bypass the SCM
  • Build images with artifacts from different sources

We first check that the project is ready for the lab.

Ensure that the LAB_USER environment variable is set.

echo $LAB_USER

If the result is empty, set the LAB_USER environment variable.

command hint
export LAB_USER=<username>

Change to your main Project.

command hint
oc project $LAB_USER-build-types

Task 9.2.2.2: Binary Build

BuildConfig

Let’s create the resources for our binary deployment. We start with the ImageStreams. There are two definitions, the first one represents our builder image. The second ImageStream is used for our build binary deployment.

apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
  labels:
    app: quarkus-techlab-data-producer-bb
  name: quarkus-techlab-data-producer-bb
spec:
  lookupPolicy:
    local: false
---
apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
  labels:
    app: quarkus-techlab-data-producer-bb
  name: ubi-minimal
spec:
  lookupPolicy:
    local: false
  tags:
  - annotations:
      openshift.io/imported-from: registry.access.redhat.com/ubi8/ubi-minimal
    from:
      kind: DockerImage
      name: registry.access.redhat.com/ubi8/ubi-minimal
    generation: 2
    importPolicy: {}
    name: latest
    referencePolicy:
      type: Source

Source

oc create -f https://raw.githubusercontent.com/puzzle/amm-techlab/main/manifests/additional/binary/imageStreams.yaml

Afterwards we can create the Build Config for the binary deployment.

apiVersion: build.openshift.io/v1
kind: BuildConfig
metadata:
  annotations:
    openshift.io/generated-by: OpenShiftNewBuild
  labels:
    build: quarkus-techlab-data-producer-bb
  name: quarkus-techlab-data-producer-bb
spec:
  output:
    to:
      kind: ImageStreamTag
      name: quarkus-techlab-data-producer-bb:latest
  postCommit: {}
  resources: {}
  source:
    binary: {}
    type: Binary
  strategy:
    type: Docker
  triggers:
  - github:
      secret: u7kQquuC1Hpap8pv82Xz
    type: GitHub
  - generic:
      secret: MduzcwKRw37WrDWWSfCf
    type: Generic

Source

oc create -f https://raw.githubusercontent.com/puzzle/amm-techlab/main/manifests/additional/binary/buildConfig.yaml

The next step is to prepare our binary. We’re going to use a prebiuld quarkus binary from the data producer REST version.

mkdir bin
cd bin
wget 'https://github.com/puzzle/quarkus-techlab-data-producer/releases/download/1.1.0-rest/application'

Next we need to create a Dockerfile. This is necessary because there exists no prebuilt s2i image for binary applications. Create a new file called Dockerfile and paste the following content.

FROM registry.access.redhat.com/ubi8/ubi-minimal
WORKDIR /work/
COPY application .

# set up permissions for user `1001`
RUN chmod 775 /work /work/application \
  && chown -R 1001 /work \
  && chmod -R "g+rwX" /work \
  && chown -R 1001:root /work

EXPOSE 8080
USER 1001

CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]

Source

Now we can start our build with following command:

oc start-build quarkus-techlab-data-producer-bb --from-dir=. --follow

This command triggers a build from the current directory which contains the binary and the Dockerfile.

Create additional resources

Until now we just created the build resources. Up next is the creation of the DeploymentConfig, Service and the Route.

DeploymentConfig

apiVersion: template.openshift.io/v1
kind: Template
metadata:
  name: deploymentconfig-bb-template
objects:
- apiVersion: apps.openshift.io/v1
  kind: DeploymentConfig
  metadata:
    labels:
      app: quarkus-techlab-data-producer-bb
    name: quarkus-techlab-data-producer-bb
  spec:
    replicas: 1
    selector:
      deploymentconfig: quarkus-techlab-data-producer-bb
    strategy:
      resources: {}
    template:
      metadata:
        labels:
          deploymentconfig: quarkus-techlab-data-producer-bb
      spec:
        containers:
        - image: image-registry.openshift-image-registry.svc:5000/${PROJECT_NAME}/quarkus-techlab-data-producer-bb:latest
          name: quarkus-techlab-data-producer-bb
          ports:
          - containerPort: 8080
            protocol: TCP
          - containerPort: 8443
            protocol: TCP
          - containerPort: 8778
            protocol: TCP
          resources: {}
    test: false
    triggers:
    - type: ConfigChange
    - imageChangeParams:
        automatic: true
        containerNames:
        - quarkus-techlab-data-producer-bb
        from:
          kind: ImageStreamTag
          name: quarkus-techlab-data-producer-bb:latest
      type: ImageChange
parameters:
- description: OpenShift Project Name
  name: PROJECT_NAME
  required: true

source

oc process -f https://raw.githubusercontent.com/puzzle/amm-techlab/main/manifests/additional/binary/deploymentConfig.yaml -p PROJECT_NAME=$PROJECT_NAME | oc apply -f -

Service

apiVersion: v1
kind: Service
metadata:
  labels:
    app: quarkus-techlab-data-producer-bb
  name: quarkus-techlab-data-producer-bb
spec:
  ports:
  - name: http
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    deploymentconfig: quarkus-techlab-data-producer-bb
  sessionAffinity: None
  type: ClusterIP

source

oc create -f https://raw.githubusercontent.com/puzzle/amm-techlab/main/manifests/additional/binary/service.yaml

Route

apiVersion: v1
kind: Template
metadata:
  name: route-bb-template
objects:
- apiVersion: route.openshift.io/v1
  kind: Route
  metadata:
    labels:
      app: quarkus-techlab-data-producer-bb
    name: quarkus-techlab-data-producer-bb
  spec:
    host: ${HOSTNAME}
    port:
      targetPort: http
    tls:
      termination: edge
    to:
      kind: Service
      name: quarkus-techlab-data-producer-bb
      weight: 100
    wildcardPolicy: None
parameters:
- description: The public Hostname quarkus-techlab-data-producer-bb-${HOSTNAME}.amm-techlab.openshift.ch
  name: HOSTNAME
  mandatory: true

source

Then we can create the route

oc process -f https://raw.githubusercontent.com/puzzle/amm-techlab/main/manifests/additional/binary/route.yaml -p HOSTNAME=quarkus-techlab-data-producer-bb-$LAB_USER.techlab.openshift.ch | oc apply -f -

Check if the route was created successfully

oc get route quarkus-techlab-data-producer-bb
NAME              HOST/PORT                                          PATH   SERVICES          PORT       TERMINATION   WILDCARD
quarkus-techlab-data-producer-bb   quarkus-techlab-data-producer-bb-<username>.techlab.openshift.ch          quarkus-techlab-data-producer-bb   8080-tcp   edge          None

And finally check if you can reach your application within a browser by accessing the public route. https://quarkus-techlab-data-producer-bb-<username>.techlab.openshift.ch/data