一、gitlab

1.1 登录gitlab创建项目

项目名称等自定义,权限可以选择私有或者公有

1.2 gitlab 代码提交

创建好的项目下复制项目地址

#在gitlab服务器上创建一个目录[root@k8s-node2 local]# cd test/[root@k8s-node2 test]# lltotal 0[root@k8s-node2 test]# pwd/usr/local/test[root@k8s-node2 test]# ll -latotal 0drwxr-xr-x   2 root root   6 Oct 31 09:45 .drwxr-xr-x. 16 root root 206 Oct 31 09:45 ..#克隆项目[root@k8s-node2 test]# git@192.168.198.147:root/xmls.git-bash: git@192.168.198.147:root/xmls.git: No such file or directory[root@k8s-node2 test]# git clone git@192.168.198.147:root/xmls.gitCloning into 'xmls'...remote: Enumerating objects: 79, done.remote: Counting objects: 100% (44/44), done.remote: Compressing objects: 100% (26/26), done.remote: Total 79 (delta 22), reused 32 (delta 16), pack-reused 35Receiving objects: 100% (79/79), 104.66 MiB | 11.57 MiB/s, done.Resolving deltas: 100% (33/33), done.#项目xmls已经克隆下来[root@k8s-node2 test]# ll -latotal 0drwxr-xr-x   3 root root  18 Oct 31 09:47 .drwxr-xr-x. 16 root root 206 Oct 31 09:45 ..drwxr-xr-x   3 root root  51 Oct 31 09:47 xmls[root@k8s-node2 test]# cd xmls/[root@k8s-node2 xmls]# ll -latotal 8drwxr-xr-x 3 root root   35 Oct 31 09:48 .drwxr-xr-x 3 root root   18 Oct 31 09:47 ..drwxr-xr-x 8 root root  163 Oct 31 09:47 .git-rw-r--r-- 1 root root 6183 Oct 31 09:47 README.md#查询分支[root@k8s-node2 xmls]# git branch -a* main  remotes/origin/HEAD -> origin/main  remotes/origin/main#创建xmzw分支[root@k8s-node2 xmls]# git branch xmzw[root@k8s-node2 xmls]# git branch -a* main  xmzw  remotes/origin/HEAD -> origin/main  remotes/origin/main  remotes/origin/xmzw#切换到xmzw分支[root@k8s-node2 xmls]# git checkout xmzwD       xmtb.txtSwitched to branch 'xmzw'[root@k8s-node2 xmls]# git branch -a  main* xmzw  remotes/origin/HEAD -> origin/main  remotes/origin/main  remotes/origin/xmzw#创建test文件,测试文件上传是否正常[root@k8s-node2 xmls]# touch test.txt#给test文件添加标志[root@k8s-node2 xmls]# git add test.txt #添加提交信息[root@k8s-node2 xmls]# git commit -m "test commit"[xmzw 6aa912c] test commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.txt #提交,第一次提交提示报错。这是因为远程repository和本地的repository有冲突[root@k8s-node2 xmls]# git pushTo git@192.168.198.147:root/xmls.git ! [rejected]        xmzw -> xmzw (non-fast-forward)error: failed to push some refs to 'git@192.168.198.147:root/xmls.git'hint: Updates were rejected because the tip of your current branch is behindhint: its remote counterpart. Merge the remote changes (e.g. 'git pull')hint: before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.#解决上面报错,需要先pull拉取远程分支文件,在重新提交即可[root@k8s-node2 xmls]# git pull origin xmzwFrom 192.168.198.147:root/xmls * branch            xmzw       -> FETCH_HEADMerge made by the 'recursive' strategy. Jenkinsfile     |  35 +++++++++++++++++++++++++++++++++++ copy.sh         |  48 ++++++++++++++++++++++++++++++++++++++++++++++++ deploy.yaml     |  42 ++++++++++++++++++++++++++++++++++++++++++ harbor.sh       |  32 ++++++++++++++++++++++++++++++++ xm_nginx.tar.gz | Bin 0 -> 146217984 bytes xmyd.txt        |   3 +++ 6 files changed, 160 insertions(+) create mode 100644 Jenkinsfile create mode 100644 copy.sh create mode 100644 deploy.yaml create mode 100644 harbor.sh create mode 100644 xm_nginx.tar.gz create mode 100644 xmyd.txt[root@k8s-node2 xmls]# [root@k8s-node2 xmls]# [root@k8s-node2 xmls]# lltotal 142820-rw-r--r-- 1 root root      1352 Oct 31 10:01 copy.sh-rw-r--r-- 1 root root       760 Oct 31 10:01 deploy.yaml-rw-r--r-- 1 root root       852 Oct 31 10:01 harbor.sh-rw-r--r-- 1 root root      2272 Oct 31 10:01 Jenkinsfile-rw-r--r-- 1 root root      6183 Oct 31 09:47 README.md-rw-r--r-- 1 root root         0 Oct 31 09:52 test.txt-rw-r--r-- 1 root root 146217984 Oct 31 10:01 xm_nginx.tar.gz-rw-r--r-- 1 root root        19 Oct 31 10:01 xmyd.txt#重新提交,可以看到test文件已经成功上传[root@k8s-node2 xmls]# git pushCounting objects: 6, done.Delta compression using up to 2 threads.Compressing objects: 100% (4/4), done.Writing objects: 100% (4/4), 501 bytes | 0 bytes/s, done.Total 4 (delta 2), reused 0 (delta 0)remote: remote: To create a merge request for xmzw, visit:remote:   http://192.168.198.147/root/xmls/-/merge_requests/new?merge_request%5Bsource_branch%5D=xmzwremote: To git@192.168.198.147:root/xmls.git   bf35535..5455143  xmzw -> xmzw

