jenkins+pipeline+kubernetes部署java服务
脚本文件编写1、查询模板脚本情况[root@k8s-jenkins jenkins_home]# ls model/build.shdeploy.yamldockerfileleaf.properties2、查询dockerfile文件[root@k8s-jenkins jenkins_home]# cat model/dockerfileFROM java:8VOLUME /appCOPY IN
·
脚本文件编写
1、查询模板脚本情况
[root@k8s-jenkins jenkins_home]# ls model/
build.sh deploy.yaml dockerfile leaf.properties
2、查询dockerfile文件
[root@k8s-jenkins jenkins_home]# cat model/dockerfile
FROM java:8
VOLUME /app
COPY INAME.jar INAME.jar
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
RUN bash -c "touch /INAME.jar"
EXPOSE 8085
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./uranedom","-jar","-Dspring.profiles.active=test", "-Dserver.port=8085", "-Duser.timezone=GMT+08", "/INAME.jar"]
3、查看build.sh文件
[root@k8s-jenkins jenkins_home]# cat model/build.sh
#!/bin/bash
#DOCKER_HOME=/var/jenkins_home/tools/docker
#export PATH=$PATH:$DOCKER_HOME
docker build -t $1:$2 .
# 制作镜像
docker tag $1:$2 harbor-test.sgpexchange.com/sgpexchange_test/$1:$2 # 镜像做tar标记
docker login -u admin -p 12345 harbor-test.sgpexchange.com
docker push harbor-test.sgpexchange.com/sgpexchange_test/$1:$2 # 将镜像上传致我们的镜像库,一般都是私有镜像库
4、文件中的leaf.properties文件为服务配置文件
jenkins配置
1、配置丢弃构建
2、配置构建发布参数
3、配置git参数
4、配置java服务名称
5、配置环境参数
6、配置Pipeline Script
其中需要配置gitlab账号参数(credentialsId: ‘c5d7e5af-f442-44ed-98ff-d7a097cd4f49’)
pipeline script脚本内容
// 部署远程主机ip地址,需要通过密钥的方式设置免密登录
def remoteIp = "192.168.1.214"
def remotePort='22'
// 远程tomcat位置(windows要使用/来表示路径)
def tomcatPath = "/var/jenkins_home/image/${envir}"
def project='leaf-server'
def service='test'
def v='123'
def img=''
node (){ //指定Slave标签
stage('Git Checkout') {
if("Deploy" == env.status){
checkout([$class: 'GitSCM', branches: [[name: '${tag}']],
userRemoteConfigs: [[credentialsId: 'c5d7e5af-f442-44ed-98ff-d7a097cd4f49',
url: 'http://gitlab.tqxd.com/new-sgp/leaf.git']]])
}else{
echo "回滚版本跳过拉取代码";
}
}
stage('Maven Build') {
if("Deploy" == env.status){
sh '''
MAVEN_HOME=/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven3.6.3
export PATH=$MAVEN_HOME/bin:/var/jenkins_home/tools/jdk1.8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
mvn clean install -am -Dmaven.test.skip=true
'''
}else{
echo "回滚版本跳过拉取代码";
}
}
stage('push') {
if("Deploy" == env.status){
echo "pushde时间:${v}"
sh '''
mkdir -p /var/jenkins_home/image/${envir}/
cp /var/jenkins_home/model/dockerfile /var/jenkins_home/image/${envir}/
cp /var/jenkins_home/model/build.sh /var/jenkins_home/image/${envir}
mkdir -p /var/jenkins_home/image/${envir}/${name}-conf/
cp /var/jenkins_home/model/leaf.properties /var/jenkins_home/image/${envir}/${name}-conf/
cp /var/jenkins_home/model/deploy.yaml /var/jenkins_home/image/${envir}/${name}.yaml
sed -i 's/model/'${name}'/g' /var/jenkins_home/image/${envir}/${name}.yaml
sed -i 's/version/'${envir}'/g' /var/jenkins_home/image/${envir}/${name}.yaml
sed -i 's/INAME/'${name}'/g' /var/jenkins_home/image/${envir}/dockerfile
cp /var/jenkins_home/workspace/${JOB_NAME}/${name}/target/*.jar /var/jenkins_home/image/${envir}/${name}.jar
'''
v = sh(script: "date +%Y%m%d.%H%M%S", returnStdout: true).trim()
sh(script: "cd /var/jenkins_home/image/${envir}/ && ./build.sh ${name} ${envir}", returnStdout: true).trim()
img = "harbor-test.sgpexchange.com/${name}:v${envir}"
}else{
echo "回滚版本跳过拉取代码";
}
}
stage('publish') {
if("Rollback" == env.status){
img = env.Deploy
}
sh '''
pwd
'''
echo "end的时间出:${v}"
sh(script: "cd /var/jenkins_home/image/test && ./build.sh ${name} ${envir}", returnStdout: true).trim()
sh '''
echo "end img:${img}"
'''
}
stage('end') {
if("Rollback" == env.status){
img = env.Deploy
}
sh(script: "scp -i /var/jenkins_home/.ssh/id_rsa.pem /var/jenkins_home/image/${envir}/${name}.yaml root@192.168.1.214:/root/", returnStdout: true).trim()
sh(script: "scp -i /var/jenkins_home/.ssh/id_rsa.pem -r /var/jenkins_home/image/${envir}/${name}-conf/ root@192.168.1.214:/root/", returnStdout: true).trim()
sh(script: "ssh -i /var/jenkins_home/.ssh/id_rsa.pem -t root@192.168.1.214 'kubectl replace --force -f '${name}'.yaml'", returnStdout: true).trim()
}
}
7、构建job查看job运行情况
k8s服务pods查询
在k8s服务中查看各种信息
1、查询pods服务
[root@k8s-master ~]# kubectl get pods -n k8s-test -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
leaf-server-6c8b4d8454-r2blv 1/1 Running 0 90m 10.244.3.58 k8s-node3 <none> <none>
2、查询service服务
[root@k8s-master ~]# kubectl get svc -n k8s-test -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
leaf-server-service NodePort 10.109.121.210 <none> 8085:31079/TCP 91m app=leaf-server
3、查询deployment服务
[root@k8s-master ~]# kubectl get deployment -n k8s-test -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
leaf-server 1/1 1 1 92m leaf-server harbor-test.sgpexchange.com/sgpexchange_test/leaf-server:test app=leaf-server
4、查询configmap服务
[root@k8s-master ~]# kubectl get configmap -n k8s-test -o wide
NAME DATA AGE
kube-root-ca.crt 1 3d1h
leaf-server-config 1 3d
测试
1、通过nodeport地址测试
http://192.168.1.214:31079/api/segment/get/leaf_ransom_order_id
2、宿主服务测试
至此服务部署完毕
更多推荐
已为社区贡献26条内容
所有评论(0)