运维采用k8s后,相关服务需要做到自动生成Docker images并上传到私服,私服采用的阿里云仓库并配合VPC网络
Java环境说明
- jdk 1.8
- spring-boot 2.0
- maven
- dockerfile-maven 1.3.6
Java image
项目采用Maven构建,使用dockerfile-maven-plugin来做docker images的构建与上传
插件引用
在pom.xml中添加插件引用
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<configuration>
<!-- 排除该工程不deploy 到远程服务器上 -->
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>vc.thinker.cabbage.web.AdminServerMain</mainClass>
<layout>JAR</layout>
<executable>true</executable>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 仓库地址 -->
<repository>registry-vpc.cn-shenzhen.aliyuncs.com/xxx/${project.artifactId}</repository>
<!-- docker images tag -->
<tag>${project.version}</tag>
<!-- 采用Maven Settings进行docker仓库授权 -->
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
<!-- dockerfile ARG,buildArgs可自定义多个配合dockerfile 的ARG指令使用 -->
<buildArgs>
<!-- jar地址 -->
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
<!-- 配制文件地址 -->
<PROPERTIES>target/classes/${project.build.finalName}/application.properties</PROPERTIES>
<!-- 启动执行命令 -->
<START>target/classes/${project.build.finalName}/k8s-start.sh</START>
</buildArgs>
</configuration>
</plugin>
dockerfile
在项目根目录下添加Dockerfile文件
#built java images,注意本例中没有处理时区问题
FROM openjdk:8-jre
ARG JAR_FILE
ARG PROPERTIES
ARG START
COPY ${JAR_FILE} /JAVA/app.jar
COPY ${PROPERTIES} /JAVA/application.properties
COPY ${START} /JAVA/k8s-start.sh
RUN chmod +x /JAVA/k8s-start.sh && \
ln -s /JAVA/k8s-start.sh /usr/bin/k8s-start
#web 服务端口
EXPOSE 8080
#CMD ["k8s-start"]
ENTRYPOINT ["k8s-start"]
配制文件与执行命令
src/main/resources 下添加 k8s-start.sh 和 application.properties,这里说明一下k8s-start.sh
#!/bin/sh
#采用环境变量的方式修改配制文档,由于环境变量不能有".-"字符,约定".-"统一转换为下划线_
function change(){
echo "準備修改配置項 $1"
local j=`echo "$1"| sed 's/\./_/g'| sed 's/\-/_/g'`
#echo $j
local i=`eval echo '$'{"$j"}`
echo "對應變量為 $j"
[[ ! $i ]] && echo "$j 環境變量不存在,不進行替換!" && return 0
sed -i "s/$1=.*/$1=$i/" /ROOT/application.properties && echo "替換變量 $i 成功 "
}
#修改配制文档,
change spring.datasource.url
change spring.datasource.username
change spring.datasource.password
cd /JAVA
if [[ "$1" == 'sh' || "$1" == 'bash' || "$1" == '/bin/sh' ]]; then
exec "/bin/sh"
fi
#java-conf为jvm参数
echo "配置:java-conf=$java_conf"
if [ "${java_conf}" ]; then
echo "运行java命令:java $java_conf -jar app.jar "
exec su-exec java java $java_conf -jar app.jar
fi
echo "运行java命令:java "$@" -jar app.jar"
exec su-exec java java "$@" -jar app.jar
docker仓库权限
在maven settings.xml设置仓库地址与权限
<configuration>
<repository>docker-repo.example.com:8080/organization/image</repository>
<tag>latest</tag>
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
</configuration>
权限
<servers>
<server>
<id>docker-repo.example.com:8080</id>
<username>me</username>
<password>mypassword</password>
</server>
</servers>
使用说明
本例中直接执行 mvn deploy 就可以做到docker images的构建和上传
mvn deploy
本地运行
docker run --name my-server -p 8080:8080 \
-e spring_datasource_username="test"
-d images:tag \
-e spring_datasource_username=”test” 为修改application.properties中的spring.datasource.username配制
Dockerfile Maven的特性
默认,构建Dockerfile的过程包含在mvn package阶段;
默认,为Dockerfile打标签的过程包含在mvn package阶段;
默认,发布Dockerfile的过程包含在mvn deploy阶段;
也可以直接陆续执行:
mvn dockerfile:build
mvn dockerfile:tag
mvn dockerfile:push
如运行出现 “No plugin found for prefix ‘dockerfile’ in the current project and in the plugin groups [org.apache.maven.plugins”我们需要在settings.xml里把这个插件加入白名单
<settings>
<pluginGroups>
<pluginGroup>com.spotify</pluginGroup>
</pluginGroups>
</settings>
跳过Dockerfile相关的构建、发布
如果要临时跳过所有的Dockerfile相关的所有3个goals,执行如下Maven命令:
mvn clean package -Ddockerfile.skip
如果只是想跳过某一个goal,执行如下Maven命令:
mvn clean package -Ddockerfile.build.skip
mvn clean package -Ddockerfile.tag.skip
mvn clean package -Ddockerfile.push.skip