wget https://github.com/openshift/source-to-image/releases/download/v1.2.04/source-to-image-v1.1.14-874754de-linux-386.tar.gz
tar -xvf source-to-image-v1.1.14-874754de-linux-386.tar.gz
ls
s2i source-to-image-v1.1.14-874754de-linux-386.tar.gz sti
cp s2i /usr/local/bin
初始化镜像构建器
1
2
s2i create java-ubuntu22 java-ubuntu22
cd java-ubuntu22
# java-ubuntu22FROM ubuntu:22.04 # TODO: Put the maintainer name in the image metadata# LABEL maintainer="Your Name <your@email.com>"# 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 imageLABEL io.k8s.description="Java 8 web application"\
io.k8s.display-name="Java 8 Web"\
io.openshift.expose-services="8080:http"\
io.openshift.tags="builder,java,web"\
# this label tells s2i where to find its mandatory scripts# (run, assemble, save-artifacts) io.openshift.s2i.scripts-url="image:///usr/libexec/s2i"# TODO: Install required packages here:# RUN yum install -y ... && yum clean all -yRUN apt-get update && apt-get install -y \
curl \
wget \
openjdk-8-jdk &&\
rm -rf /var/lib/apt/listsWORKDIR /opt# TODO (optional): Copy the builder files into /opt/app-root# COPY ./<builder_folder>/ /opt/app-root/# 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 labelCOPY ./s2i/bin/ /usr/libexec/s2i# TODO: Drop the root user and make the content of /opt/app-root owned by user 1001# RUN chown -R 1001:1001 /opt/app-rootRUN chgrp -R 0 /usr/libexec/s2i \
&& chmod -R u=rwx,go=rx /usr/libexec/s2i &&\
chgrp -R 0 /opt \
&& chmod -R u=rwx,go=rx /opt &&\
chown -R 1001:1001 /opt &&\
chown -R 1001:1001 /usr/libexec/s2i/assembleRUN mkdir -p /opt/prometheus/etc \
&& curl https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.17.0/jmx_prometheus_javaagent-0.17.0.jar \
-o /opt/prometheus/jmx_prometheus_javaagent.jarCOPY prometheus-config.yml /opt/prometheus/etc # This default user is created in the openshift/base-centos7 imageUSER 1001# TODO: Set the default port for applications built using this imageEXPOSE 8080# TODO: Set the default CMD for the imageCMD["/usr/libexec/s2i/usage"]
#!/bin/bash -e
## S2I assemble script for the 'java-ubuntu22' 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 'java-ubuntu22' assemble script is executed with the '-h' flag, print the usage.if[["$1"=="-h"]];thenexec /usr/libexec/s2i/usage
fi# Restore artifacts from the previous build (if they exist).#echo"---> Installing application..."mkdir -p /opt/app
ls /tmp/src/*
mv /tmp/src/* /opt/app/
chmod +x /opt/app/*.jar
默认情况下,s2i build 将应用程序源代码放在 /tmp/src。上述命令将应用程序 jar 包复制到 /opt/app/ 目录下
run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash -e
## S2I run script for the 'java-ubuntu22' 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#PROMETHEUS_JMX_OPTS="-javaagent:/opt/prometheus/jmx_prometheus_javaagent.jar=${PROMETHEUS_JMX_PORT}:/opt/prometheus/etc/prometheus-config.yml"# Always include jolokia-opts, which can be empty if switched off via envJAVA_OPTIONS="${JAVA_OPTIONS:+${JAVA_OPTIONS}}"# Temporary options variable until the harmonization hawt-app PR #5 has been applied (hopefully)JVM_ARGS="${JVM_ARGS:+${JVM_ARGS}}${JAVA_OPTIONS}${PROMETHEUS_JMX_OPTS}"export JAVA_OPTIONS JVM_ARGS PROMETHEUS_JMX_OPTS
exec java -jar ${JVM_ARGS} /opt/app/app.jar
# curl http://localhost:12345|more % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100110081001100800541170 --:--:-- --:--:-- --:--:-- 56163# HELP jvm_memory_pool_allocated_bytes_total Total bytes allocated in a given JVM memory pool. Only updated after GC, not continuously.# TYPE jvm_memory_pool_allocated_bytes_total counterjvm_memory_pool_allocated_bytes_total{pool="Code Cache",} 1.1579328E7
jvm_memory_pool_allocated_bytes_total{pool="PS Eden Space",} 5.42141224E8
jvm_memory_pool_allocated_bytes_total{pool="PS Old Gen",} 1.2845896E7
jvm_memory_pool_allocated_bytes_total{pool="PS Survivor Space",} 2.0936672E7
jvm_memory_pool_allocated_bytes_total{pool="Compressed Class Space",} 3700680.0
jvm_memory_pool_allocated_bytes_total{pool="Metaspace",} 3.0138704E7
# HELP jvm_threads_current Current thread count of a JVM# TYPE jvm_threads_current gaugejvm_threads_current 17.0
# HELP jvm_threads_daemon Daemon thread count of a JVM# TYPE jvm_threads_daemon gaugejvm_threads_daemon 15.0
# HELP jvm_threads_peak Peak thread count of a JVM# TYPE jvm_threads_peak gaugejvm_threads_peak 17.0
# HELP jvm_threads_started_total Started thread count of a JVM# TYPE jvm_threads_started_total counterjvm_threads_started_total 20.0
# HELP jvm_threads_deadlocked Cycles of JVM-threads that are in deadlock waiting to acquire object monitors or ownable synchronizers# TYPE jvm_threads_deadlocked gaugejvm_threads_deadlocked 0.0
# HELP jvm_threads_deadlocked_monitor Cycles of JVM-threads that are in deadlock waiting to acquire object monitors# TYPE jvm_threads_deadlocked_monitor gaugejvm_threads_deadlocked_monitor 0.0
# HELP jvm_threads_state Current count of threads by state# TYPE jvm_threads_state gaugejvm_threads_state{state="NEW",} 0.0
jvm_threads_state{state="TIMED_WAITING",} 7.0
jvm_threads_state{state="BLOCKED",} 0.0
jvm_threads_state{state="RUNNABLE",} 8.0
jvm_threads_state{state="WAITING",} 2.0
jvm_threads_state{state="TERMINATED",} 0.0
# HELP jvm_classes_currently_loaded The number of classes that are currently loaded in the JVM# TYPE jvm_classes_currently_loaded gaugejvm_classes_currently_loaded 5736.0
# HELP jvm_classes_loaded_total The total number of classes that have been loaded since the JVM has started execution# TYPE jvm_classes_loaded_total counterjvm_classes_loaded_total 5736.0
# HELP jvm_classes_unloaded_total The total number of classes that have been unloaded since the JVM has started execution# curl http://localhost:8080 Hello,World!%
自定义 S2I 模板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
apiVersion:devops.kubesphere.io/v1alpha1kind:S2iBuilderTemplatemetadata:labels:controller-tools.k8s.io:"1.0"builder-type.kubesphere.io/s2i:"s2i"name:java-ubuntuspec:containerInfo:- builderImage:java-ubuntu22 ## 自定义的镜像构建器镜像codeFramework:java# type of code frameworkdefaultBaseImage:java-ubuntu22# default Image Builder (can be replaced by a customized image)version:0.0.1# Builder template versiondescription:"模板描述"# Builder template description
apiVersion:devops.kubesphere.io/v1alpha1kind:S2iBuilderTemplatemetadata:annotations:descriptionCN:Java 应用的构建器模版。通过该模版可构建出直接运行的应用镜像。descriptionEN:This is a builder template for Java builds whose result can berun directly without any further application server.It's suited ideally formicroservices with a flat classpath (including "far jars").devops.kubesphere.io/s2i-template-url:https://github.com/kubesphere/s2i-java-container/blob/master/java/imageshelm.sh/hook:pre-installlabels:binary-type.kubesphere.io:jarbuilder-type.kubesphere.io/b2i:b2ibuilder-type.kubesphere.io/s2i:s2icontroller-tools.k8s.io:"1.0"name:javaspec:codeFramework:javacontainerInfo:- buildVolumes:- s2i_java_cache:/tmp/artifactsbuilderImage:kubesphere/java-8-centos7:v3.2.0runtimeArtifacts:- source:/deploymentsruntimeImage:kubesphere/java-8-runtime:v3.2.0- buildVolumes:- s2i_java_cache:/tmp/artifactsbuilderImage:kubesphere/java-11-centos7:v3.2.0runtimeArtifacts:- source:/deploymentsruntimeImage:kubesphere/java-11-runtime:v3.2.0defaultBaseImage:kubesphere/java-8-centos7:v3.2.0description:This is a builder template for Java builds whose result can be rundirectly without any further application server.It's suited ideally for microserviceswith a flat classpath (including "far jars")environment:- defaultValue:""description:Arguments to use when calling Maven, replacing the default packagehawt-app:build -DskipTests -e. Please be sure to run the hawt-app:build goal(when not already bound to the package execution phase), otherwise the startupscripts won't work.key:MAVEN_ARGSrequired:falsetype:string- defaultValue:""description:Additional Maven arguments, useful for temporary adding argumentslike -X or -am -pl .key:MAVEN_ARGS_APPENDrequired:falsetype:string- defaultValue:""description:With Repositories you specify from which locations you want to downloadcertain artifacts, such as dependencies and maven-plugins.key:MAVEN_MIRROR_URLrequired:falsetype:string- defaultValue:""description:If set then the Maven repository is removed after the artifact isbuilt. This is useful for keeping the created application image small, but preventsincremental builds. The default is falsekey:MAVEN_CLEAR_REPOrequired:falsetype:boolean- defaultValue:""description:Path to target/ where the jar files are created for multi modulebuilds. These are added to ${MAVEN_ARGS}key:ARTIFACT_DIRrequired:falsetype:string- defaultValue:""description:Arguments to use when copying artifacts from the output dir to theapplication dir. Useful to specify which artifacts will be part of the image.It defaults to -r hawt-app/* when a hawt-app dir is found on the build directory,otherwise jar files only will be included (*.jar).key:ARTIFACT_COPY_ARGSrequired:falsetype:string- defaultValue:""description:the directory where the application resides. All paths in your applicationare relative to this directory. By default it is the same directory where thisstartup script resides.key:JAVA_APP_DIRrequired:falsetype:string- defaultValue:""description:directory holding the Java jar files as well an optional classpathfile which holds the classpath. Either as a single line classpath (colon separated)or with jar files listed line-by-line. If not set JAVA_LIB_DIR is the same asJAVA_APP_DIR.key:JAVA_LIB_DIRrequired:falsetype:string- defaultValue:""description:options to add when calling javakey:JAVA_OPTIONSrequired:falsetype:string- defaultValue:""description:a number >= 7. If the version is set then only options suitable forthis version are used. When set to 7 options known only to Java > 8 will beremoved. For versions >= 10 no explicit memory limit is calculated since Java>= 10 has support for container limits.key:JAVA_MAJOR_VERSIONrequired:falsetype:string- defaultValue:""description:is used when no -Xmx option is given in JAVA_OPTIONS. This is usedto calculate a default maximal Heap Memory based on a containers restriction.If used in a Docker container without any memory constraints for the containerthen this option has no effect. If there is a memory constraint then -Xmx isset to a ratio of the container available memory as set here. The default is25when the maximum amount of memory available to the container is below 300M,50otherwise, which means in that case that 50% of the available memory is usedas an upper boundary. You can skip this mechanism by setting this value to 0in which case no -Xmx option is added.key:JAVA_MAX_MEM_RATIOrequired:falsetype:string- defaultValue:""description:is used when no -Xms option is given in JAVA_OPTIONS. This is usedto calculate a default initial Heap Memory based on a containers restriction.If used in a Docker container without any memory constraints for the containerthen this option has no effect. If there is a memory constraint then -Xms isset to a ratio of the container available memory as set here. By default thisvalue is not set.key:JAVA_INIT_MEM_RATIOrequired:falsetype:string- defaultValue:""description:restrict manually the number of cores available which is used forcalculating certain defaults like the number of garbage collector threads. Ifset to 0 no base JVM tuning based on the number of cores is performed.key:JAVA_MAX_CORErequired:falsetype:string- defaultValue:""description:set this to get some diagnostics information to standard out whenthings are happeningkey:JAVA_DIAGNOSTICSrequired:falsetype:string- defaultValue:""description:main class to use as argument for java. When this environment variableis given, all jar files in $JAVA_APP_DIR are added to the classpath as wellas $JAVA_LIB_DIR.key:JAVA_MAIN_CLASSrequired:falsetype:string- defaultValue:""description:A jar file with an appropriate manifest so that it can be startedwith java -jar if no $JAVA_MAIN_CLASS is set. In all cases this jar file isadded to the classpath, too.key:JAVA_APP_JARrequired:falsetype:string- defaultValue:""description:Name to use for the processkey:JAVA_APP_NAMErequired:falsetype:string- defaultValue:""description:the classpath to use. If not given, the startup script checks fora file ${JAVA_APP_DIR}/classpath and use its content literally as classpath.If this file doesn't exists all jars in the app dir are added (classes:${JAVA_APP_DIR}/*).key:JAVA_CLASSPATHrequired:falsetype:string- defaultValue:""description:If set remote debugging will be switched onkey:JAVA_DEBUGrequired:falsetype:string- defaultValue:""description:If set enables suspend mode in remote debuggingkey:JAVA_DEBUG_SUSPENDrequired:falsetype:string- defaultValue:""description: 'Port used for remote debugging. Default:5005'key:JAVA_DEBUG_PORTrequired:falsetype:string- defaultValue:""description:The URL of the proxy server that translates into the http.proxyHostand http.proxyPort system properties.key:HTTP_PROXYrequired:falsetype:string- defaultValue:""description:The URL of the proxy server that translates into the https.proxyHostand https.proxyPort system properties.key:HTTPS_PROXYrequired:falsetype:string- defaultValue:""description:The list of hosts that should be reached directly, bypassing theproxy, that translates into the http.nonProxyHosts system property.key:NO_PROXYrequired:falsetype:stringiconPath:assets/java.pngversion:0.0.2