当前位置: 首页 > news >正文

Jenkins+K8s实现持续集成(二)

部署前呢,要先把jenkins搭建好。

同时呢已经有了k8s的环境。

基于以上两步已经有了的情况,继续要实现jenkins+k8s持续集成,需要先准备四个文件:

Dockerfile首先要准备好一个Dockerfile文件,用于构建Docker镜像的文本文件
project.yaml这个是部署应用的yaml文件,名字可以取项目的名字,例如cool-hello.yaml
project-svc.yaml这个文件是将应用的端口和暴露对外的端口进行映射,名字可以是应用名字接svc来命名
jenkins.sh是执行部署的脚本

目录

一、Dockerfile怎么写呢?

二、project.yaml怎么写呢?

三、project-svc.yaml怎么写呢?

四、jenkins.sh脚本如何写?

五、如何在jenkins里面进行配置呢?


一、Dockerfile怎么写呢?

挂载目录和项目名字需要改成自己的。

# 基础镜像
FROM openjdk8-alpine-cst:v1.0.0
# author
MAINTAINER hello# 挂载目录
VOLUME /home/hello
# 创建目录
RUN mkdir -p /home/hello
# 指定路径
WORKDIR /home/hello
# 复制jar文件到路径
COPY ./hello.jar /home/hello/hello.jar
# 启动认证服务
ENTRYPOINT java ${JAVA_OPTS} -Dfile.encoding=utf-8 -jar hello.jar --spring.profiles.active=${SPRING_PROFILES_ACTIVE}

二、project.yaml怎么写呢?

apiVersion: apps/v1
kind: Deployment
metadata:name: hello-deploy #这里可以改成自己项目+deploynamespace: hello #这个命名空间可以改成自己的labels: #本发布app: hello-deploy #这里可以改成自己项目+deployversion: v1.0.0
spec:replicas: 1revisionHistoryLimit: 3selector:matchLabels: #需匹配资源app: hello #这里可以改成自己项目名version: v1.0.0template:metadata:labels: # 资源标签app: hello #这里可以改成自己项目名version: v1.0.0spec: # 资源规范containers:- name: hello #这里可以改成自己项目名image: 镜像地址imagePullPolicy: Alwaysenv:- name: JAVA_OPTSvalue: "-Xmx1024m -Xms512m"- name: SPRING_PROFILES_ACTIVEvalue: "test"resources:limits:cpu: 2memory: 2Girequests:cpu: 200mmemory: 640MilivenessProbe:httpGet:path: /healthCheck #这个地址必须要加入白名单,地址可以换成自己的地址port: 8089 #端口可以改成自己项目的端口scheme: HTTPinitialDelaySeconds: 60timeoutSeconds: 10periodSeconds: 30successThreshold: 1failureThreshold: 5readinessProbe:httpGet:path: /healthCheckport: 8089 #端口可以改成自己项目的端口scheme: HTTPinitialDelaySeconds: 60timeoutSeconds: 10periodSeconds: 30successThreshold: 1failureThreshold: 5ports:- name: httpcontainerPort: 8089 #端口可以改成自己项目的端口protocol: TCPimagePullSecrets:- name: hello-secret #secret的名字,这个是连接镜像仓库的秘钥名字

三、project-svc.yaml怎么写呢?

apiVersion: v1
kind: Service
metadata:name: hello-svc #这里可以改自己的项目名+svcnamespace: hello #这里可以改自己的命名空间
spec:type: NodePort #这里只能填NodePortselector:app: hello #这里可以改自己的项目名ports:- port: 8089 #应用的端口targetPort: 8089 #应用的端口nodePort: 32089 #映射端口,对外暴露的端口protocol: TCPname: restful

四、jenkins.sh脚本如何写?

