And Todays’s Quote Would Be..

Life begins at the end of your comfort zone.

Neale Donald Walsch

Dockerfile for Creating A UBI-based Docker Image and OpenJDK

I use UBI 8 as base images for basically almost every java deployment. It’s lightweight, freely redistributable, and easy to configure.

This is my simplified Dockerfile script, which consist of UBI8 and OpenJDK installation.

FROM registry.access.redhat.com/ubi8/ubi-minimal

MAINTAINER Muhammad Edwin < emuhamma at redhat dot com >

# Some version information
LABEL io.k8s.description="Platform for running plain Java applications (fat-jar and flat classpath)" \
      io.k8s.display-name="My Simple Java Applications" \
      io.openshift.tags="builder,java" \
      org.jboss.deployments-dir="/deployments" \
      com.redhat.deployments-dir="/deployments" \
      com.redhat.dev-mode="JAVA_DEBUG:false" \
      com.redhat.dev-mode.port="JAVA_DEBUG_PORT:5005"

# Install Java runtime
RUN microdnf install java-11-openjdk-headless --nodocs \
 && microdnf install shadow-utils && microdnf clean all \
 && mkdir /deployments

# Add user & group
RUN groupadd jboss && useradd jboss -g jboss

# Use /dev/urandom to speed up startups & Add jboss user to the root group
RUN echo securerandom.source=file:/dev/urandom >> /usr/lib/jvm/jre/lib/security/java.security \
 && usermod -g root -G jboss jboss

# set working directory at /deployments
WORKDIR /deployments

# copy corresponding jar file
COPY demo.jar demo.jar

# Necessary to permit running with a randomised UID
RUN mkdir -p /deployments/data \
 && chmod -R "g+rwX" /deployments \
 && chown -R jboss:jboss /deployments \
 && chmod 664 /etc/passwd

# gives uid
USER 185

# run it
CMD ["java","-jar","demo.jar"]
Google+

Running A Simple Java Application CI/CD with Jenkins and Openshift

So basically im trying to create a simple CI/CD using Jenkins which runs on top of Openshift. It will do a very simple thing, fetching code from Github, and deploy it automatically to Openshift platform.

For this example, im using my previous Github repository which located at https://github.com/edwin/hello-world. It’s a very simple spring boot app, open an API and shows “hello world”.

But first, lets prepare our Jenkins instance on Openshift.

Once done, we can see Jenkins Dashboard.

And add Maven to Jenkins, on Manage jenkins > Global Tool Configuration

For this example, i want to deploy the app on a different Openshift project (eee project) compare to Jenkins which located on Fuse project. Therefore i need to create a service account specifially for Jenkins to deploy.

Create a simple pipeline item on Jenkins,

Which are triggered by a Poll SCM,

And after that, we can create a simple pipeline script for building the code. Changing project location to “eee”, and deploy it accordingly.

def gitRepo="https://github.com/edwin/hello-world.git"
def branch="master"

pipeline {
  agent any
  tools {
    maven 'M3'
  }
  stages {
    stage('Preparing'){
        steps{
            git branch: branch, url: gitRepo
        }
      }
    stage('Build and Deploy') {    
        steps {
            sh 'oc project eee'
            sh 'mvn -B clean fabric8:deploy'
        }
    }
  }
}

Simple isnt it? ;)

Google+

How to Deploy Source Code from Local Folder to Openshift Using S2I Build

First lets create a simple PHP code, and name it index.php

<?php
echo "hello world";
?>

We want to deploy it on top of PHP 7 images, and make it online on Openshift Platform.

So how to do it basically consist of 3 steps.
1. First we need to create a new binary build, using preferred image stream as its base image

oc new-build --name=my-php --image-stream=php:7.0 --binary=true

2. Next is start building image using sourcecode’s directory

oc start-build my-php --from-dir=.

3. Last is create application using previously built image

oc new-app my-php --name=my-php

So simple right :)

Google+

Backup All Openshift Template and Restore it to Another Openshift Instance

Previously i have to move a lot of Openshift template from one instance to another instance. So instead of copying a template one by one, why not just dump the whole template and import it to the new instance.

