1、在项目根目录加上Dockerfile
Dockerfile内容
FROM tomcat
#VOLUME ["/home/data/sts/media","/usr/data/sts/media"]
#定义时区参数
ENV TZ=Asia/Shanghai
#设置时区
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
&& echo '$TZ' > /etc/timezone
ARG JAR_PATH=./hwyg-sts-console/target
RUN rm -rf /usr/local/tomcat/webapps/ROOT
#因为打包成root 所以拼接URL的时候不用加项目名称为根目录
ADD ${JAR_PATH}/*.war /usr/local/tomcat/webapps/ROOT.war
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
EXPOSE 8080
2、在项目根目录建立k8s文件夹,加入以下两个文件
deployment.json内容
{
"kind": "Deployment",
"apiVersion": "apps/v1",
"metadata": {
"name": "${APP_NAME}-deployment",
"namespace": "xxxx",
"labels": {
"k8s-app": "${APP_NAME}"
}
},
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"k8s-app": "${APP_NAME}"
}
},
"template": {
"metadata": {
"name": "${APP_NAME}",
"labels": {
"k8s-app": "${APP_NAME}"
}
},
"spec": {
"containers": [
{
"name": "app",
"image": "hfdockerhub.com.cc:1443/hfocean/jenkins-${APP_NAME}:${BUILD_NUMBER}",
"resources": {},
"imagePullPolicy": "IfNotPresent",
"securityContext": {
"privileged": false,
"procMount": "Default"
},
"ports":
[{ "containerPort": 8080,
"targetPort": 8080
}],
"volumeMounts":[
]
}
],
"volumes":[],
"type": "NodePort",
"restartPolicy": "Always",
"terminationGracePeriodSeconds": 30,
"dnsPolicy": "ClusterFirst",
"securityContext": {},
"schedulerName": "default-scheduler"
}
},
"strategy": {
"type": "RollingUpdate",
"rollingUpdate": {
"maxUnavailable": "25%",
"maxSurge": "25%"
}
},
"revisionHistoryLimit": 10,
"progressDeadlineSeconds": 600
}
}
service.json内容
{
"apiVersion":"v1",
"kind":"Service",
"metadata":{
"namespace":"xxxx",
"name":"${APP_NAME}-service",
"labels":{
"k8s-service":"${APP_NAME}"
}
},
"spec":{
"selector":{
"k8s-app": "${APP_NAME}"
},
"ports":[
{"port":8080,"targetPort":8080}
],
"type":"NodePort"
}
}
一般这两个文件内容无需修改,只要修改跟定的namespace就好了,name中的APP_NAME是自定义的环境变量,一般在jekins配置好了
3、jenkins配置
配置环境变量
配置k8s脚本
其中BUILD_NUMBER是JENKINS中可用的环境变量,APP_NAME是自定义的环境变量
docker build -t hfdockerhub.com.cc:1443/xxxx/jenkins-${APP_NAME}:${BUILD_NUMBER} --build-arg JAR_PATH=./xxxxxx/target .
docker push hfdockerhub.com.cc:1443/xxxxx/jenkins-${APP_NAME}:${BUILD_NUMBER}
在jekins配置
配置完成后保存,然后构建项目
4、到k8s上面查看是否已经部署成功
在jenkins中执行构建后,我们可以在dashboard中看到刚才部署的内容
在“服务”一栏,可以看到由service.yaml创建的服务,这个时间可以看到对外端口为30188
根据此端口就可以访问了