s2i简介

作者: IvyFan2017 | 来源:发表于2017-10-08 09:52 被阅读0次

s2i原理

s2i是红帽推出的一种基于容器的应用镜像构建工具。它需要三个元素:源码,基础镜像,构建配置。大概流程如下:

  1. 首先用户需要制作一个基础镜像,作为构建的基础环境,用户需要配置好相应的软件包,环境变量,等等
  2. s2i负责启动一个容器(其镜像为用户指定的基础镜像)
  3. s2i将源码拉取到容器中的/tmp/src目录下,然后读取s2i的配置信息进行源码编译及安装配置
  4. s2i将编译好的容器提交成新的镜像

s2i配置文件信息

主要包含四个文件,分别为:

  1. assemble 用于对源码编译以及安装配置的脚本
  2. run 启动应用镜像时执行的脚本
  3. save-artifacts 用于保存一些编译中复用的内容,这样下次编译可以直接使用这些文件,从而加快编译安装等速度
  4. usage 打印生成镜像的帮助信息

这四个文件都需要用户自行定义,s2i会通过三种方式寻找这些文件,分别为:

  • 源码中.s2i/bin下
  • --scripts-url 参数指定的位置
  • 镜像中label:io.openshift.s2i.scripts-url指定的位置

推荐使用第一种方式,这样方便管理和持续集成,其他两种url都支持以下三种形式:

  • image://path_to_scripts_dir 在镜像内的绝对路径
  • file://path_to_scripts_dir 在主机上的绝对或者相对路径
  • http(s)://path_to_scripts_dir 网络上的文件

cloudstack在openshift中的部署

由于不同应用的配置文件差异很大,所以此处不会对配置文件进行详细解释,主要列出主要文件的内容。在cloudstack的环境中中主要包含三台cloudstack server和后端的mysql主从数据库,其中需要用到s2i功能的只有cloudstack的编译,由于三个cs server的差别不大,所以使用同一个镜像,通过环境变量的不同来决定是否为主服务器。

cloudstack服务部署

cloudstack基础镜像

此处直接列出它的Dockerfile

# cloudstack-base
FROM centos:6.6

# TODO: Put the maintainer name in the image metadata
MAINTAINER bdt <bit_bdt@foxmail.com>

# relate url : https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md#required-image-contents

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

Run yum -y install wget which && yum -y install epel-release && yum -y update && yum -y upgrade \
    && yum install -y ntp && yum -y install java-1.6.0-openjdk \
    && yum -y install mysql mysql-server && yum -y install git svn rpm-build \
    && yum -y install python-setuptools && yum -y install genisoimage \
    && yum -y install java-1.6.0-openjdk-devel tomcat6 ws-commons-util MySQL-python \
    && yum -y clean all

Run yum -y install selinux-policy  libselinux-devel selinux-policy-targeted python-pip &&  yum -y clean all  \
    && pip install --upgrade pip && pip install supervisor \
    && sed -i 's/SELINUX/#SELINUX/g' /etc/selinux/config  && echo "SELINUX=disabled" >> /etc/selinux/config

Run wget https://archive.apache.org/dist/tomcat/tomcat-6/v6.0.35/bin/apache-tomcat-6.0.35.tar.gz \
    && tar xzvf apache-tomcat-6.0.35.tar.gz -C /usr/local \
    && rm -rf apache-tomcat-6.0.35.tar.gz

Run wget http://mirror.cc.columbia.edu/pub/software/apache/maven/maven-3/3.0.5/binaries/apache-maven-3.0.5-bin.tar.gz \
    && tar xzf apache-maven-3.0.5-bin.tar.gz -C /usr/local \
    && cd /usr/local && ln -s apache-maven-3.0.5 maven && rm -rf apache-maven-3.0.5-bin.tar.gz

Run pip uninstall -y meld3 && wget https://pypi.python.org/packages/source/m/meld3/meld3-1.0.2.tar.gz && tar -zxf meld3-1.0.2.tar.gz \
    && cd meld3-1.0.2 && python setup.py install && cd - && rm -f meld3-1.0.2.tar.gz && rm -rf meld3-1.0.2

Run yum -y install gcc && yum -y clean all

ADD repository /root/.m2/repository

s2i配置

前边所述的四个文件,在本文中主要用到是run和assemble,所以此处只列出它们的内容

run

主要执行start.sh

sh /tmp/src/.s2i/bin/start.sh

其内容如下:

for de in $(losetup -a | grep "cloudstack" | awk -F: '{print $1}'); do losetup -d $de; done;

a=$(losetup -a|wc -l)
mknod /dev/loop$a -m666 b 7 $a
let a++
mknod /dev/loop$a -m666 b 7 $a
losetup -f

#losetup -f
#if [ $? -ne 0 ]; then
#    mknod $(losetup -a | awk -F: 'BEGIN{a=0}  {a++;} END{printf("/dev/loop%d -m666 b 7 %d",a,a)}')
#fi

if [ "$CSM" == "TRUE" ]; then
        cloudstack-setup-databases cloud:password@"$MYSQL_MASTER_SERVICE_HOST"  --deploy-as=root:password
elif [ "$CSM" == "FALSE" ]; then
        cloudstack-setup-databases cloud:password@"$MYSQL_MASTER_SERVICE_HOST"
else
        echo "the value of CSM is not legal"
fi