Below is the screenshot of source Openshift where we want to export the template from.

 
oc get -n openshift -o yaml --export templates > mytemplate.yaml

Below is the screenshot of the target Openshift,

oc apply -f mytemplate.yaml

This is the end result,

Google+

Creating A Simple Openshift Template

An Openshift Template is, as its name, a template on creating an application deployment. We can define url, imagestream location, service, and other things. We’ll start on a very simple json template which will took a specific image from docker hub, and deploy it to Openshift and generating URL on the fly automatically.

{
  "kind": "Template",
  "apiVersion": "v1",
  "metadata": {
    "name": "create-quarkus-template",
    "annotations": {
      "description": "This example shows how to create a simple template in openshift 3.11",
      "tags": "Quarkus"
    }
  },
  "objects": [
    {
      "kind": "Service",
      "apiVersion": "v1",
      "metadata": {
        "name": "my-quarkus"
      },
      "spec": {
        "ports": [
          {
            "name": "web",
            "protocol": "TCP",
            "port": 8080,
            "targetPort": 8080,
            "nodePort": 0
          }
        ],
        "selector": {
          "name": "my-quarkus"
        },
        "type": "ClusterIP",
        "sessionAffinity": "None"
      },
      "status": {
        "loadBalancer": {}
      }
    },
    {
        "kind": "Route",
        "apiVersion": "v1",
        "metadata": {
          "name": "my-quarkus"
        },
        "spec": {
          "to": {
            "kind": "Service",
            "name": "my-quarkus"
          },
          "tls": {
            "termination": "edge"
          }
        }
    },
    {
      "kind": "ImageStream",
      "apiVersion": "v1",
      "metadata": {
        "name": "my-quarkus"
      },
      "spec": {
        "dockerImageRepository": "edwinkun/my-quarkus"
      },
      "status": {
        "dockerImageRepository": ""
      }
    },
    {
        "kind": "DeploymentConfig",
        "apiVersion": "v1",
        "metadata": {
          "name": "my-quarkus",
          "annotations": {
            "template.alpha.openshift.io/wait-for-ready": "true"
          }
        },
        "spec": {
          "strategy": {
            "type": "Rolling",
            "rollingParams": {
              "updatePeriodSeconds": 1,
              "intervalSeconds": 1,
              "timeoutSeconds": 120,
              "pre": {
                "failurePolicy": "Abort",
                "execNewPod": {
                  "command": [
                    "/bin/true"
                  ],
                  "env": [
                  ],
                  "containerName": "my-quarkus"
                }
              },
              "post": {
                "failurePolicy": "Ignore",
                "execNewPod": {
                  "command": [
                    "/bin/true"
                  ],
                  "env": [
                  ],
                  "containerName": "my-quarkus"
                }
              }
            },
            "resources": {}
          },
          "triggers": [
            {
              "type": "ImageChange",
              "imageChangeParams": {
                "automatic": true,
                "containerNames": [
                  "my-quarkus"
                ],
                "from": {
                  "kind": "ImageStreamTag",
                  "name": "my-quarkus:latest"
                }
              }
            },
            {
              "type": "ConfigChange"
            }
          ],
          "replicas": 3,
          "selector": {
            "name": "my-quarkus"
          },
          "template": {
            "metadata": {
              "labels": {
                "name": "my-quarkus"
              }
            },
            "spec": {
              "containers": [
                {
                  "name": "my-quarkus",
                  "image": "my-quarkus",
                  "ports": [
                    {
                      "containerPort": 8080,
                      "protocol": "TCP"
                    }
                  ],
                  "env": [
                  ],
                  "resources": {},
                  "terminationMessagePath": "/dev/termination-log",
                  "imagePullPolicy": "IfNotPresent",
                  "securityContext": {
                    "capabilities": {},
                    "privileged": false
                  }
                }
              ],
              "restartPolicy": "Always",
              "dnsPolicy": "ClusterFirst"
            }
          }
        },
        "status": {}
      }
  ],
  "labels": {
    "template": "my-quarkus-dockerbuild"
  }
}
Google+