k8s-master tomcat启动jenkins服务
1.验证java环境
1 2 3 4
| [root@k8s-master ~]# java -version openjdk version "1.8.0_292" OpenJDK Runtime Environment (build 1.8.0_292-b10) OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)
|
2.下载tomcat,jenkins安装包
1 2
| wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.69/bin/apache-tomcat-8.5.69.tar.gz wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
|
3.配置环境变量
vim /etc/profile 打开配置文件,按 i 进入编辑模式,在profile文件尾部 unset -f pathmunge 后面添加如下内容:
1 2 3 4 5 6 7 8
| unset i unset -f pathmunge export JAVA_HOME=/usr/local/java/jdk1.8.0_221 export JRE_HOME=${JAVA_HOME}/jre export CATALINA_HOME=/usr/local/tomcat8 export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin:${CATALINA_HOME}/bin export PATH=$PATH:${JAVA_PATH}:$PATH
|
4.配置tomcat并启动jenkins
1 2 3 4 5 6 7
| mkdir /usr/local/tomcat8 tar -zxvf apache-tomcat-8.5.37.tar.gz -C /usr/local/tomcat8 mv jenkins.war /usr/local/tomcat8/webapps #启动tomcat /usr/local/tomcat8/bin/startup.sh #停止tomcat /usr/local/tomcat8/bin/shutdown.sh
|
http://localhost:8080 访问jenkins
自动化脚本
在jenkins的后端准备好自动化部署脚本k8s_auto_deploy.sh,事先做好公私钥认证,使得jenkins能够访问k8s-master
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #!/bin/bash URL=http://106.12.189.57/root/java-demo.git Starttime=`date +"%Y-%m-%d_%H-%M-%S"` Method=$1 Branch=$2 t1=`date +"%Y-%m-%d %H:%M:%S"`
#代码克隆至jenkins后端 clone_code(){ cd /root/.jenkins/workspace/jenkins_project && git clone -b $Branch ${URL}&& echo "Clone Finished" }
#代码打包压缩并远程推送至k8s-master-1的nginx镜像制作目录 Pack_scp(){ cd /root/.jenkins/workspace/jenkins_project/java-demo/ && tar cvzf java-demo.tar.gz * && echo Package Finished cp java-demo.tar.gz /data/Dockerfile/java-demo/ && cd /data/Dockerfile/java-demo/ && tar xvf java-demo.tar.gz && rm -f java-demo.tar.gz }
#远程操作k8s-master-1节点,进行镜像制作并推送至harbor镜像仓库 build_iamge(){ cd /data/Dockerfile/java-demo/ && ./build.sh ${BUILD_NUMBER} && echo 'build_image and push_harbor success!' }
#对k8s集群中的nginx的pod应用进行升级 app_update(){ sed -ri 's@image: .*@image: 106.12.37.109/library/tomcat-java-demo:${BUILD_NUMBER}@g' /data/mainfest/deployment.yaml kubectl set image deployment/java-demo java-demo=106.12.37.109/library/tomcat-java-demo:${BUILD_NUMBER} -n default --record=true t2=`date +"%Y-%m-%d %H:%M:%S"` start_T=`date --date="${t1}" +%s` end_T=`date --date="${t2}" +%s` total_time=$((end_T-start_T)) echo "deploy success,it has been spent ${total_time} seconds" }
#k8s集群中的pod应用进行回滚 app_rollback(){ kubectl rollout undo deployment/java-demo -n default }
#进行k8s集群自动部署的主函数 main(){ case $Method in deploy) clone_code Pack_scp build_iamge app_update ;; rollback) app_rollback ;; esac }
#执行主函数命令 main $1 $2
|
构建镜像代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #!/bin/bash #镜像版本变量 version=$1 #编译项目代码 mvn clean package -Dmaven.test.skip=true #解压项目代码至新文件夹 unzip target/*.war -d target/ROOT #编写Dockerfile cat > Dockerfile <<EOF FROM lizhenliang/tomcat LABEL maintainer alex RUN rm -rf /usr/local/tomcat/webapps/* COPY target/ROOT /usr/local/tomcat/webapps/ROOT EOF #build镜像并推送 docker build -t 106.12.37.109/library/tomcat-java-demo:$version . docker push 106.12.37.109/library/tomcat-java-demo:$version
|
参数化构建
web端访问jenkins
创建一个jenkins_project项目,并编辑配置
将jenkins的公钥配置到gitlab服务的web端,使得jenkins可以直接拉取代码无需认证
或者使用https方式来取代码 ,我测试中用的是https,生产中最好用ssh
后续验证就不写了