Docker容器运维相关OpenShift Origin

010-S2I镜像定制

2018-04-26  本文已影响53人  四冶读史

1.概述

Source to Image流程为应用的容器化提供了一个标准,实现了自动化。OpenShift默认提供Java WildFly、PHP、Python、Ruby及Perl的S2I Builder镜像。但是现实中的需求是多样化的,特殊的应用构建环境需要用户定制S2I的Builder Image来满足。

S2I Builder镜像从本质上来说也是一个普通的Docker镜像,只是在镜像中会加入S2I流程需要的一些脚本和配置。下面将展示一个基础的S2I Builder镜像的制作过程。

2.环境准备

1)下载S2I的二进制执行文件。

[root@master ~]# cd /opt
[root@master opt]# wget https://github.com/openshift/source-to-image/releases/download/v1.1.9/source-to-image-v1.1.9-db2b4645-linux-amd64.tar.gz

2)将下载好的压缩包解压到/usr/bin目录。

[root@master opt]# tar zxvf source-to-image-v1.1.9-db2b4645-linux-amd64.tar.gz -C /usr/bin

3)检查安装

[root@master opt]# s2i
Source-to-image (S2I) is a tool for building repeatable docker images.

A command line interface that injects and assembles source code into a docker image.
Complete documentation is available at http://github.com/openshift/source-to-image

Usage:
  s2i [flags]
  s2i [command]

Available Commands:
  build       Build a new image
  completion  Generate completion for the s2i command (bash or zsh)
  create      Bootstrap a new S2I image repository
  rebuild     Rebuild an existing image
  usage       Print usage of the assemble script associated with the image
  version     Display version

Flags:
      --ca string        Set the path of the docker TLS ca file (default "/root/.docker/ca.pem")
      --cert string      Set the path of the docker TLS certificate file (default "/root/.docker/cert.pem")
  -h, --help             help for s2i
      --key string       Set the path of the docker TLS key file (default "/root/.docker/key.pem")
      --loglevel int32   Set the level of log output (0-5)
      --tls              Use TLS to connect to docker; implied by --tlsverify
      --tlsverify        Use TLS to connect to docker and verify the remote
  -U, --url string       Set the url of the docker socket to use (default "unix:///var/run/docker.sock")

Use "s2i [command] --help" for more information about a command.

3.创建S2I Builder镜像

以python-27-centos为例。

[root@master opt]# s2i create python-27-centos python-27-centos

第一个参数定义了S2I Builder镜像名,第二个参数定义了工作目录名称。

s2i create命令执行成功后,可以看到命令创建了一个python-27-centos目录,其中包含一个Dockerfile、Makefile、s2i和test目录。

[root@master opt]# ls -a python-27-centos/
.  ..  Dockerfile  Makefile  README.md  s2i  test

查看Dockerfile文件,可见大部分内容已被注释。该文件只是一个实例,用户可以在此文件的基础上编写实际的Dockerfile内容。

[root@master opt]# cd python-27-centos
[root@master python-27-centos]# cat Dockerfile 
......

以上的几个脚本在S2I的不同阶段会被调用并执行相应的操作,它们的作用分别如下:

4.编写Dockerfile

[root@master python-27-centos]# cat Dockerfile 
# python-27-centos
FROM centos/python-27-centos7

# TODO: Put the maintainer name in the image metadata
LABEL maintainer="Zhang XianWen <xianwen.zhang@aorise.org>"

# TODO: Rename the builder environment variable to inform users about application you provide them
ENV BUILDER_VERSION 1.0

# TODO: Set labels used in OpenShift to describe the builder image
LABEL io.openshift.s2i.scripts-url=image:///usr/libexec/s2i \
      io.k8s.description="Python2.7 S2I Builder" \
      io.k8s.display-name="python2.7 s2i builder 1.0" \
      io.openshift.expose-services="8080:http" \
      io.openshift.tags="builder,python"

WORKDIR /opt

# TODO: Copy the S2I scripts to /usr/libexec/s2i, since openshift/base-centos7 image
# sets io.openshift.s2i.scripts-url label that way, or update that label
COPY ./s2i/bin/ /usr/libexec/s2i

# This default user is created in the openshift/base-centos7 image
#USER 1001

# TODO: Set the default port for applications built using this image
EXPOSE 8080

ENTRYPOINT []

# TODO: Set the default CMD for the image
CMD ["/usr/libexec/s2i/usage"]

Docker镜像一个巨大的优点是它的重要性。因此,构建一个S2I Builder不必从无做起。该例就是在基于python-27-centos7镜像来构建一个新的S2I Builder镜像的。

LABEL指令添加了一些帮助OpenShift获取镜像元信息的标签。其中io.openshift.s2i.scripts-url=image:///usr/libexec/s2i标签指定了S2I依赖的脚步所在的路径,S2I执行器将到此路径中查找需要的执行脚本。

此次并没有启用USER,该命令指定一个用户作为容器的启动用户。DockerHub上大量的容器都是以root用户作为启动用户,在某些情况下存在安全风险,因此建议能用非特权用户启动的容器应用,尽量以非特权用户启动。

5.编辑S2I脚本

5.1.编辑assemble

S2I执行器下载好源码后将执行assemble脚本。

 [root@master python-27-centos]# cat s2i/bin/assemble 
......

git源码默认存放在/tmp/src下,此次下载的源码并不需要做其它的操作,故保持默认即可。

5.2.编辑run

编辑./s2i/bin/run脚本,添加启动命令。S2I流程生成的应用镜像启动时将执行run脚本,启动服务。

[root@master python-27-centos]# cat s2i/bin/run 
#!/bin/bash -e
#
# S2I run script for the 'python-27-centos' 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 asdf -p 8080
bash -c "/opt/control start"

6.执行镜像构建

[root@master python-27-centos]# make

执行构建完成后,可以通过s2i build <源代码地址> <Builder镜像名> <构建输出镜像名> 命令执行测试此S2I构建的镜像。

[root@master python-27-centos]# s2i build https://github.com/smtants/hbt python-27-centos test-app

运行镜像并测试镜像是否工作正常。

[root@master python-27-centos]# docker run -it -p 8080:8080 test-app
[root@master python-27-centos]# curl 127.0.0.1:8080

7.导入镜像

通过前面的步骤,我们已成功构建了一个S2I Builder。这个镜像要在OpenShift中用起来还需要将这个镜像推送到一个Docker仓库中,并将镜像信息导入OpenShift形成Image Stream。为了测试,可以在本地搭建一个Docker仓库。生产环境可以使用一个实例来搭建Docker仓库。

通过yum在Master节点上安装并配置Docker Registry。

[root@master python-27-centos]# yum install docker-registry -y
[root@master python-27-centos]# systemctl start docker-registry
[root@master python-27-centos]# systemctl enable docker-registry

如果发现没有找到docker-registry,那就需要根据yum的下载提示,docker-registry已更名为dcoker-distribution。

由于本例搭建的仓库并没有配置证书,因此,需要修改Docker的配置文件/etc/sysconfig/docker,添加如下配置,将本例中的仓库标记为不使用证书验证仓库。

INSECURE_REGISTRY='--insecure-registry master.example.com:5000'

配置文件修改后,重启Docker服务,使配置生效。

[root@master python-27-centos]# systemctl restart docker

仓库创建好后,为镜像打上标签,并将其推送至已搭建好的镜像仓库。

[root@master python-27-centos]# docker tag python-27-centos master.example.com:5000/python-27-centos
[root@master python-27-centos]# docker push master.example.com:5000/python-27-centos

当镜像推送至仓库后,可通过oc import-image将镜像导入OpenShift中生成相应的Image Stream。注意到导入openshift项目中,以使该Image Stream可以被其他项目引用。

[root@master python-27-centos]# oc import-image master.example.com:5000/python-27-centos -n openshift --confirm --insecure

此时,在web界面上还无法识别。为了让OpenShift识别出这个镜像是S2I的Builder镜像,需要编辑刚导入的Image Stream,添加注解"tags": "builder"。例如:

[root@master python-27-centos]# oc edit is/python-27-centos -n openshift
tags:
  - annotations:
      description: Python 2.7 builder
      iconClass: icon-python
      openshift.io/display-name: Python 2.7 builder
      tags: builder,python
      version: "2.7"

至此,自定义的S2I镜像就已完成,可以在Web控制台使用。

参考地址:
https://github.com/openshift/source-to-image
https://blog.csdn.net/huqigang/article/details/78110233

上一篇 下一篇

猜你喜欢

热点阅读