提示:本章主要记录各基本环境搭建好后如何配置Jenkins流水线部署微服务到K8s
主机 | 操作系统 | IP | 备注 |
---|---|---|---|
物理主机 | Win11 | 192.168.6.1 | Jenkins、Nexus3、Docker Desktop |
虚拟主机1 | CentOS7 | 192.168.6.11 | k8s-master |
虚拟主机2 | CentOS7 | 192.168.6.12 | k8s-worker |
前提:
1、Git、 Gitee
2、Maven Integration
3、Publish Over SSH、SSH server
4、Docker plugin
注:可能还有一些其它插件
1、选择Pipeline
2、参数化构建,这里配置Git参数
3、用Git拉取Jenkinsfile,用Jenkinsfile来运行流水线脚本
Jenkinsfile内容如下:
pipeline {
agent any
// 声明全局变量
environment{
nexusUserName='admin'
nexusPwd='xxx'
nexusAddr='192.168.6.1:8082'
nexusRepository='repository'
}
stages {
stage('Git拉取代码') {
steps {
checkout scmGit(branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitee-auth', url: 'https://gitee.com/MonochromeContrast/mytest.git']])
echo 'Git拉取代码 - SUCCESS'
}
}
stage('Maven构建项目') {
steps {
bat 'D:\\Maven\\bin\\mvn clean package -DskipTests'
echo 'Maven构建项目 - SUCCESS'
}
}
stage('Docker制作镜像') {
steps {
bat '''move "target\\*.jar" "docker\\"
docker build -t %JOB_NAME%:%tag% .\\docker\\'''
echo 'Docker制作镜像 - SUCCESS'
}
}
stage('推送镜像到Nexus镜像仓库') {
steps {
bat '''docker login -u %nexusUserName% -p %nexusPwd% %nexusAddr%
docker tag %JOB_NAME%:%tag% %nexusAddr%/%nexusRepository%/%JOB_NAME%:%tag%
docker push %nexusAddr%/%nexusRepository%/%JOB_NAME%:%tag%'''
echo '推送镜像到Nexus镜像仓库 - SUCCESS'
}
}
stage('将yml文件推送到k8s-master服务器'){
steps{
sshPublisher(publishers: [sshPublisherDesc(configName: 'k8s-master', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'deploy.yml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo '将yml文件推送到k8s-master服务器 - SUCCESS'
}
}
stage('远程执行k8s部署命令'){
steps{
bat 'ssh [email protected] kubectl apply -f /k8s/myapp/deploy.yml'
}
}
}
}
deploy.yml文件的内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: dev
name: test
labels:
app: test
spec:
replicas: 1
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
imagePullSecrets:
- name: docker-secret
containers:
- name: test
image: 192.168.6.1:8082/repository/test:v6.0.0
imagePullPolicy: Always
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
namespace: dev
labels:
app: test
name: test
spec:
selector:
app: test
ports:
- port: 8081
targetPort: 8080
type: NodePort
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: dev
name: test
spec:
ingressClassName: ingress
rules:
- host: felix.test.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: test
port:
number: 8081
FROM openjdk:11
COPY mytest.jar /usr/local/
WORKDIR /usr/local
CMD java -jar mytest.jar
提示:前面的基础配置跟着做就行,重点要理解Jenkinsfile里写的每一个步骤
主要用Jenkins的片段生成器来生成每一步的脚本:
用指定目录下的maven执行Maven构建项目命令(因为该操作是在jenkins所在win11系统下进行的,所以这里用的是windows批处理命令。如果是centos系统,这里就得相应选择sh)
将制作好的镜像推送到Nexus镜像仓库(这里用%%包起来的是引用的环境变量,nexus镜像仓库信息都配置到了Jenkinsfile文件的环境变量里)、
提示:windows批处理命令里引用变量用%%,shell命令应用变量则是${}
将deploy.yml文件推送到k8s-master服务器(deploy.yml文件是部署k8s的描述性文件)
我们之前已经配置好了Jenkins与远程服务器的连接(k8s-master),而且指定了远程服务器文件夹位置:
6.远程访问虚拟主机1(k8s-master),执行部署服务的命令
提示:这里必须满足两个条件:
(1)、win11能无密码远程访问虚拟主机1(k8s-master)
ssh [email protected]
// 不需要输入密码就是ok的
(2)、jenkins服务所拥有的账户身份必须能无密码远程访问虚拟主机1(要和在命令行测试的账户一致),否则这里有个坑,流水线会卡在这一步。