jenkins-gitlab-harbor-ceph基於Kubernetes的CI/CD運用(一)


注:這部分的學習還是要靠自己多點點 多嘗試嘗試
這部分19年3月份我是玩的很溜的,一年沒用,基本忘光光了。
學習要溫故而知新!

流程拓撲圖

前提准備

部署應用服務

部署kubernetes 集群:https://www.cnblogs.com/zisefeizhu/p/12505117.html

部署ceph集群:https://www.cnblogs.com/zisefeizhu/p/12512377.html

部署harbor:https://www.cnblogs.com/zisefeizhu/p/12329864.html

部署jenkins:https://www.cnblogs.com/zisefeizhu/p/12499084.html

部署gitlab:https://www.cnblogs.com/zisefeizhu/p/12524824.html

檢查集群狀態

kubernetes集群
# kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-1               Healthy   {"health":"true"}   
etcd-2               Healthy   {"health":"true"}   
etcd-0               Healthy   {"health":"true"}   
# kubectl get nodes
NAME         STATUS                     ROLES    AGE     VERSION
20.0.0.201   Ready,SchedulingDisabled   master   4d22h   v1.17.2
20.0.0.202   Ready,SchedulingDisabled   master   4d22h   v1.17.2
20.0.0.203   Ready,SchedulingDisabled   master   4d22h   v1.17.2
20.0.0.204   Ready                      node     4d22h   v1.17.2
20.0.0.205   Ready                      node     4d22h   v1.17.2
20.0.0.206   Ready                      node     4d22h   v1.17.2
# kubectl get pods -n assembly
NAME                              READY   STATUS             RESTARTS   AGE
jenkins-0                         1/1     Running            2          74m
rbd-provisioner-9cf46c856-ngxwn   1/1     Running            1          65m

ceph集群
# ceph -s
......

    health: HEALTH_OK

gitlab創建項目

創建組:**

使用管理員root創建組,一個組里面可以有多個項目分支,可以將開發添加到組里面進行設置權限,不同的組就是公司不同的開發項目或者服務模塊,不同的組添加不同的開發即可實現對開發設置權限的管理。

使用管理員創建項目



免密鑰拉取gitlab 代碼

# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDFLo3T4x0JJeOg7rzTXrjuxQMj+Y39j6wan6lO4nI5Zj5Z7YKy4YUjVy/i3iYTycyvVHCwfds/yGUxdoGIt/IfyGbH4XYtFTdJwh+8y1lBNwYxiGN9103n1JlrK08GjFj73vhSduLATEiUx2mLyVd3D26aE51RrxW+sK1PSN0EzMNPJiJLRDVbOYA8sd+xloWbNOFKJu9ecKzvy57Z8c8ohRnm3xZ23tHZdGdRTfN8LBXkAPUB7jA8SkXOpSMg9qj/BajFf7FPynNQr16AmiIU7F9PDl6JM1fWgs6SQyTezw1CK1oXcIAZLNIMTrtX/vd0ZY+ml+luyuZtLuRIKPRN root@bs-k8s-gitlab


# git clone git@20.0.0.208:web-test/app1.git
正克隆到 'app1'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
接收對象中: 100% (3/3), done.
[root@bs-k8s-gitlab ~]# cd app1/
[root@bs-k8s-gitlab app1]# cat index.html 
Tomcat app1 v1[root@bs-k8s-gitlab app1]# echo zisefeizhu >> index.html 
[root@bs-k8s-gitlab app1]# git add .
[root@bs-k8s-gitlab app1]# git commit -m "Testing gitlab and jenkins Connection #1"
[master f21339a] Testing gitlab and jenkins Connection #1
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@bs-k8s-gitlab app1]# git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 288 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@20.0.0.208:web-test/app1.git
   1198095..f21339a  master -> master

jenkins配置插件

插件安裝

Git plugin        git 
GitLab Plugin     gitlab 
Kubernetes plugin 動態創建代理 
Pipeline          流水線 
Email Extension   郵件擴展
Extended Choice Parameter

gitlab觸發jenkins

gitlab生成token

復制此token,此token只顯示一次:8YJobWVnZri-chf-QakJ

jenkins配置連接gitlab

系統管理 --> 系統設置 --> gitlab

創建jenkins任務

http://20.0.0.202:30006/project/gitlab-citest-pipeline 這個地址用來設置gitlab的webhook。

token: c8124514f0d3f60588a707470be57bca

gitlab設置webhooks

提交代碼至gitlab觸發jenkins任務

# git clone git@20.0.0.208:web-test/app1.git
正克隆到 'app1'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
接收對象中: 100% (6/6), done.
# cd app1/
# echo zisefeizhu >> index.html 
# git add .
# git commit -m "Testing gitlab and jenkins Connection #1"
[master f1005f8] Testing gitlab and jenkins Connection #1
 1 file changed, 1 insertion(+)
# git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 289 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@20.0.0.208:web-test/app1.git
   f21339a..f1005f8  master -> master

顯示任務由gitlab觸發的。

Jenkins Pipeline 及參數化構建

**Jenkins參數化構建流程圖 ** 【網圖】

Jenkins Pipeline是一套插件,支持在Jenkins中實現集成和持續交付管道;

  • Pipeline通過特定語法對簡單到復雜的傳輸管道進行建模;
    1. 聲明式:遵循與Groovy相同語法。pipeline { }
    2. 腳本式:支持Groovy大部分功能,也是非常表達和靈活的工具。node { }
  • Jenkins Pipeline的定義被寫入一個文本文件,稱為Jenkinsfile。
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying'
            }
        }
    }
}

pipeline簡單使用

pipeline {
    agent any
        parameters {
  choice choices: ['20.0.0.208/web-test/app1.git', '20.0.0.208/web-test/app2.git', '20.0.0.208/web-test/app3.git'], description: '請選擇要發布的項目git地址', name: 'git'
  choice choices: ['20.0.0.204', '20.0.0.205', '20.0.0.206'], description: '請選擇要發布的服務器', name: 'host'
            
        }
    stages {
        stage('Build') {
            steps {
                echo "${git}"
            }
        }
        stage('Test') {
            steps {
                echo 'Testing'
            }
        }
        stage('Deploy') {
            steps {
                echo "${host}"
            }
        }
    }
}

構建發布測試

Started by user zisefeizhu
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/gitlab-citest-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
20.0.0.208/web-test/app2.git
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
Testing
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] echo
20.0.0.205
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM