部署前呢,要先把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