2016年12月20日星期二

OpenShift_018:如何重头定制一个 builder image?

环境:OSE 3.0.1

本文介绍如何重头做一个 builder image,当然这个重头也不是完全重头开始,至少你要有一个 linux image,该 linux image 一般官网都会提供。
比如 cenos7 image 在 https://hub.docker.com;rhel7 image 在 http://registry.access.redhat.com (需要有红帽订阅)。
另外,在 https://github.com/openshift/sti-base 提供了一些 OpenShift base image。

第1步,根据 rhel7 image 制作一个 base-rhel7 image。base-rhel7 image 将作为 OpenShift 3 中所有 builder image 的 base image。

第2步,根据 base-rhel7 image 制作一个 apache httpd-s2i builder image。httpd-s2i builder image 将用于发布和运行静态 html 页面。

1. 制作 base-rhel7 image
(1)git 克隆 sti-base,感觉这个和 https://github.com/openshift/sti-base 是一个项目
[student@workstation ~]$ git clone http://workstation.pod0.example.com/sti-base.git
(2)下载 rhel7 image
[student@workstation ~]$ sudo docker pull workstation.pod0.example.com:5000/rhel7
(3)build base-rhel7 image

[student@workstation ~]$ cd sti-base
[student@workstation sti-base]$ sudo docker build -t openshift/base-rhel7 -f Dockerfile.rhel7 .
Dockerfile.rhel7 内容如下:
FROM rhel7
#FROM workstation.pod0.example.com:5000/rhel7

# This image is the base image for all OpenShift v3 language Docker images.
# Location of the STI scripts inside the image
LABEL io.openshift.s2i.scripts-url=image:///usr/local/sti

# DEPRECATED: This label will be kept here for backward compatibility
LABEL io.s2i.scripts-url=image:///usr/local/sti

# Deprecated. Use above LABEL instead, because this will be removed in future versions.
ENV STI_SCRIPTS_URL=image:///usr/local/sti

# Labels consumed by Red Hat build service
LABEL BZComponent="openshift-sti-base-docker" \
      Name="openshift3/sti-base" \
      Version="1.0" \
      Release="1" \
      Architecture="x86_64"

# The $HOME is not set by default, but some applications needs this variable
# TODO: There is a bug in rhel7.1 image where the PATH variable is not exported
# properly as Docker image metadata, which causes the $PATH variable do not
# expand properly.
ENV HOME=/opt/app-root/src \
    PATH=/opt/app-root/src/bin:/opt/app-root/bin:/usr/local/sti:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# When bash is started non-interactively, to run a shell script, for example it
# looks for this variable and source the content of this file. This will enable
# the SCL for all scripts without need to do 'scl enable'.
ADD contrib/scl_enable /opt/app-root/etc/scl_enable
ENV BASH_ENV=/opt/app-root/etc/scl_enable \
    ENV=/opt/app-root/etc/scl_enable \
    PROMPT_COMMAND=". /opt/app-root/etc/scl_enable"

# This is the list of basic dependencies that all language Docker image can
# consume.
# Also setup the 'openshift' user that is used for the build execution and for the
# application runtime execution.
# TODO: Use better UID and GID values
#RUN yum-config-manager --enable rhel-7-server-optional-rpms && \
ADD training.repo /etc/yum.repos.d/training.repo
RUN yum install -y --setopt=tsflags=nodocs --skip-broken \
  autoconf \
  automake \
  bsdtar \
  findutils \
  gcc-c++ \
  gdb \
  gettext \
  git \
  libcurl-devel \
  libxml2-devel \
  libxslt-devel \
  lsof \
  make \
  mariadb-devel \
  mariadb-libs \
  openssl-devel \
  patch \
  postgresql-devel \
  procps-ng \
  scl-utils \
  sqlite-devel \
  tar \
  unzip \
  wget \
  which \
  yum-utils \
  zlib-devel && \
  yum clean all -y && \
  mkdir -p ${HOME} && \
  useradd -u 1001 -r -g 0 -d ${HOME} -s /sbin/nologin \
      -c "Default Application User" default && \
  chown -R 1001:0 /opt/app-root

# Create directory where the image STI scripts will be located
# Install the base-usage script with base image usage informations
ADD bin/base-usage /usr/local/sti/base-usage

# Use entrypoint so path is correctly adjusted already at the time the command
# is searching, so something like docker run IMG python runs binary from SCL
ADD bin/container-entrypoint /usr/bin/container-entrypoint

# Directory with the sources is set as the working directory so all STI scripts
# can execute relative to this path
WORKDIR ${HOME}

ENTRYPOINT ["container-entrypoint"]
CMD ["base-usage"]

2. 制作 httpd-s2i builder image
(1)安装 s2i 工具
s2i 工具不在 RHEL7 或 OpenShift 3 安装包中,需要单独安装。
[student@workstation ~]$ s2i version
(2)创建 httpd-s2i 项目
[student@workstation ~]$ s2i create httpd-s2i httpd-s2i
第 1 个 httpd-s2i 是 image 的名称, 第 2 个 httpd-s2i 是 project 的名称。
该命令执行后,会生成一个目录框架,其中包括 Dokerfile 以及 .sti/bin/run, .sti/bin/assemble 等文件。
你可以在此基础上,再做修改。
(3)添加必要的文件及相关内容
[student@workstation ~]$ cd httpd-s2i
  •  httpd-s2i/Dockerfile 内容如下:
FROM openshift/base-rhel7

EXPOSE 8080

# Install Apache httpd from the Red Hat SCL
#RUN yum-config-manager --enable rhel-7-server-optional-rpms && \
ADD training.repo /etc/yum.repos.d/training.repo
RUN yum install -y --setopt=tsflags=nodocs httpd24-httpd && \
    yum clean all -y

