Jenkins安装与构建生产线

本案例首先需要:

Docker安装,请参考https://www.cnblogs.com/heho/p/16924935.html

K8S环境搭建,请参考https://www.cnblogs.com/heho/p/16925275.html 

Gogs源代码管理,请参考 https://www.cnblogs.com/heho/p/16925571.html

Harbor私有镜像仓库,请参考https://www.cnblogs.com/heho/p/16925132.html

一、Jenkins挂载与初始化

挂载宿主机docker目录,使Jenkins可使用宿主机docker命令

docker run -d -u root -v /var/jenkins:/var/jenkins_home -v '/usr/bin/docker:/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock -p 18080:8080 -p 15000:5000 --name myjenkins --restart=always --privileged Jenkins/Jenkins

查看Jenkins生成的文件password,登录

docker exec -i 7c9e526e2845 cat /var/jenkins_home/secrets/initialAdminPassword

 

 

 

 

下一步,选择需要插件,安装

 

从高级选项卡,修改hudson.model.UpdateCenter.xml里的加速镜像

https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

 

二、配置harbor密钥到K8S,使K8S登录Harbor拉取镜像

方法1:

在k8s Master节点登录harbor

docker login 192.168.137.12:80 -u admin -p 123456

查看密钥数据

cat ~/.docker/config.json

Base64加密

cat ~/.docker/config.json|base64 -w 0

编写yaml文件,data节点

apiVersion: v1

kind: Secret

metadata:

name: mysecret2

type: kubernetes.io/dockerconfigjson

data:

.dockerconfigjson: ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjEzNy4xMjo4MCI6IHsKCQkJImF1dGgiOiAiWVdSdGFXNDZNVEl6TkRVMiIKCQl9Cgl9Cn0=

 

方法2

直接在K8S上使用命令生成

kubectl create secret docker-registry harbor-secret -secret --docker-server=192.168.137.12:80 --docker-username=admin --docker-password=123456

Deployment配置使用密钥,

三、安装Publish over SSH,并设置如下,

配置通过SSH到K8S master节点,执行K8S服务命令

最后的Pipeline脚本,如下

  1. 从Gogs签出代码
  2. 构建镜像
  3. 发布镜像至harbor
  4. SSH登录至K8S,拉取镜像,执行部署deployment和Service

 

pipeline {

agent any

stages {

stage("checkout"){

steps{

checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'http://192.168.137.10:13001/whh/net5.git']]])

}

}

stage('build images') {

steps {

sh 'docker build -t 192.168.137.12:80/test/net6api:v1 .'

}

}

stage('pull harbor') {

steps {

sh 'docker login 192.168.137.12:80 -u admin -p 123456'

sh 'docker push 192.168.137.12:80/test/net6api:v1'

 

}

}

stage('pull k8s') {

steps {

sshPublisher(publishers: [sshPublisherDesc(configName: 'k8s master', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'kubectl apply -f /root/deployment.yaml', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'deployment.yaml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])

}

 

}

}

}

 

Jenkins构建成功

 

四、测试

1.查看K8S是否存在相应的Pod

kubectl get pod -owide

2.chrome测试,确认Service是否连接

 

posted @ 2022-11-26 12:55  CODE物  阅读(100)  评论(0编辑  收藏  举报