?
vi pipeline.yml
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: test
name: pipeline
labels:
app: pipeline
spec:
replicas: 2
selector:
matchLabels:
app: pipeline
template:
metadata:
labels:
app: pipeline
spec:
containers:
- name: pipeline
image: 192.168.88.125:80/repo/devops-pipeline-test:v4.0.0
#镜像拉取策略,不管内容变不变都重新拉取
imagePullPolicy: Always
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
namespace: test
#注意:这里要和Deployment的名称一致
name: pipeline
labels:
app: pipeline
spec:
selector:
#注意:这里要和Deployment的app名称一致
app: pipeline
ports:
#Service的端口
- port: 8081
#容器内的端口
targetPort: 8080
type: NodePort
---
#apiVersion值必须写成networking.k8s.io/v1
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: test
name: pipeline
spec:
#可视化界面配置的ingress名称
ingressClassName: ingress
rules:
#映射的域名
- host: k8s.pipeline.com
http:
paths:
#访问什么样的路径
- path: /
#匹配方式,前缀匹配,即匹配上path配置的斜杠就可以
pathType: Prefix
backend:
#自定用哪个service
service:
name: pipeline
port:
#映射的端口
number: 8081
要想从Harbor仓库中拉取镜像,首先要往主机和从机的daemon.json文件中添加Harbor的地址信息
"insecure-registries":["192.168.88.125:80"],别忘了80端口
vi /etc/docker/daemon.json
添加完之后,重启下docker
systemctl restart docker
要拉取镜像,需要Harbor登录进行拉取,到Kuboard控制台对应的命名空间找到密文菜单,点击创建Secret。
填写对应的信息即可
复制上图中的复制命令在k8s主机上运行
docker login 192.168.88.125:80/ -u admin -p Harbor12345
kubectl apply -f pipeline.yml
浏览器请求服务正常返回,脚本测试通过。
http://192.168.88.126:31603/test/test
先在SpringBoot项目下创建pipeline.yml文件,内容为步骤2中的脚本内容。
接着生成流水线脚本,目标服务器选择k8smaster服务器,文件写pipeline.yml文件。
将流水线脚本粘贴到Jenkinsfile对应的步骤中
在Jenkinsfile文件中,流水线步骤增加如下增加远程执行kubectl命令步骤。
原始脚本
kubectl apply -f /usr/local/k8s/pipeline.yml
#强制刷新deployment,防止pipeline.yml没发生变化时不重新部署
kubectl rollout restart deployment pipeline -n test
生成流线脚本:
sshPublisher(publishers: [sshPublisherDesc(configName: 'k8smaster', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'kubectl apply -f /usr/local/k8s/pipeline.yml', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
粘贴到Jenkinsfile文件对应的步骤中
完整的Jenkinsfile文件内容如下:
//所有的脚本命令都放在pipeline中
pipeline {
//指定任务在哪个集群节点中执行
agent any
//声明全局变量,方便后面使用
environment {
harborUser = 'admin'
harborPassword = 'Harbor12345'
harborAddress = '192.168.88.125:80'
harborRepo = 'repo'
}
stages {
stage('拉取代码') {
steps {
echo '开始拉取git仓库代码……'
checkout scmGit(branches: [[name: '$tag']], extensions: [], userRemoteConfigs: [[url: 'http://192.168.88.125:8929/root/devops-test.git']])
echo '开始拉取git仓库代码完毕。'
}
}
stage('构建项目') {
steps {
echo '开始通过maven构建项目……'
sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
echo '通过maven构建项目完毕'
}
}
stage('代码检查') {
steps {
echo 'TODO 通过SonarQube做代码质量检测'
}
}
stage('制作镜像') {
steps {
echo '通过Docker制作自定义镜像……'
sh '''mv ./target/*.jar ./docker/
docker build -t ${JOB_NAME}:${tag} ./docker/'''
echo '通过Docker制作自定义镜像完毕'
}
}
stage('推送镜像') {
steps {
echo '将自定义对象推送到Harbor仓库……'
sh '''docker login -u ${harborUser} -p ${harborPassword} ${harborAddress}
docker tag ${JOB_NAME}:${tag} ${harborAddress}/${harborRepo}/${JOB_NAME}:${tag}
docker push ${harborAddress}/${harborRepo}/${JOB_NAME}:${tag}
docker image prune -f'''
echo '将自定义对象推送到Harbor仓库完成'
}
}
stage('将yml文件传到k8smaster') {
steps {
echo '将yml文件传到k8smaster……'
sshPublisher(publishers: [sshPublisherDesc(configName: 'k8smaster', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'pipeline.yml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo '将yml文件传到k8smaster完毕'
}
}
stage('远程执行k8smaster的kubectl命令') {
steps {
echo '远程执行k8smaster的kubectl命令……'
sshPublisher(publishers: [sshPublisherDesc(configName: 'k8smaster', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'kubectl apply -f /usr/local/k8s/pipeline.yml', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo '远程执行k8smaster的kubectl命令完毕'
}
}
}
}
执行Jenkins项目构建
构建成功后,去Kuboard可以到对应的Service
?
kubernetes2
DevOps8
Docker4
Jenjins6
架构师5
kubernetes · 目录
上一篇DevOps搭建(十五)-kubernetes部署项目详细步骤