Build DOCKER image using Openshift S2I

Source-To-Image (S2I), as the name implies, is responsible for transforming your application source into an executable Docker image that we can later run inside of OpenShift v3 or directly via `docker run`.

We can find the main project at github and bunch of sti templates for language specific builds.

https://github.com/openshift/sti-php
https://github.com/openshift/sti-ruby
https://github.com/openshift/sti-wildfly
https://github.com/openshift/sti-perl
https://github.com/openshift/sti-python
https://github.com/openshift/sti-nodejs

We need to get the s2i tool from github release and include in PATH.

[sudhaker@dell-cs24-n2 ~]$ wget https://github.com/openshift/source-to-image/releases/download/v1.0.5/source-to-image-v1.0.5-b731f95-linux-amd64.tar.gz
...
[sudhaker@dell-cs24-n2 ~]$ ll
total 5860
drwxrwxr-x. 2 sudhaker sudhaker    4096 Feb 26 21:04 bin
-rw-rw-r--. 1 sudhaker sudhaker 5992810 Feb 18 13:23 source-to-image-v1.0.5-b731f95-linux-amd64.tar.gz
[sudhaker@dell-cs24-n2 ~]$ cd bin; tar zxf ../source-to-image-*.tar.gz; rm ../source-to-image-*.tar.gz; cd -

And the proceed with cooking docker image from a source repository.

[sudhaker@dell-cs24-n2 ~]$ sudo docker images | grep -v openshift
REPOSITORY                                   TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
[sudhaker@dell-cs24-n2 ~]$ sudo ~/bin/s2i build git://github.com/sudhaker/my-node-app openshift/nodejs-010-centos7 my-node-app
I0228 19:19:01.037320 01056 clone.go:32] Downloading "git://github.com/sudhaker/my-node-app" ...
I0228 19:19:01.356601 01056 install.go:236] Using "assemble" installed from "image:///usr/libexec/s2i/assemble"
I0228 19:19:01.356684 01056 install.go:236] Using "run" installed from "image:///usr/libexec/s2i/run"
I0228 19:19:01.356740 01056 install.go:236] Using "save-artifacts" installed from "image:///usr/libexec/s2i/save-artifacts"
---> Installing application source
---> Building your Node application from source
E0228 19:19:05.417666 01056 util.go:91] npm info it worked if it ends with ok
E0228 19:19:05.417827 01056 util.go:91] npm info using npm@1.4.28
E0228 19:19:05.417915 01056 util.go:91] npm info using node@v0.10.40
E0228 19:19:05.608556 01056 util.go:91] npm info preinstall my-node-app@0.0.1
E0228 19:19:05.618591 01056 util.go:91] npm info build /opt/app-root/src
E0228 19:19:05.619139 01056 util.go:91] npm info linkStuff my-node-app@0.0.1
E0228 19:19:05.620359 01056 util.go:91] npm info install my-node-app@0.0.1
E0228 19:19:05.622702 01056 util.go:91] npm info postinstall my-node-app@0.0.1
E0228 19:19:05.623821 01056 util.go:91] npm info prepublish my-node-app@0.0.1
E0228 19:19:05.628719 01056 util.go:91] npm info ok
[sudhaker@dell-cs24-n2 bin]$ sudo docker images | grep -v openshift
REPOSITORY                                   TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
my-node-app                                  latest              85901d40c60b        55 seconds ago      438.7 MB

Let’s test this docker image

[sudhaker@dell-cs24-n2 ~]$ sudo docker run --detach --publish 8080:8080 my-node-app
92c707e8bedd4d08e5e9f2edc432b1febb700102b62dfb98349fd2217e5d342e
[sudhaker@dell-cs24-n2 ~]$ curl http://localhost:8080/
My Node App v-1.0 !! Server : 92c707e8bedd

Demo script for OpenShift V3

Quick demo script for OpenShift V3

# needed if running on all-in-one node
unset KUBECONFIG

oc login -u demo

oc new-project demo
oc project demo

#oc delete all --all

#ensure git is installed
git --version

oc new-app https://github.com/sudhaker/my-node-app.git -l app=my-node-app
watch oc status

oc expose service my-node-app -l name=my-node-app --hostname=my-node-app.apps.sudhaker.com

oc get pods
oc get svc
oc get route

# scale up pod
oc scale dc/my-node-app --replicas=2
oc scale dc/my-node-app --replicas=5
oc scale dc/my-node-app --replicas=2

# pod auto-recovery
oc get pods
oc delete pod #ID

# re-build
oc start-build bc/my-node-app
oc logs bc/my-node-app

# curl based test
while true; do echo -n "$(date) || "; curl http://my-node-app.apps.sudhaker.com/; sleep 0.2; done

### BlueGreen deployment

oc delete all --all

# deploy version 1.0
oc new-app "https://github.com/sudhaker/my-node-app.git#v1" --name=node-app-v1 -l app=my-node-app

oc expose service node-app-v1 --name=my-node-app -l name=my-node-app --hostname=my-node-app.apps.sudhaker.com

# curl based test
for i in {1..5}; do curl http://my-node-app.apps.sudhaker.com/; done

# deploy version 2.0
oc new-app "https://github.com/sudhaker/my-node-app.git#v2" --name=node-app-v2 -l app=my-node-app

oc edit route my-node-app

# curl based test
for i in {1..5}; do curl http://my-node-app.apps.sudhaker.com/; done

### AB deployment

oc delete route my-node-app
oc delete service node-app-v1
oc delete service node-app-v2

oc create -f ab-node-app-service.json

oc expose service ab-node-app -l app=my-node-app --hostname=ab-node-app.apps.sudhaker.com

oc scale dc/node-app-v1 --replicas=4

oc scale dc/node-app-v2 --replicas=1

# curl based test
for i in {1..12}; do curl http://ab-node-app.apps.sudhaker.com/; done


File: my-node-app-ab-service.json

{
    "kind": "Service",
    "apiVersion": "v1",
    "metadata": {
        "name": "ab-node-app",
        "namespace": "demo",
        "labels": {
            "app": "my-node-app"
        }
    },
    "spec": {
        "ports": [
            {
                "name": "8080-tcp",
                "protocol": "TCP",
                "port": 8080,
                "targetPort": 8080
            }
        ],
        "selector": {
            "app": "my-node-app"
        },
        "sessionAffinity": "None"
    }
}

# A colorful demo

oc new-app https://github.com/sudhaker/node_quotes.git -l app=nodequotes
oc expose service nodequotes -l name=nodequotes --hostname=nodequotes.apps.sudhaker.com



Install the latest OpenShift V3 on CentOS 7.x

Prerequisites: CentOS 7.x minimal install (tested on 7.2)

Updated 2016/06/05: to docker 1.11.x & OPENSHIFT_VERSION=v1.2.0

Mode: Single node setup, all manual.

Step 1: Install docker and tweak INSECURE_REGISTRY for smoother operation on “integrated docker registry”.

Fix DNS (as needed)

tee -a /etc/hosts << '__EOF__'
 
192.168.1.145 osv3-single
__EOF__

Continue reading Install the latest OpenShift V3 on CentOS 7.x