jjqquu / s2i-workshop

OpenShift template and S2I Workshop Content

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Source-to-Image Workshop Content

This document contains the code/configuration snippets corresponding to the workshop slides.

Pre Docker

Pre Docker - Step 1

#Power up your Vagrant VM and SSH to it
$ vagrant up

$ vagrant ssh

#Set the environment variables
[vagrant@rhel-cdk ~]$ cat <<EOF >> ~/.bashrc
export GOROOT=$HOME/go
export GOPATH=$HOME/work
export GOBIN=$HOME/work/bin
export PATH=$PATH:$HOME/work/src/github.com/openshift/source-to-image/_output/local/bin/linux/amd64/:$HOME/go/bin:$HOME/work/bin:$HOME/bzr-2.7.0
EOF

[vagrant@rhel-cdk ~]$ source ~/.bashrc

Pre Docker - Step 2

# Obtain golang tooling
[vagrant@rhel-cdk ~]$ curl -k -o go1.6.2.linux-amd64.tar.gz https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz

[vagrant@rhel-cdk ~]$ tar -xvf go1.6.2.linux-amd64.tar.gz

Pre Docker - Step 3

# Obtain Canonical Bazaar
[vagrant@rhel-cdk ~]$ curl -k -L -o bzr.tar.gz https://launchpad.net/bzr/2.7/2.7.0/+download/bzr-2.7.0.tar.gz

[vagrant@rhel-cdk ~]$ tar -xvf bzr.tar.gz

Pre Docker - Step 4

#Adjust date and time
[vagrant@rhel-cdk ~]$ date
Sun Jul 10 07:21:24 EDT 2016

#If this isn't right, we need to correct it
[vagrant@rhel-cdk ~]$ sudo timedatectl 2016-07-10

[vagrant@rhel-cdk ~]$ sudo timedatectl 07:22:30

Pre Docker - Step 5

#Download and build the gochat application
[vagrant@rhel-cdk ~]$ cd ~

[vagrant@rhel-cdk ~]$ go get -insecure github.com/rhtps/gochat

[vagrant@rhel-cdk ~]$ go install github.com/rhtps/gochat

Pre Docker - Step 6

[vagrant@rhel-cdk ~]$ gochat -host=0.0.0.0:8080 -callBackHost=http://10.1.2.2:8080 -templatePath=$GOPATH/src/github.com/rhtps/gochat/templates/ -avatarPath=$GOPATH/src/github.com/rhtps/gochat/avatars -htpasswdPath=$GOPATH/src/github.com/rhtps/gochat/htpasswd

Option 1 - Dockerfile

The following steps are performed in your Vagrant CDK VM.

Dockerfile - Step 1

[vagrant@rhel-cdk ~]$ cd ~

[vagrant@rhel-cdk ~]$ git clone https://github.com/rhtps/gochat-docker.git

[vagrant@rhel-cdk ~]$ less gochat-docker/Dockerfile

Dockerfile - Step 2

# Build the image
[vagrant@rhel-cdk ~]$ cd ~/gochat-docker

[vagrant@rhel-cdk gochat-docker]$ docker build -t gochat-docker .

Dockerfile - Step 3

# Start the gochat container
[vagrant@rhel-cdk ~]$ docker run -d -p 8080:8080 --name gochat gochat-docker -host=0.0.0.0:8080 -callBackHost=http://10.1.2.2:8080 -templatePath=/opt/gopath/src/github.com/rhtps/gochat/templates -avatarPath=/opt/gopath/src/github.com/rhtps/gochat/avatars -htpasswdPath=/opt/gopath/src/github.com/rhtps/gochat/htpasswd

Dockerfile - Step 4

[vagrant@rhel-cdk ~]$ docker stop gochat

[vagrant@rhel-cdk ~]$ docker rm gochat

Option 2 - Source-to-Image (S2I)

The following steps are performed in your Vagrant CDK VM.

S2I - Step 1

[vagrant@rhel-cdk ~]$ go get github.com/openshift/source-to-image

[vagrant@rhel-cdk ~]$ cd $GOPATH/src/github.com/openshift/source-to-image

[vagrant@rhel-cdk source-to-image]$ export PATH=$PATH:$GOPATH/src/github.com/openshift/source-to-image/_output/local/bin/linux/amd64/

[vagrant@rhel-cdk source-to-image]$ hack/build-go.sh

S2I - Step 2

[vagrant@rhel-cdk ~]$ cd ~

[vagrant@rhel-cdk ~]$ s2i create golang-s2i golang-s2i

[vagrant@rhel-cdk golang-s2i]$ tree -a golang-s2i
.
├── Dockerfile
├── Makefile
├── .s2i
│   └── bin
│       ├── assemble
│       ├── run
│       ├── save-artifacts
│       └── usage
└── test
    ├── run
    └── test-app

S2I - Step 3

[vagrant@rhel-cdk ~]$ cat /dev/null > ~/golang-s2i/Dockerfile

[vagrant@rhel-cdk ~]$ vi ~/golang-s2i/Dockerfile

And then paste the following Content

# golang-s2i
FROM rhel7

MAINTAINER Kenneth D. Evensen <kevensen@redhat.com>

ARG GO_VERSION=1.6.2
ENV BUILDER_VERSION 1.0
ENV HOME /opt/app-root
ENV GOPATH $HOME/gopath
ENV GOBIN $GOPATH/bin
ENV GOROOT $HOME/go
ENV PATH $PATH:$GOROOT/bin:$GOBIN

LABEL io.k8s.description="Platform for building go based programs" \
      io.k8s.display-name="gobuilder 0.0.1" \
      io.openshift.expose-services="8080:http" \
      io.openshift.tags="gobuilder,0.0.1" \
      io.openshift.s2i.scripts-url="image:///usr/local/s2i" \
      io.openshift.s2i.destination="/opt/app-root/destination"

RUN yum clean all && \
    yum install -y --disablerepo='*' --enablerepo='rhel-7-server-rpms' --enablerepo='rhel-7-server-optional-rpms' tar git-bzr && yum clean all && rm -rf /var/cache/yum/*

COPY ./.s2i/bin/ /usr/local/s2i
RUN useradd -u 1001 -r -g 0 -d ${HOME} -s /sbin/nologin -c "Default Application User" default && \
    mkdir -p /opt/app-root/destination/{src,artifacts} && \
    curl -o $HOME/go${GO_VERSION}.linux-amd64.tar.gz https://storage.googleapis.com/golang/go${GO_VERSION}.linux-amd64.tar.gz && \
    tar -C $HOME -xvf $HOME/go${GO_VERSION}.linux-amd64.tar.gz && \
    rm -f $HOME/go${GO_VERSION}.linux-amd64.tar.gz && \
    chown -R 1001:0 $HOME && chmod -R og+rwx ${HOME}

WORKDIR ${HOME}
USER 1001
EXPOSE 8080

S2I - Step 4

[vagrant@rhel-cdk ~]$ cat /dev/null > ~/golang-s2i/.s2i/bin/assemble

[vagrant@rhel-cdk ~]$ vi ~/golang-s2i/.s2i/bin/assemble

And ensure the file matches the following

#!/bin/bash
# S2I assemble script for the 'golang-s2i' image.
cd $HOME/destination/src
go get -insecure .
cd $GOBIN
mv src goexec

S2I - Step 5

[vagrant@rhel-cdk ~]$ cat /dev/null > ~/golang-s2i/.s2i/bin/run

[vagrant@rhel-cdk ~]$ vi ~/golang-s2i/.s2i/bin/run

And ensure the file matches the following

#!/bin/bash
exec $GOBIN/goexec $ARGS

S2I - Step 6

[vagrant@rhel-cdk ~]$ cat /dev/null > ~/golang-s2i/.s2i/bin/save-artifacts

[vagrant@rhel-cdk ~]$ vi ~/golang-s2i/.s2i/bin/save-artifacts

And ensure the file matches the following

#!/bin/bash

cd $GOPATH
tar cf - pkg bin src

S2I - Step 7

[vagrant@rhel-cdk ~]$ cat /dev/null > ~/golang-s2i/.s2i/bin/usage

[vagrant@rhel-cdk ~]$ vi ~/golang-s2i/.s2i/bin/usage

S2I - Step 8

[vagrant@rhel-cdk ~]$ cd ~/golang-s2i/

[vagrant@rhel-cdk golang-s2i]$ docker build -t golang-s2i .

S2I - Step 9

# s2i build <source location> <builder image> [<tag>] [flags]

[vagrant@rhel-cdk golang-s2i]$ s2i build https://github.com/rhtps/gochat.git golang-s2i gochat

S2I - Step 10

[vagrant@rhel-cdk golang-s2i]$ docker run -e "ARGS=-callBackHost=http://10.1.2.2:8080 -templatePath=/opt/app-root/gopath/src/github.com/rhtps/gochat/templates -avatarPath=/opt/app-root/gopath/src/github.com/rhtps/gochat/avatars -htpasswdPath=/opt/app-root/gopath/src/github.com/rhtps/gochat/htpasswd" -p 8080:8080 -d --name chat gochat

When you are done

[vagrant@rhel-cdk golang-s2i]$ docker stop chat

[vagrant@rhel-cdk golang-s2i]$ docker rm chat

Preparing for OpenShift

Template

apiVersion: v1
kind: Template
labels:
  template: golang
metadata:
  annotations:
    description: A basic builder for Golang applications
    iconClass: icon-go-gopher
    tags: golang
  creationTimestamp: null
  name: golang
objects:
  ...

ImageStreams

objects:
- apiVersion: v1
  kind: ImageStream
  metadata:
    annotations:
      description: Keeps track of changes in the application image
    name: golang-builder
- apiVersion: v1
  kind: ImageStream
  metadata:
    annotations:
      description: Keeps track of changes in the application image
    name: ${APPLICATION_NAME}

BuildConfiguration

objects:

- apiVersion: v1
  kind: BuildConfig
  metadata:
    annotations:
      description: Defines how to build the application
    name: golang-builder
  spec:
    output:
      to:
        kind: ImageStreamTag
        name: golang-builder:latest
    source:
      contextDir: ${BUILDER_CONTEXT_DIR}
      git:
        ref: ${BUILDER_SOURCE_REPOSITORY_REF}
        uri: ${BUILDER_SOURCE_REPOSITORY_URL}
      type: Git
    strategy:
      dockerStrategy:
      type: Docker
    triggers:
    - type: ConfigChange
    - github:
        secret: ${BUILDER_GITHUB_WEBHOOK_SECRET}
      type: GitHub
- apiVersion: v1
  kind: BuildConfig
  metadata:
    annotations:
      description: Defines how to build the application
    name: ${APPLICATION_NAME}
  spec:
    output:
      to:
        kind: ImageStreamTag
        name: ${APPLICATION_NAME}:latest
    source:
      contextDir: ${APP_CONTEXT_DIR}
      git:
        ref: ${APP_SOURCE_REPOSITORY_REF}
        uri: ${APP_SOURCE_REPOSITORY_URL}
      type: Git
    strategy:
      sourceStrategy:
        from:
          kind: ImageStreamTag
          name: golang-builder:latest
      type: Source
    triggers:
    - type: ImageChange
    - type: ConfigChange
    - github:
        secret: ${APP_GITHUB_WEBHOOK_SECRET}
      type: GitHub

DeploymentConfiguration

- apiVersion: v1
  kind: DeploymentConfig
  metadata:
    annotations:
      description: Defines how to deploy the application server
    name: ${APPLICATION_NAME}
  spec:
    replicas: 1
    selector:
      name: ${APPLICATION_NAME}
    strategy:
      type: Rolling
    template:
      metadata:
        labels:
          name: ${APPLICATION_NAME}
        name: ${APPLICATION_NAME}
      spec:
        containers:
        - env:
          - name: ARGS
            value: ${APP_ARGS}
          image: ${APPLICATION_NAME}
          name: ${APPLICATION_NAME}
          ports:
          - containerPort: 8080
    triggers:
    - imageChangeParams:
        automatic: true
        containerNames:
        - ${APPLICATION_NAME}
        from:
          kind: ImageStreamTag
          name: ${APPLICATION_NAME}:latest
      type: ImageChange
    - type: ConfigChange

Services

- apiVersion: v1
  kind: Service
  metadata:
    annotations:
      description: Exposes and load balances the application pods
    name: ${APPLICATION_NAME}
  spec:
    ports:
    - name: web
      port: 8080
      targetPort: 8080
    selector:
      name: ${APPLICATION_NAME}

Route

- apiVersion: v1
  id: ${APPLICATION_NAME}
  kind: Route
  metadata:
    annotations:
      description: Route for application's http service.
    labels:
      application: ${APPLICATION_NAME}
    name: ${APPLICATION_NAME}
  spec:
    host: ${APPLICATION_DOMAIN}
    to:
      name: ${APPLICATION_NAME}

Parameters

parameters:
- description: The URL of the repository with your Golang S2I builder Dockerfile
  name: BUILDER_SOURCE_REPOSITORY_URL
  value: https://github.com/rhtps/golang-s2i.git
- description: Set this to a branch name, tag or other ref of your repository if you
    are not using the default branch
  name: BUILDER_SOURCE_REPOSITORY_REF
- description: Set this to the relative path to your project if it is not in the root
    of your repository
  name: BUILDER_CONTEXT_DIR
- description: A secret string used to configure the GitHub webhook for the builder repo
  from: '[a-zA-Z0-9]{40}'
  generate: expression
  name: BUILDER_GITHUB_WEBHOOK_SECRET
- description: The URL of the repository with your Golang application code
  name: APP_SOURCE_REPOSITORY_URL
- description: Set this to a branch name, tag or other ref of your repository if you
    are not using the default branch
  name: APP_SOURCE_REPOSITORY_REF
- description: Set this to the relative path to your project if it is not in the root
    of your repository
  name: APP_CONTEXT_DIR
- description: A secret string used to configure the GitHub webhook for the app repo
  from: '[a-zA-Z0-9]{40}'
  generate: expression
  name: APP_GITHUB_WEBHOOK_SECRET
- description: The name for the application.
  name: APPLICATION_NAME
  required: true
  value: golang-app
- description: 'Custom hostname for service routes.  Leave blank for default hostname,
    e.g.: <application-name>.<project>.<default-domain-suffix>'
  name: APPLICATION_DOMAIN
- description: Command line arguments to provide to the Golang application
  name: APP_ARGS

Final

Let's create the template

[vagrant@rhel-cdk golang-s2i]$ cd openshift

[vagrant@rhel-cdk openshift]$ oc login -u openshift-dev

[vagrant@rhel-cdk openshift]$ oc new-project gochat

[vagrant@rhel-cdk openshift]$ oc create -f golang.yaml

Application arguments (APP_ARGS)

-host=0.0.0.0:8080 -callBackHost=10.1.2.2:8080 -templatePath=/opt/app-root/gopath/src/github.com/rhtps/gochat/templates -avatarPath=/opt/app-root/gopath/src/github.com/rhtps/gochat/avatars

About

OpenShift template and S2I Workshop Content