echo_supervisord_conf > /etc/supervisord.conf
echo -e "[program:cloudstack-setup-management]\ncommand=cloudstack-setup-management" >> /etc/supervisord.conf
supervisord --nodaemon

assemble

#!/bin/bash -e
#
# S2I assemble script for the 'cs-base' image.
# The 'assemble' script builds your application source so that it is ready to run.
#
# For more information refer to the documentation:
#   https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#

# If the 'cs-base' assemble script is executed with the '-h' flag, print the usage.
if [[ "$1" == "-h" ]]; then
    exec /usr/libexec/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 cloudstack source code..."
#cp -Rf /tmp/src/. ./

echo "---> Building cloudstack from source with args -p noredist... "
# TODO: Add build steps for your application, eg npm install, bundle install, pip install, etc.
pushd /tmp/src/packaging/centos63/
sh package.sh -p noredist
popd


echo "---> move rpms and clean build data..."
cp -r /tmp/src/dist/rpmbuild/RPMS/x86_64/ /root/x86_64/
rm -rf /tmp/src/dist
rm -rf /root/.m2

echo "---> yum localinstall rpms..."

yum -y localinstall /root/x86_64/cloudstack-common-4.3.0*.rpm /root/x86_64/cloudstack-awsapi-4.3.0*.rpm /root/x86_64/cloudstack-management-4.3.0*.rpm

mysql主从配置

mysql master的配置

其dockerfile如下

# cloudstack-base
FROM centos:6.6

# TODO: Put the maintainer name in the image metadata
MAINTAINER bdt <bit_bdt@foxmail.com>

RUN yum -y update && yum install mysql-server -y && yum clean all -y

RUN chkconfig iptables off

COPY start.sh /usr/share/start.sh

RUN chmod 555 /usr/share/start.sh

EXPOSE 3306

CMD sh /usr/share/start.sh && /usr/bin/mysqld_safe

对应的start.sh内容如下:

sed -i "/\[mysqld\]/a server-id=1\nlog-bin=mysql-bin\nbinlog-format='ROW'" /etc/my.cnf
service mysqld start \
    && mysql -e \
    "SET PASSWORD = PASSWORD('password'); \
    CREATE USER 'root'@'%' IDENTIFIED BY 'password'; \
    CREATE USER 'repl'@'%' IDENTIFIED BY 'password'; \
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; \
    GRANT REPLICATION SLAVE ON  *.* TO 'repl'@'%' IDENTIFIED BY 'password'; \
    flush privileges;"
service mysqld stop

mysql slave的配置

其dockerfile如下

# cloudstack-base
FROM centos:6.6

# TODO: Put the maintainer name in the image metadata
MAINTAINER bdt <bit_bdt@foxmail.com>

RUN yum -y update && yum install mysql-server -y && yum clean all -y

Run chkconfig iptables off

ADD start.sh /usr/share/start.sh

RUN chmod 555 /usr/share/start.sh

EXPOSE 3306

CMD sh /usr/share/start.sh  && /usr/bin/mysqld_safe

对应的start.sh如下

sed -i "/\[mysqld\]/a server-id=2\nlog-bin=mysql-bin\nbinlog-format = 'ROW'" /etc/my.cnf
service mysqld start
mysql -e \
    "SET PASSWORD = PASSWORD('password'); \
    CREATE USER 'root'@'%' IDENTIFIED BY 'password'; \
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; \
    flush privileges;
    change master to master_host='$MYSQL_MASTER_SERVICE_HOST',master_user='repl',master_password='password';"
service mysqld stop

openshift中部署cloudstack应用

具体的yaml文件此处不再展示,可以访问gitlab中cloudstack项目自行查看

参考链接

  1. https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md#required-image-contents
  2. https://blog.openshift.com/create-s2i-builder-image/

相关文章

  • s2i简介

    s2i原理 s2i是红帽推出的一种基于容器的应用镜像构建工具。它需要三个元素:源码,基础镜像,构建配置。大概流程如...

  • OpenShift Python S2I镜像

    Python S2I 镜像 概览 OpenShift Container Platform提供支持 S2I的 Py...

  • Openshift完成S2I

    将S2I产生的镜像push到openshift Rejistry Rejistry的分类:1)内部镜像仓库:供op...

  • 近期项目小结

    自定义应用 Pod 的启动命令 在使用 openshift 提供的 S2I 方式进行镜像的构建和启动 pod ...

  • 定制化openshit S2I

    Openshift可以根据git repo的source code去创建应用。oc new-app根据git re...

  • s2i ksonnet helm minikube kubect

    以上 工具都是基于docker 的虚拟化工具,每个工具都足够让你节省非常多的时间来构建镜像,部署你的 应用服务 s...

  • openshift s2i tomcat 镜像定制

    参考链接:https://github.com/openshift/source-to-image/blob/ma...

  • 18旅游二班各组自我简介汇总

    一组简介 二组简介 三组简介 四组简介 五组简介 六组简介 七组简介 八组简介

  • 简介

    上辈子,洛幽喜欢了东凡一辈子,没有结果,之后她死了,她不后悔,只是为什么她一睁眼,嗯?捏了捏自己的脸,她这是回到了...

  • 简介

    1.主动地去阅读。 2.阅读目的:获得资讯,获得理解 前者为了增进资讯,获得信息,不能增加理解力。后者为了理解更多...

网友评论

      本文标题:s2i简介

      本文链接:https://www.haomeiwen.com/subject/opkeyxtx.html