您的位置:首页 > 其它

一文带你了解s2i的原理使用,快速构建镜像

2020-06-09 04:34 302 查看

源到镜像(S2I)是一个独立工具,在创建构建器镜像时非常有用。S2I是OpenShift 3中用于构建应用程序的主要策略。以下特性可能是您感兴趣:

速度 -使用S2I,组装过程可以执行大量复杂的操作,而无需在每个步骤中创建新的层,从而实现了快速的镜像构建过程。
可修补性 -如果基础映像由于安全性问题需要修补,则S2I允许您一致地重建应用程序。
用户效率 -S2I禁止开发人员在其应用程序构建期间执行任意的yum安装类型操作,这会导致开发迭代缓慢。
生态系统 -S2I鼓励共享镜像生态系统,您可以在其中利用最佳实践应用程序。

本文是关于创建一个简单的S2I构建器镜像的。

S2I使用源代码和builder image (构建器镜像)生成一个新的Docker映像。S2I项目包含许多常用的builder image 镜像,例如Python或Ruby,您也可以使用自己的自定义脚本扩展S2I。

在详细介绍如何创建构建器镜像之前,让我们解释一下S2I的工作原理以及构建器镜像在构建过程中的作用。顾名思义,Source-To-Image负责将您的应用程序源转换为可执行的Dockerimage,您可以稍后在OpenShift 3内部运行或直接通过docker run。builder image 包含生成该可执行最终镜像所需的特定脚本依赖(也称为构建工件)。整个构建过程涉及几个步骤,包括调用构建器。在本文中,我们将重点介绍最简单的工作流程:

  1. 下载S2I脚本(或从内部构建器镜像中使用该脚本)。
  2. 下载应用程序源代码。
  3. 然后,S2I将脚本和应用程序源代码流式传输到构建器映像容器中。
  4. 然后,它将运行在构建器镜像中定义的脚本。
  5. 保存最终镜像。

要完成所有这些工作,构建器镜像中需要一些内容。首先,由于构建器镜像负责实际构建应用程序,因此它必须包含构建和运行应用程序所需的所有必需的库和工具。例如,Tomcat构建器映像将安装Tomcat,JDK和Maven,而Python构建器映像可能具有Gunicorn或Cherry.py,SciPy二进制文件和pip。其次,它需要包含脚本逻辑以实际执行构建和运行操作。这部分由两个必需的S2I脚本处理:

  1. assemble -负责构建应用程序
  2. run -负责运行应用程序。

在接下来的步骤中,我将展示如何使用提供静态html文件的Lighttpd服务器,并结合示例docker镜像 构建构建器镜像。有一个随附的github仓库,其中包含在每个步骤中创建的所有文件。

首先,从https://github.com/openshift/source-to-image/releases/获取最新的S2I二进制文件开始我们的旅程(本文使用的是1.0.9版)。

步骤1
S2I附带了一个方便的命令,该命令为构建器镜像引导所需的目录结构。让我们用它来创建所有必要的依赖构件:

s2i create lighttpd-centos7 s2i-lighttpd

我调用了s2i create传递构建器镜像的将来名称(lighttpd-centos7)和应在其中创建构件的目录(s2i-lighttpd)。如果目录不存在,它将为您创建。运行上述命令的结果是以下目录结构:

s2i-lighttpd/

  • Dockerfile -这是一个标准的Dockerfile,我们将在其中定义构建器映像
  • Makefile -用于构建和测试构建器映像的帮助脚本
  • test/
    run -测试脚本,测试构建器映像是否正常工作
    test-app/ -测试应用程序的目录
  • .s2i/bin/
    assemble -负责构建应用程序的脚本
    run -负责运行应用程序的脚本
    save-artifacts -负责增量构建的脚本,将在以后的文章中介绍
    usage -负责打印构建器图像用法的脚本

第2步
首先,通过修改Dockerfile来定义构建器镜像的分层结构:

# We are basing our builder image on openshift base-centos7 image

FROM openshift/base-centos7
# Inform users who's the maintainer of this builder image
MAINTAINER Maciej Szulik <maszulik@redhat.com>