LABEL io.k8s.description="Platform for building and running httpd applications" \
      io.k8s.display-name="Apache 2.4" \
      io.openshift.expose-services="8080:http" \
      io.openshift.tags="builder,httpd"

# Copy the S2I scripts from the specific language image to /usr/local/sti
COPY ./.s2i/bin/ /usr/local/sti

# Each language image can have 'contrib' a directory with extra files needed to
# run and build the applications.
COPY ./contrib/ /opt/app-root

# In order to drop the root user, we have to make some directories world
# writeable as OpenShift default security model is to run the container under
# random UID.
RUN sed -i -f /opt/app-root/etc/httpdconf.sed /opt/rh/httpd24/root/etc/httpd/conf/httpd.conf && \
    head -n151 /opt/rh/httpd24/root/etc/httpd/conf/httpd.conf | tail -n1 | grep "AllowOverride All" || exit && \
    chmod -R a+rwx /opt/rh/httpd24/root/var/run/httpd && \
    chown -R 1001:1001 /opt/app-root

USER 1001

# Set the default CMD to print the usage of the language image
CMD ["usage"]
  •    httpd-s2i/training.repo 内容如下:
[rhel-7-server-rpms]
name=Rhel 7 rpms
baseurl=http://content.example.com/ose3.0/x86_64/dvd/rhel-7-server-rpms
enabled=1
gpgcheck=0

[rhel-7-server-extras-rpms]
name=Rhel 7 extras
baseurl=http://content.example.com/ose3.0/x86_64/dvd/rhel-7-server-extras-rpms
enabled=1
gpgcheck=0

[rhel-7-server-optional-rpms]
name=Rhel 7 optional
baseurl=http://content.example.com/ose3.0/x86_64/dvd/rhel-7-server-optional-rpms
enabled=1
gpgcheck=0

[rhel-7-server-ose-3.0-rpms]
name=OpenShift
baseurl=http://content.example.com/ose3.0/x86_64/dvd/rhel-7-server-ose-3.0-rpms
enabled=1
gpgcheck=0

[rhel-server-rhscl-7-rpms]
name=Software Collections
baseurl=http://content.example.com/ose3.0/x86_64/rhscl/rhel-server-rhscl-7-rpms
enabled=1
gpgcheck=0
  •  httpd-s2i/contrib/etc/scl_enable 内容如下:
# IMPORTANT: Do not add more content to this file unless you know what you are
#            doing. This file is sourced everytime the shell session is opened.
#
# This will make scl collection binaries work out of box.
unset BASH_ENV PROMPT_COMMAND ENV
source scl_source enable httpd24
  •   httpd-s2i/contrib/etc/httpdconf.sed 内容如下:
s/^Listen 80/Listen 0.0.0.0:8080/
s/^User apache/User default/
s/^Group apache/Group root/
s%^DocumentRoot "/opt/rh/httpd24/root/var/www/html"%DocumentRoot "/opt/app-root/src"%
s%^s%^s%^ErrorLog "logs/error_log"%ErrorLog "/tmp/error_log"%
s%CustomLog "logs/access_log"%CustomLog "/tmp/access_log"%
151s%AllowOverride None%AllowOverride All%

  •   httpd-s2i/.sti/bin/assemble 内容如下:
#!/bin/bash -e
#
# S2I assemble script for the 'httpd-s2i' image.
# The 'assemble' script builds your application source ready to run.
#
# For more information refer to the documentation:
#    https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#

if [ "$1" = "-h" ]; then
    # If the 'httpd-s2i' assemble script is executed with '-h' flag,
    # print the usage.
    exec /usr/local/s2i/usage
fi

# Restore artifacts from the previous build (if they exist).
#
if [ "$(ls /tmp/artifacts/ 2>/dev/null)" ]; then
  echo "---> Restoring build artifacts"
  mv /tmp/artifacts/. ./
fi

echo "---> Installing application source"
cp -Rf /tmp/src/. ./

echo "---> Building application from source"
# TODO: Add build steps for your application, eg npm install, bundle install
  •   httpd-s2i/.sti/bin/run 内容如下:
#!/bin/bash -e
#
# S2I run script for the 'httpd-sti' image.
# The run script executes the server that runs your application.
#
# For more information see the documentation:
#    https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#

exec httpd -D FOREGROUND
  •   httpd-s2i/test/test-app/index.html 内容如下:
<html>
<head>
   <title>HTML Home Page</title>
</head>
<body>
<h1>This is the home page</h1>
</body>
</html>

(4)build httpd-s2i image
[student@workstation httpd-s2i]$ sudo docker build -t httpd-s2i .

(5)测试 httpd-s2i image
[student@workstation httpd-s2i]$ cd test/test-app
[student@workstation test-app]$ git init .
[student@workstation test-app]$ git add .
[student@workstation test-app]$ git commit -m "test HTML application"

[student@workstation httpd-s2i]$ sudo s2i build file:///home/student/httpd-s2i/test/test-app/ httpd-s2i:latest httpd-s2i-test
httpd-s2i:latest 是 builder image; httpd-s2i-test 是要生成的应用 image;file:///home/student/httpd-s2i/test/test-app/ 是源代码根目录。

[student@workstation httpd-s2i]$ sudo docker run -p 8080:8080 httpd-s2i-test

新开一个终端,访问
[student@workstation ~]$ curl http://127.0.0.1:8080

(6)导出 httpd-s2i image
[student@workstation ~]$ sudo docker save -o httpd-s2i.tar httpd-s2i
[student@workstation ~]$ gzip -v httpd-s2i.tar

至此,终于得到了  httpd-s2i.tar.gz,现在你可以把它发布了。
如何发布定制的 builder image,请参考《 OpenShift_016:如何发布定制的 builder image ?

没有评论: