OpenShift 4 Hands-on Lab (5) - 用Jenkins Pipeline实现在不同运行环境中升迁部署应用

在DevOps过程中CI/CD是最主要的工具,通过它的Pipeline可以驱动整个开发、测试、交付和部署过程。在OpenShift 4.x中自带两个CI/CD引擎:Jenkins和Tekton。其中Jenkins是目前最为常用的CI/CD工具,而Tekton是是直接运行在Kubernetes上的原生CI/CD。我们在本章节介绍如何部署运行一套Jenkins的环境,进而利用Pipeline将应用在开发、测试和生产环境中进行升迁部署。OpenShift 4 Hands-on Lab (5) - 用Jenkins Pipeline实现在不同运行环境中升迁部署应用_第1张图片

  1. 执行以下命令创建相关项目。其中一个项目用来运行Jenkins的Pipeline,其它项目用来模拟3个阶段的应用环境。
$ export USERID=USER-ID
$ oc new-project ${USERID}-pipeline-jenkins
$ oc new-project ${USERID}-pipeline-dev
$ oc new-project ${USERID}-pipeline-test
$ oc new-project ${USERID}-pipeline-prod
  1. 执行以下命令,在USER-ID-cicd项目中根据OpenShift预制的模板创建Jenkins运行环境。
$ oc new-app jenkins-ephemeral -n ${USERID}-pipeline-jenkins
  1. 执行执行命令,查看cicd 的pod创建进度是否问running。
$ oc get pod -w -n ${USERID}-pipeline-jenkins
  1. 查看Jenkins的访问route,然后用浏览器访问它。
$ oc get route jenkins -n ${USERID}-pipeline-jenkins -o template --template '{{.spec.host}}'
  1. 执行以下命令,允许USER-ID-pipeline-jenkines项目的Jenkins系统账号可以访问其它项目,同时允许USER-ID-pipeline-test和USER-ID-pipeline-prod系统账号从USER-ID-pipeline-dev项目中拉取镜像。
$ oc policy add-role-to-user edit system:serviceaccount:${USERID}-pipeline-jenkins:jenkins -n ${USERID}-pipeline-dev
$ oc policy add-role-to-user edit system:serviceaccount:${USERID}-pipeline-jenkins:jenkins -n ${USERID}-pipeline-test
$ oc policy add-role-to-user edit system:serviceaccount:${USERID}-pipeline-jenkins:jenkins -n ${USERID}-pipeline-prod
$ oc policy add-role-to-group system:image-puller system:serviceaccounts:${USERID}-pipeline-test -n ${USERID}-pipeline-dev
$ oc policy add-role-to-group system:image-puller system:serviceaccounts:${USERID}-pipeline-prod -n ${USERID}-pipeline-dev
  1. 执行以下命令,在USER-ID-pipeline-dev项目中创建myapp测试应用,然后可通过route验证应用可访问。
$ oc new-app --name=myapp openshift/php:7.1~https://github.com/liuxiaoyu-git/cotd.git -n ${USERID}-pipeline-dev
  1. 查看pod状态,当运行myapp的pod为Running状态后在创建route,最后用浏览器访问route。
$ oc get pod -n ${USERID}-pipeline-dev -w
NAME             READY   STATUS      RESTARTS   AGE
myapp-1-build    0/1     Completed   0          2m45s
myapp-1-deploy   0/1     Completed   0          36s
myapp-1-fgh9v    1/1     Running     0          27s
$ oc expose service myapp -n ${USERID}-pipeline-dev
$ oc get route myapp -n ${USERID}-pipeline-dev -o template --template '{{.spec.host}}'
  1. 页面会随机显示一个动物的照片。
  2. 执行命令,禁止自动deployment。
$ oc get dc myapp -o yaml -n ${USERID}-pipeline-dev | sed 's/automatic: true/automatic: false/g' | oc replace -f -
deploymentconfig.apps.openshift.io/myapp replaced
  1. 下载https://raw.githubusercontent.com/liuxiaoyu-git/OpenShift-HOL/master/jenkins-pipeline-demo1.yaml文件。
$ curl -LO https://raw.githubusercontent.com/liuxiaoyu-git/OpenShift-HOL/master/jenkins-pipeline-demo1.yaml
  1. 将jenkins-pipeline-demo1.yaml文件中的“USERID变量内容改为自己的用户名。另外还可确认这个Pipeline中包括2个stage,一个用来做build操作,一个用来做deploy操作。
sPipeline
def USERID  = "USER-ID"
def devProj = "$USERID-pipeline-dev"
pipeline {
  agent any
  stages{
    stage("Build") {
      steps{
        script{
          openshift.withCluster() {
            openshift.withProject("${devProj}") {
              echo '*** Build Starting ***'
              openshift.selector('bc', 'myapp').startBuild("--wait").logs('-f')
              echo '*** Build Complete ***'
            }
          }
        }
      }
    }
    stage("Deploy and Verify in Development Env"){
      steps{
        script{
          openshift.withCluster() {
            openshift.withProject("${devProj}") {
              echo '*** Deployment Starting ***'
              openshift.selector('dc', 'myapp').rollout().latest()
              echo '*** Deployment Complete ***'
            }
          }
        }
      }
    }
  }
}
  1. 然后执行以下命令,在USER-ID-pipeline-jenkins项目中创建BuildConfig,然后启动基于Pipeline的构建。
$ oc create -f jenkins-pipeline-demo1.yaml -n ${USERID}-pipeline-jenkins
start-build jenkins-pipeline-dembuildconfig.build.openshift.io/jenkins-pipeline-demo created
$ oc start-build jenkins-pipeline-demo -n ${USERID}-pipeline-jenkins
build.build.openshift.io/jenkins-pipeline-demo-1 started
  1. 在OpenShift控制台上进入Developer视图的Builds菜单,然后进入名为“jenkins-pipeline-demo”的BuildConfig,再进入到Builds标签,最后再进入“jenkins-pipeline-demo-1”的Build。此时会显示Pipeline的构建进度。
    OpenShift 4 Hands-on Lab (5) - 用Jenkins Pipeline实现在不同运行环境中升迁部署应用_第2张图片
  2. 点击上图的“View logs”链接会进入Jenkins,查看Pipeline的运行日志。
    OpenShift 4 Hands-on Lab (5) - 用Jenkins Pipeline实现在不同运行环境中升迁部署应用_第3张图片
  3. 在构建完后在浏览器中刷新应用页面,点击右上方的灰色图标,在弹出框中会显示当前运行的应用版本,此时赢还是“Version 1”。
    OpenShift 4 Hands-on Lab (5) - 用Jenkins Pipeline实现在不同运行环境中升迁部署应用_第4张图片
  4. 用浏览器访问https://github.com/liuxiaoyu-git/cotd.git,打开item.php页面,然后通过点击“Edit”图标编辑这个文件。在代码中找到“

    Version”并修改版本号,然后“Commit Changes”。

  5. 然后再次执行(12)中的start-build命令,在Jenkins Pipeline运行成功后可刷新并查看页面是否发生变化。
  6. 查看USER-ID-pipeline-test和USER-ID-pipeline-prod项目,确认没有任何对象。这是因为上面操作我们是通过手动部署的。
$ oc get all -n ${USERID}-pipeline-test
No resources found in pipeline-test namespace.
$ oc get all -n ${USERID}-pipeline-prod
No resources found in pipeline-prod namespace.
  1. 要将myapp部署运行在USER-ID-pipeline-test和USER-ID-pipeline-prod项目,还要执行以下命令,将名为“myapp:latest”的ImageStream新打两个标签“myapp:testready”和“myapp:prodready”。
$ oc tag myapp:latest myapp:testready myapp:prodready -n ${USERID}-pipeline-dev
Tag myapp:testready set to myapp@sha256:566ee55311f0cd0a80291c2345e7a1b72ca3eb84455a032433e2e3d11f39efbb.
Tag myapp:prodready set to myapp@sha256:566ee55311f0cd0a80291c2345e7a1b72ca3eb84455a032433e2e3d11f39efbb.
$ oc get is -n ${USERID}-pipeline-dev
NAME    IMAGE REPOSITORY                                                                             TAGS                         UPDATED
myapp   default-route-openshift-image-registry.apps.cluster.example.opentlc.com/pipeline-dev/myapp   prodready,testready,latest   9 seconds ago
  1. 执行以下命令,使用(19)的两个新ImageStreamTag在在USER-ID-pipeline-test和USER-ID-pipeline-prod项目中创建应用。
$ oc new-app ${USERID}-pipeline-dev/myapp:testready --name=myapp -n ${USERID}-pipeline-test
$ oc new-app ${USERID}-pipeline-dev/myapp:prodready --name=myapp -n ${USERID}-pipeline-prod
$ oc expose service myapp -n ${USERID}-pipeline-test
$ oc expose service myapp -n ${USERID}-pipeline-prod
$ oc get dc myapp -o yaml -n ${USERID}-pipeline-test | sed 's/automatic: true/automatic: false/g' | oc replace -f -
$ oc get dc myapp -o yaml -n ${USERID}-pipeline-prod | sed 's/automatic: true/automatic: false/g' | oc replace -f -
  1. 下载https://raw.githubusercontent.com/liuxiaoyu-git/OpenShift-HOL/master/jenkins-pipeline-demo2.yaml文件。此文件在jenkins-pipeline-demo1.yaml的基础让增加了将myapp部署到test和prod环境的步骤。
$ curl -LO https://raw.githubusercontent.com/liuxiaoyu-git/OpenShift-HOL/master/jenkins-pipeline-demo2.yaml
  1. 将jenkins-pipeline-demo2.yaml文件中的USERID变量内容改为自己的用户编号,然后执行以下命令,在USER-ID-pipeline-jenkins项目中替换已有的BuildConfig,然后启动Pipeline构建。
$ oc replace -f jenkins-pipeline-demo2.yaml -n ${USERID}-pipeline-jenkins
$ oc start-build jenkins-pipeline-demo -n ${USERID}-pipeline-jenkins
  1. 查看名为jenkins-pipeline-demo-2的Build执行情况。当出现 “Input Required”,点击该链接进入Jenkins。
    OpenShift 4 Hands-on Lab (5) - 用Jenkins Pipeline实现在不同运行环境中升迁部署应用_第5张图片
  2. 在Jenkins中点击左侧的Console Output,在右侧找到下图位置,然后点击“Promote”链接,Pipeline继续得以运行直到显示“Finished: SUCCESS”。
    在这里插入图片描述
  3. 执行以下命令,访问3个环境的应用。
$ oc get route myapp -n ${USERID}-pipeline-dev -o template --template '{{.spec.host}}'
$ oc get route myapp -n ${USERID}-pipeline-test -o template --template '{{.spec.host}}'
$ oc get route myapp -n ${USERID}-pipeline-prod -o template --template '{{.spec.host}}'
  1. 根据(16)的说明修改程序中的Version,然后再次执行(12)中的start-build命令运行Pipeline。在Jenkins Pipeline运行成功后可刷新并查看三个环境的应用页面是否发生变化。

你可能感兴趣的:(OpenShift,4,Dev,Jenkins)