二、Jenkins

2.1 Jenkins创建流水线项目

比如创建任务名称:pipeline的流水线项目,点击确定

2.2 项目配置

点击pipeline任务,点击配置

1. 选择scm,配置gitlab仓库地址,这里要复制http的地址。

2. 勾选参数化构建过程,配置版本为传参

3. 点击流水线语法,会自动转换成Jenkins脚本命令

三、流水线部署

3.1 在gitlab上创建一个Jenkins文件

内容如下:

大概流程:从git仓库拉取代码-发送目标服务器-上传镜像到harbor-使用k8s部署业务

pipeline {    agent any    stages {        stage('从git仓库拉取代码') {            steps {                checkout([$class: 'GitSCM', branches: [[name: '*/xmzw']], extensions: [], userRemoteConfigs: [[credentialsId: '8f6e9bea-7734-43d8-a6a2-d54e233fa9ff', url: 'http://192.168.198.147/root/xmls.git']]])                echo 'H从git仓库拉取代码成功'            }        }stage('发送目标服务器') {            steps {                sshPublisher(publishers: [sshPublisherDesc(configName: 'ssh', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '*tar.gz,*sh,*yaml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])                echo '发送目标服务器成功'            }        }stage('上传镜像') {            steps {               sshPublisher(publishers: [sshPublisherDesc(configName: 'ssh', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''ssh root@192.168.198.144cd /usr/local/devopssh harbor.sh $tag''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])                echo '上传镜像成功'            }        }stage('部署业务') {            steps {                sshPublisher(publishers: [sshPublisherDesc(configName: 'ssh', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''ssh root@192.168.198.144cd /usr/local/devopskubectl apply -f deploy.yamlkubectl rollout restart -n my-ns-pipeline deployment my-deploy''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])                echo '部署业务成功'            }        }    }}

3.2 推送镜像的脚本

[root@k8s-node2 xmls]# cat harbor.sh #/bin/bashimageTag=$1echo > $imageTagcontainerName=xm_nginx:${imageTag}echo containerName: ${containerName}cd /usr/local/devopsscp xm_nginx.tar.gz root@192.168.198.146:/usr/local/devops/scp xm_nginx.tar.gz root@192.168.198.147:/usr/local/devops/imageID=`docker images | grep xm_nginx |grep -v grep| awk '{print $3}'`hostImageTag=`docker images | grep xm_nginx |grep -v grep| awk '{print $2}'`echo imageID:$imageID   hostImageTag:$hostImageTag#删除旧镜像if [[ "$hostImageTag" =~ "$imageTag" ]] ; then   docker rmi -f ${imageID}  echo "Delete Image ${imageID} Success"fi#加载镜像docker load -i xm_nginx.tar.gzecho "load Success"#打标签docker tag xm_nginx:$imageTag 192.168.198.144:80/xmzw/xm_nginx:latestdocker login 192.168.198.144:80 -u admin -p Harbor12345docker push 192.168.198.144:80/xmzw/xm_nginx:latest

3.3 业务部署的yaml文件

[root@k8s-node2 xmls]# cat deploy.yaml apiVersion: apps/v1kind: Deploymentmetadata:  creationTimestamp: null  labels:    app: my-deploy  name: my-deploy  namespace: my-ns-pipelinespec:  replicas: 1  selector:    matchLabels:      app: my-deploy  template:    metadata:      creationTimestamp: null      labels:        app: my-deploy    spec:      containers:      - image: 192.168.198.144:80/xmzw/xm_nginx:latest        imagePullPolicy: Always        name: xm-nginx-hzsn5        ports:        - containerPort: 80          name: web          protocol: TCP---apiVersion: v1kind: Servicemetadata:  name: my-svc  namespace: my-ns-pipelinespec:  ports:  - port: 8081    name: http    targetPort: 80    nodePort: 31761  selector:    app: my-deploy  type: NodePort

3.4 把deploy.yaml,harbor.sh,Jenkinsfile都同步到gitlab仓库

xm_nginx.tar.gz为Nginx的1.23.1的镜像包,网上下载一个即可

3.5 gitlab上创建tag

3.6 构建

选择1.23.1,点击构建

成功构建

3.7 测试访问正常