#!/bin/bash# master pwd and ip
master_pwd='把应用程序推送到目标机器(部署的机器)密码'
master_ip=把应用程序推送到目标机器(部署的机器)的ip# full path
echo jar_path=${WORKSPACE}/${project_dir}${project_name}/target/${project_name}.jarif [ ! -f $WORKSPACE/$project_dir$project_name/target/$project_name.jar ];thenecho -------------------file not exists,path:${WORKSPACE}/${project_name}/target/${project_name}.jar-----------------------exit 1
fiecho cd ${WORKSPACE}/${project_dir}${project_name}/target/ #进入到该目录,下面会将Dockerfile复制到这个目录
cd $WORKSPACE/$project_dir$project_name/target/echo copy Dockerfile to target project
echo cp ${WORKSPACE}/${project_name}/k8s/* . #将Dockerfile文件复制到$WORKSPACE/$project_dir$project_name/target/
cp $WORKSPACE/$project_name/k8s/{project_name}/* .
#cp $WORKSPACE/k8s/$project_name/Dockerfile .# delete nerdctl images by id
image_id=$(nerdctl images | grep "${group_name}/${project_name}" | awk '{print $3}')
echo image_id=${image_id}
# if exists 
if [ -n "$image_id" ]; thenecho '--------------------rm images--------------------'echo nerdctl rmi -f ${group_name}/${project_name}:${version}nerdctl rmi -f ${group_name}/${project_name}:${version}
fi# delete none images
image_ids=$(nerdctl images | grep none | awk '{print $3}')
echo image_ids=${image_ids}
if [ -n "$image_ids" ]; thenecho '--------------------rm images--------------------'nerdctl images | grep nonenerdctl images | grep none | awk '{print $3}' | xargs nerdctl rmi
fiecho nerdctl build -t ${group_name}/${project_name}:${version} .
nerdctl build -t ${group_name}/${project_name}:${version} .echo nerdctl login #后面写镜像地址,登录到镜像
nerdctl login 镜像地址 -u 账号 -p 密码echo nerdctl push ${group_name}/${project_name}:${version}#将打包好的镜像推送到镜像仓库
nerdctl push ${group_name}/${project_name}:${version}echo sleep 5
sleep 5echo kubectl rollout restart deployment ${project_name}-deploy -n ${namesapce}#部署deploy
sshpass -p ${master_pwd} ssh root@${master_ip} "kubectl rollout restart deployment ${project_name}-deploy -n ${namesapce}"echo sleep 5
sleep 5echo delay 5 seconds, search: kubectl get pods -n ${namesapce} ...

五、如何在jenkins里面进行配置呢?

#!/bin/bash# set jenkins var, for jenkins will kill me .
BUILD_ID=dontKillMe# build project poms.
echo project build start,path:$WORKSPACE/pom.xml
mvn clean install -DskipTests -f $WORKSPACE/pom.xml# 分组名称(同一分组不用更改)
group_name='这里可以放镜像地址加命名空间,比如镜像地址是:hello.com/train'
# K8S命名空间名称(同一空间或项目不用更改)
namesapce='train'
# 项目目录,如果为空就不需要填写
project_dir=''
# 项目名称(必改)
project_name='hello'
# app version
version='v1.0.0'
# auto script path
auto_script_path='/home/docker/auto/srcipt/jenkins.sh'echo '--------------------jenkins build begin--------------------'
if [ -f $auto_script_path ];then# call auto script echo begin call linux auto script, filepath=$auto_script_pathsource $auto_script_path
else # auto script files not existsecho auto script files not exists, service interrupt, please check...echo filepath=$auto_script_pathexit 1
fi

相关文章:

  • [数据集][目标检测]药片药丸检测数据集VOC+YOLO格式152张1类别
  • 理解堆排序
  • Golang中的CAS操作
  • 算法训练营第六十七天 | 卡码网110 字符串接龙、卡码网105 有向图的完全可达性、卡码网106 岛屿的周长
  • 【操作系统】第五章 文件系统
  • odoo的采购询价单,默认情况下显示‘draft‘,‘sent‘,‘purchase‘,请问什么情况下才会显示‘to approve‘?
  • clean code-代码整洁之道 阅读笔记(第十一章)
  • 静态ip详解
  • Android面试题精选——再聊Android-Handler机制
  • 分类接口开发
  • [SAP ABAP] 排序内表数据
  • 计组--存储系统--复习专用...
  • 【iOS】#include、#import、@class、@import
  • 2024广东省职业技能大赛云计算赛项实战——Minio服务搭建
  • CTFHUB-SSRF-端口扫描
  • 78. Subsets
  • download使用浅析
  • ES6核心特性
  • JavaScript的使用你知道几种?(上)
  • Java面向对象及其三大特征
  • node-glob通配符
  • Python 使用 Tornado 框架实现 WebHook 自动部署 Git 项目
  • Quartz实现数据同步 | 从0开始构建SpringCloud微服务(3)
  • React16时代,该用什么姿势写 React ?
  • Solarized Scheme
  • springMvc学习笔记(2)
  • vue自定义指令实现v-tap插件
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 仿天猫超市收藏抛物线动画工具库
  • 猴子数据域名防封接口降低小说被封的风险
  • 经典排序算法及其 Java 实现
  • 强力优化Rancher k8s中国区的使用体验
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 实习面试笔记
  • 用Python写一份独特的元宵节祝福
  • 完善智慧办公建设,小熊U租获京东数千万元A+轮融资 ...
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • (3)(3.5) 遥测无线电区域条例
  • (搬运以学习)flask 上下文的实现
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (新)网络工程师考点串讲与真题详解
  • (续)使用Django搭建一个完整的项目(Centos7+Nginx)
  • (转)Android学习笔记 --- android任务栈和启动模式
  • (转)linux下的时间函数使用
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .NET Core工程编译事件$(TargetDir)变量为空引发的思考
  • .NET CORE使用Redis分布式锁续命(续期)问题
  • .NET 读取 JSON格式的数据
  • .NET(C#) Internals: as a developer, .net framework in my eyes
  • .NET/C# 在 64 位进程中读取 32 位进程重定向后的注册表
  • .net之微信企业号开发(一) 所使用的环境与工具以及准备工作
  • .sdf和.msp文件读取
  • :not(:first-child)和:not(:last-child)的用法
  • @RequestParam,@RequestBody和@PathVariable 区别
  • @SuppressLint(NewApi)和@TargetApi()的区别