OpenShift-构建自动化-Nginx

创建S2I项目

s2i create nginx-centos7-s2i nginx-centos7
./media/image1.png

准备S2I脚本

assemble

#!/bin/bash -e # # S2I assemble script for the ‘nginx-centos7’ 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 [[ “\$1” == “-h” ]]; then # If the ‘nginx-centos7’ assemble script is executed with ‘-h’ flag, # print the usage. exec /usr/libexec/s2i/usage fi # Restore artifacts from the previous build (if they exist). # We set them here just for show, but you will need to set this up with your logic # according to the application directory that you chose. #if [ “\$(ls /tmp/artifacts/ 2>/dev/null)” ]; then # echo “—> Restoring build artifacts…” # mv /tmp/artifacts/* \ #fi # Override the default nginx index.html file. # This is what we consider in this example ‘installing the application’ # here you can go ahead an replace this with the actual building of python modules, # bundle install, and anything else you need. echo “—> Building and installing application from source…” if [ -f /tmp/src/nginx.conf ]; then mv /tmp/src/nginx.conf /etc/nginx/nginx.conf fi if [ “\$(ls -A /tmp/src)” ]; then mv /tmp/src/* /usr/share/nginx/html/ fi

run

#!/bin/bash -e # 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 # # We will turn off daemonizing for the nginx process so that the container # doesn’t exit after the process runs. exec /usr/sbin/nginx -g “daemon off;”

save-artifacts

#!/bin/sh -e # # S2I save-artifacts script for the ‘nginx-centos7’ image. # The save-artifacts script streams a tar archive to standard output. # The archive contains the files and folders you want to re-use in the next build. # # For more information see the documentation: # https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md # #tar cf - \

usage

#!/bin/bash -e cat \<\<EOF This is the nginx-centos7 S2I image: To use it, install S2I: https://github.com/openshift/source-to-image Sample invocation: s2i build test/test-app nginx-centos7 nginx-centos7-app You can then run the resulting image via: docker run -d -p 8080:8080 nginx-centos7-app and see the test via http://localhost:8080 EOF

准备Dockfile

# nginx-centos7 # Here you can use whatever base image is relevant for your application. FROM centos:centos7 # Here you can specify the maintainer for the image that you’re building LABEL maintainer=”Chen Fei \<chenf\@inno-view.cn>“ # Export an environment variable that provides information about the application version. # Replace this with the version for your application. ENV NGINX_VERSION=1.6.3 # Set the labels that are used for OpenShift to describe the builder image. LABEL io.k8s.description=”Nginx Webserver” \ io.k8s.display-name=”Nginx 1.6.3” \ io.openshift.expose-services=”8080:http” \ io.openshift.tags=”builder,webserver,html,nginx” \ # this label tells s2i where to find its mandatory scripts # (run, assemble, save-artifacts) io.openshift.s2i.scripts-url=”image:///usr/libexec/s2i” # Install the nginx web server package and clean the yum cache RUN yum install -y epel-release && \ yum install -y –setopt=tsflags=nodocs nginx && \ yum clean all # Change the default port for nginx # Required if you plan on running images as a non-root user). RUN sed -i ‘s/80/8080/‘ /etc/nginx/nginx.conf RUN sed -i ‘s/user nginx;//‘ /etc/nginx/nginx.conf # Copy the S2I scripts to /usr/libexec/s2i since we set the label that way COPY ./s2i/bin/ /usr/libexec/s2i RUN chown -R 1001:1001 /usr/share/nginx RUN chown -R 1001:1001 /var/log/nginx RUN chown -R 1001:1001 /var/lib/nginx RUN touch /run/nginx.pid RUN chown -R 1001:1001 /run/nginx.pid RUN chown -R 1001:1001 /etc/nginx USER 1001 # Set the default port for applications built using this image EXPOSE 8080 # Modify the usage script in your application dir to inform the user how to run # this image. CMD [“/usr/libexec/s2i/usage”]

生成Builder Image

docker build -t nginx-centos7 .
./media/image2.png

上传OpenShift

上传到Harbor

docker tag nginx-centos7:latest dtr.ivops.cn:5443/openshift-chenf/nginx-centos7:latest
docker push dtr.ivops.cn:5443/openshift-chenf/nginx-centos7:latest

创建Image Stream

oc import-image dtr.ivops.cn:5443/openshift-chenf/nginx-centos7 -n openshift –confirm –insecure
添加Builder tags
oc edit is nginx-centos7 -n openshift
./media/image3.png
./media/image4.png

根据源代码构建

./media/image5.png
Builds:
./media/image6.png
完成
./media/image7.png
验证应用
./media/image8.png

代码更新

代码侧有版本更新,重新构建
./media/image9.png
./media/image10.png
./media/image11.png