Jenkins方式構建的定位是使用專門的CICD平台。
既支持把JenKins作為一個Pod部署到openshift內部,也支持部署在Openshift集群外部,操作上的區別是
- openshift自己內部帶的jenkins images包含了openshift的plugin
- Jenkins放在openshift外,需要手工安裝plugin了。具體的安裝過程可以參考
- https://blog.openshift.com/using-openshift-pipeline-plugin-external-jenkins/
為了演示方便仍然直接用openshift里面的imagestream了
jenkins Plugin能直接觸發openshift的構建和部署過程, 同時最吸引的特點是:
- 支持流水線Pipeline這種模式,便於在同一集群的多個項目(對應開發,測試,生產)環境或者多個集群(對應開發集群,Stage集群,生產集群)中進行發布。
- 流水線支持自定義不同的階段,每個階段完成不同的任務,比如可以定義階段為: CI環境部署->Stage環境部署->Prd部署
- 一條流水線支持包含多個微服務,針對項目中包含多個微服務,一旦定制好流水線,就可以重復運行
1.安裝jenkins
CDK安裝完成后暫時直接用里面帶的Jenkins ImageStream
oc new-project ci
然后界面上部署jenkins,如果部署失敗,除了刪除deployment,service,route外,還需要刪除ServiceAccount(jenkins),rolebinding(jenkins_edit)和pvc jenkins
ericdeMacBook-Pro:openshift-tomcat ericnie$ oc get sa; NAME SECRETS AGE builder 2 7h default 2 7h deployer 2 7h jenkins 2 7h ericdeMacBook-Pro:openshift-tomcat ericnie$ oc get rolebinding; NAME ROLE USERS GROUPS SERVICE ACCOUNTS SUBJECTS admin /admin developer jenkins_edit /edit jenkins system:deployers /system:deployer deployer system:image-builders /system:image-builder builder system:image-pullers /system:image-puller system:serviceaccounts:ci ericdeMacBook-Pro:openshift-tomcat ericnie$ oc get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE jenkins Bound pv0012 100Gi RWO,ROX,RWX 7h
部署完成后,通過命令行查看
ericdeMacBook-Pro:openshift-tomcat ericnie$ oc get all NAME REVISION DESIRED CURRENT TRIGGERED BY deploymentconfigs/jenkins 1 1 1 config,image(jenkins:2) NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD routes/jenkins jenkins-ci.192.168.99.100.nip.io jenkins <all> edge/Redirect None NAME READY STATUS RESTARTS AGE po/jenkins-1-f5gk7 1/1 Running 0 7h NAME DESIRED CURRENT READY AGE rc/jenkins-1 1 1 1 7h NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/jenkins ClusterIP 172.30.182.209 <none> 80/TCP 7h svc/jenkins-jnlp ClusterIP 172.30.154.212 <none> 50000/TCP 7h
點擊鏈接跳轉
授權其他項目訪問
出現jenkins主頁面
2.構建項目和Openshift集成
建立一個項目,選擇
在構建處選擇Trigger OpenShift Build,然后填入相關的選項
同時把jenkins用戶加入訪問命名空間的權限
oc policy add-role-to-user edit system:serviceaccount:ci:jenkins -n s2i-tomcat
手工觸發立即構建后,觸發s2i-tomcat項目的構建,同時把鏡像更新到depolyment
3.pipeline流水線構建
建立一個pipeline文件
ericdeMacBook-Pro:pipeline ericnie$ cat s2itomcat-pipeline.yaml apiVersion: v1 kind: BuildConfig metadata: annotations: pipeline.alpha.openshift.io/uses: '[{"name": "s2itomcat", "namespace": "s2i-tomcat", "kind": "DeploymentConfig"}]' creationTimestamp: null labels: app: jenkins-pipeline-development name: s2itomcat-development-pipeline template: application-template-development-pipeline name: s2itomcat-development-pipeline spec: output: {} postCommit: {} resources: {} runPolicy: Serial source: type: None strategy: jenkinsPipelineStrategy: jenkinsfile: |- node('maven') { stage 'build' openshiftBuild(buildConfig: 's2itomcat', showBuildLogs: 'true', namespace: 's2i-tomcat') stage 'deploy' openshiftDeploy(deploymentConfig: 's2itomcat', namespace: 's2i-tomcat') } type: JenkinsPipeline triggers: - github: secret: secret101 type: GitHub - generic: secret: secret101 type: Generic status: lastVersion: 0
切換到jenkins所在的項目ci, 然后通過命令行建立
ericdeMacBook-Pro:pipeline ericnie$ oc create -f s2itomcat-pipeline.yaml buildconfig "s2itomcat-development-pipeline" created
可以看到在jenkins界面上基於OpenShift的CI project創建了一個pipeline
授權
ericdeMacBook-Pro:pipeline ericnie$ oc policy add-role-to-user edit system:serviceaccount:ci:jenkins -n s2i-tomcat role "edit" added: "system:serviceaccount:ci:jenkins"
在openshift console中start pipeline開始構建,完成后可以看見s2itomcat項目deployment的更新。
每次構建jenkins會啟動一個新的maven pod,然后進行構建工作,構建完成后會將pod刪除。
4.一條流水線多組件部署配置
在實際項目中存在一條流水線構建多個組件,同時需要部署多個鏡像和微服務的場景,因此下面這個例子演示一下多組件的流水線構建過程。
還是相同的項目,我們再建立一個組件nodejs-example
ricdeMacBook-Pro:minishift ericnie$ oc new-app https://raw.githubusercontent.com/ericnie2015/nodejs-ex/master/openshift/templates/nodejs-dev.json --> Deploying template "s2i-tomcat/nodejs-example" for "https://raw.githubusercontent.com/ericnie2015/nodejs-ex/master/openshift/templates/nodejs-dev.json" to project s2i-tomcat Node.js --------- An example Node.js application with no database. For more information about using this template, including OpenShift considerations, see https://github.com/openshift/nodejs-ex/blob/master/README.md. The following service(s) have been created in your project: nodejs-example. For more information about using this template, including OpenShift considerations, see https://github.com/openshift/nodejs-ex/blob/master/README.md. * With parameters: * Name=nodejs-example * Namespace=openshift * Memory Limit=512Mi * Git Repository URL=https://github.com/alezzandro/nodejs-ex.git * Git Reference= * Context Directory= * Application Hostname= * GitHub Webhook Secret=gk0fyAeT43s368qQIxkGvoeHbphK7XKkSXOMHKnf # generated * Generic Webhook Secret=4rWULbanlnwLsm8x38S0BACQKxCwfvpnKrARfSUn # generated * Database Service Name= * MongoDB Username= * MongoDB Password= * Database Name= * Database Administrator Password= * Custom NPM Mirror URL= --> Creating resources ... service "nodejs-example" created route "nodejs-example" created imagestream "nodejs-example" created buildconfig "nodejs-example" created deploymentconfig "nodejs-example" created --> Success Access your application via route 'nodejs-example-s2i-tomcat.192.168.99.100.nip.io' Use 'oc start-build nodejs-example' to start a build. Run 'oc status' to view your app.
現在項目中的bc和dc分別包括
ericdeMacBook-Pro:minishift ericnie$ oc get dc NAME REVISION DESIRED CURRENT TRIGGERED BY myapp 1 1 1 config,image(s2itomcat:latest) nodejs-example 0 1 0 s2itomcat 8 1 1 config,image(s2itomcat:latest) ericdeMacBook-Pro:minishift ericnie$ oc get bc NAME TYPE FROM LATEST nodejs-example Source Git 0 s2itomcat Source Git@master 5
修改yaml文件,或者直接在界面上將原有pipeline的jenkinsfile修改成
node('maven') { stage 'build' openshiftBuild(buildConfig: 's2itomcat', showBuildLogs: 'true', namespace: 's2i-tomcat') openshiftBuild(buildConfig: 'nodejs-example', showBuildLogs: 'true', namespace: 's2i-tomcat') stage 'deploy' openshiftDeploy(deploymentConfig: 's2itomcat', namespace: 's2i-tomcat') openshiftDeploy(deploymentConfig: 'nodejs-example', namespace: 's2i-tomcat') }
同時可以選擇Serial或者Parrell的編譯部署模式,然后完成部署。
設置了並行,但還是串行跑,應該是jenkins只provision了一個maven pod的原因。
pipeline跨集群運行的例子參考
https://github.com/openshift/jenkins-client-plugin/