# Inform about software versions being used inside the builder
ENV LIGHTTPD_VERSION=1.4.35

# Set labels used in OpenShift to describe the builder images
LABEL io.k8s.description="Platform for serving static HTML files" \
io.k8s.display-name="Lighttpd 1.4.35" \
io.openshift.expose-services="8080:http" \
io.openshift.tags="builder,html,lighttpd"

# Install the required software, namely Lighttpd and
RUN yum install -y lighttpd && \
# clean yum cache files, as they are not needed and will only make the image bigger in the end
yum clean all -y

# Defines the location of the S2I
# Although this is defined in openshift/base-centos7 image it's repeated here
# to make it clear why the following COPY operation is happening
LABEL io.openshift.s2i.scripts-url=image:///usr/local/s2i
# Copy the S2I scripts from ./.s2i/bin/ to /usr/local/s2i when making the builder image
COPY ./.s2i/bin/ /usr/local/s2i

# Copy the lighttpd configuration file
COPY ./etc/ /opt/app-root/etc

# Drop the root user and make the content of /opt/app-root owned by user 1001
RUN chown -R 1001:1001 /opt/app-root

# Set the default user for the image, the user itself was created in the base image
USER 1001

# Specify the ports the final image will expose
EXPOSE 8080

# Set the default CMD to print the usage of the image, if somebody does docker run
CMD ["usage"]

第3步
一旦Dockerfile定义了,我们现在就可以开始填写构建器镜像的其余部分。让我们处理S2I脚本。我们从开始assemble,它负责构建应用程序。在我们的例子中,它将只是将源代码文件复制到Lighttpd服务器的代码文件存放目录中:

#!/bin/bash -e

#

# S2I assemble script for the 'lighttpd-centos7' 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

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

默认情况下,s2i build将应用程序源放置在/tmp/src目录中。在此目录中,将为构建过程放置源和其他资产。您可以通过设置io.openshift.s2i.destination标签或传递–destination标志来修改此位置,在这种情况下,源代码将放置在src指定目录的子目录中。上面命令(./)中的目标正在使用openshift/base-centos7映像中设置的工作目录,该目录设置为/opt/app-root/src。

现在该处理第二个必需的脚本了- run,该脚本负责运行该应用程序。在我们的例子中,它将只处理启动Lighttpd服务器:

#!/bin/bash -e

#

# S2I run script for the 'lighttpd-centos7' 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 lighttpd -D -f /opt/app-root/etc/lighttpd.conf

我们使用exec上述命令将run脚本的进程替换为Lighttpd服务器进程。这样做是为了使docker发送的所有信号都进入Lighttpd进程,并在运行该映像时使Lighttpd 的输出(stdout和stderr)可用。

由于我们的示例中没有介绍增量构建,因此可以安全地删除save-artifacts脚本。最后,我们在usage脚本中添加了一些有关如何使用构建器映像的信息:

#!/bin/bash -e
cat <<EOF
This is the lighttpd-centos7 S2I image:
To use it, install S2I: https://github.com/openshift/source-to-image

Sample invocation:

s2i build https://github.com/soltysh/sti-lighttpd.git --context-dir=test/test-app/ lighttpd-centos7 sample-app

You can then run the resulting image via:
docker run -p 8080:8080 sample-appEOF

注意:确保检查脚本文件的权限;他们应该都是755。

第4步
构建器镜像的最后剩余部分是Lighttpd的配置文件。我们在Dockerfile和run脚本中使用它。没有它,服务器将无法工作。我s2i-lighttpd/etc/lighttpd.conf使用以下内容创建它:

# directory where the documents will be served from

server.document-root = "/opt/app-root/src"
# port the server listens on
server.port = 8080

# default file if none is provided in the URL
index-file.names = ( "index.html" )

# configure specific mimetypes, otherwise application/octet-stream will be used for every file
mimetype.assign = (
".html" => "text/html",
".txt" => "text/plain",
".jpg" => "image/jpeg",
".png" => "image/png"
)

这是Lighttpd的最简单的配置文件。我们只是在设置:

  • 提供内容的路径(/opt/app-root/src),
  • 服务器监听(8080)上的连接的端口,
  • 目录(index.html)的默认文件,
  • 模仿类型映射以正确提供文件。

我们的生成器映像已准备就绪。我们可以通过调用目录中的make命令来确保它的正确构建s2i-lighttpd,正如您从中看到的那样Makefile,该docker build命令将调用一个普通命令。

第5步
现在是时候使用示例应用程序测试构建器镜像了。我正在目录中创建一个index.html包含s2i-lighttpd/test/test-app以下内容的文件:

<!doctype html>

<html>

<head>

<title>test-app</title>

</head>

<body>

<h1>Hello from lighttpd served index.html!</h1>

</body>

有了此文件后,我们现在可以进行第一个S2I构建。让我们从s2i-lighttpd目录中调用以下命令:

s2i build test/test-app/ lighttpd-centos7 sample-app

我们正在test/test-app/使用新构建的镜像(lighttpd-centos7)从目录构建应用程序。最终可运行镜像命名为sample-app。默认情况下,S2I构建会打印assemble脚本的所有输出,因此您应该在控制台中看到以下文本(目前,您可以放心地忽略有关使用本地构建器映像和非git目录的警告):

---> Installing application source

现在是时候实际测试生成的镜像了。运行镜像,使用以下命令将端口8080从容器发布到localhost上的一个:

docker run -p 8080:8080 sample-app

现在,您应该能够访问http:// localhost:8080 /并在index.html那里查看文件的内容。

第6步
s2i create我们尚未介绍的命令生成的最后一个元素是:测试。一般来说s2i-lighttpd/test/run,假设您在中选择了默认的生成端口,则生成的脚本几乎可以按原样使用Dockerfile。在这种情况下,您可以从s2i-lighttpd目录中使用以下命令运行测试套件:

make test

注意:确保检查test/run文件权限;应该是700。

该脚本使用(作为应用程序中s2i build的s2i可执行文件PATH)test-app作为应用程序源运行,然后执行一系列测试以确保镜像可用。这些测试包括:

  • 检查s2i build完成是否没有错误,
  • 检查usage脚本是否按预期工作,
  • 运行生成的镜像,
  • 检查正在运行的应用程序容器是否正确响应。

我在这里没有包括test/run脚本的内容,因为它有点冗长。鼓励有兴趣的用户签出随附的存储库https://github.com/soltysh/sti-lighttpd/。提交历史记录显示了本文介绍的确切步骤。

恭喜,您已经生成了一个支持S2I的生成器镜像,可以与S2I(OpenShift中的 iow。Source Build策略)一起使用,使用git存储库中的html文件,并生成一个新的映像,该映像将在运行时为这些html文件提供服务。概括地说,很容易看到如何使用适当的脚本定义其他构建器镜像assemble,这些run脚本将消耗其他类型的应用程序源代码并生成用于构建和运行这些应用程序的镜像。

写在最后
我司多年专注各种服务器代理:
1 来自于阿里、腾讯等大厂开发人员免费答疑,针对您的使用场景规划最合理产品方案和架构,最大限度节约成本。
2.各类云服务器全网超低价。 联想、华为、阿里、腾讯等厂商指定合作代理公司,购买有保障。
以远低于原厂的价格享受原厂的服务!

资源资源
https://github.com/soltysh/sti-lighttpd
https://github.com/openshift/source-to-image/
https://github.com/openshift/source-to-image/blob/master/docs/cli.md
https://docs.openshift.org/latest/creating_images/s2i.html
https://docs.openshift.org/latest/creating_images/s2i_testing.html
https://docs.openshift.org/latest/creating_images/metadata.html
https://docs.openshift.org/latest/creating_images/guidelines.html
https://github.com/openshift/s2i-python
https://github.com/openshift/s2i-ruby/
https://github.com/openshift/s2i-nodejs/
https://github.com/openshift/s2i-php/
https://github.com/openshift/s2i-perl/

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