容器化参考文档
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.0 KiB

3 years ago
  1. jenkins-pipline-usage.md
  2. ------------------------
  3. ```
  4. @version 180808:1
  5. @author zhangxuhong <zhangxuhong@xitu.io>
  6. ```
  7. Name
  8. ----
  9. jenkins-pipine-usage - jenkins流水线使用手册.
  10. Table of Contents
  11. -----------------
  12. * [Name](#name)
  13. * [Reference 参考文档](#reference)
  14. Reference 参考文档
  15. ------------------
  16. * [官方手册](https://github.com/jenkinsci/kubernetes-plugin)
  17. https://zhangchenchen.github.io/2017/12/17/achieve-cicd-in-kubernetes-with-jenkins/
  18. https://www.cnblogs.com/hahp/p/5812455.html
  19. https://www.ibm.com/developerworks/cn/devops/d-based-ibm-cloud-private/index.html
  20. Tips
  21. ----
  22. 流水线构建过程将每个部分称作"stage", 语法是:
  23. ```
  24. stage('Stage Name') {
  25. // action
  26. }
  27. ```
  28. 默认stage中的操作, 例如运行shell命令等, 如果没有指定container, 都是运行是在默认的jnlp容器上的.
  29. 如果指定了container, 则会运行在指定的cloud字段中的kubernetes集群中.
  30. 所以插件中配置kubernetes集群登录信息完全不是为了直接在上面部署业务, 而是提供了一个运行jenkins slave容器的环境而已.
  31. 因此, 传统的 checkout, build, push, deploy 过程中最后的deploy过程就变成了:
  32. - 有几个kubernetes集群就弄几个包含kubectl和认证文件的docker image.
  33. - 部署的时候用这个docker image执行kubectl来部署.
  34. 下面是一段配置sample:
  35. ```
  36. def label = "worker-${UUID.randomUUID().toString()}"
  37. podTemplate(label: label, containers: [
  38. containerTemplate(name: 'gradle', image: 'gradle:4.5.1-jdk9', command: 'cat', ttyEnabled: true),
  39. containerTemplate(name: 'docker', image: 'docker', command: 'cat', ttyEnabled: true),
  40. containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl:v1.8.8', command: 'cat', ttyEnabled: true),
  41. containerTemplate(name: 'helm', image: 'lachlanevenson/k8s-helm:latest', command: 'cat', ttyEnabled: true)
  42. ],
  43. volumes: [
  44. hostPathVolume(mountPath: '/home/gradle/.gradle', hostPath: '/tmp/jenkins/.gradle'),
  45. hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
  46. ]) {
  47. node(label) {
  48. def myRepo = checkout scm
  49. def gitCommit = myRepo.GIT_COMMIT
  50. def gitBranch = myRepo.GIT_BRANCH
  51. def shortGitCommit = "${gitCommit[0..10]}"
  52. def previousGitCommit = sh(script: "git rev-parse ${gitCommit}~", returnStdout: true)
  53. stage('Test') {
  54. try {
  55. container('gradle') {
  56. sh """
  57. pwd
  58. echo "GIT_BRANCH=${gitBranch}" >> /etc/environment
  59. echo "GIT_COMMIT=${gitCommit}" >> /etc/environment
  60. gradle test
  61. """
  62. }
  63. }
  64. catch (exc) {
  65. println "Failed to test - ${currentBuild.fullDisplayName}"
  66. throw(exc)
  67. }
  68. }
  69. stage('Build') {
  70. container('gradle') {
  71. sh "gradle build"
  72. }
  73. }
  74. stage('Create Docker images') {
  75. container('docker') {
  76. withCredentials([[$class: 'UsernamePasswordMultiBinding',
  77. credentialsId: 'dockerhub',
  78. usernameVariable: 'DOCKER_HUB_USER',
  79. passwordVariable: 'DOCKER_HUB_PASSWORD']]) {
  80. sh """
  81. docker login -u ${DOCKER_HUB_USER} -p ${DOCKER_HUB_PASSWORD}
  82. docker build -t namespace/my-image:${gitCommit} .
  83. docker push namespace/my-image:${gitCommit}
  84. """
  85. }
  86. }
  87. }
  88. stage('Run kubectl') {
  89. container('kubectl') {
  90. sh "kubectl get pods"
  91. }
  92. }
  93. stage('Run helm') {
  94. container('helm') {
  95. sh "helm list"
  96. }
  97. }
  98. }
  99. }
  100. ```
  101. Issues
  102. ------
  103. - 多段构建问题
  104. ```
  105. Step 1/8 : FROM harbor02.juejin.id/infrastructure/nginx-1.10.3-centos-with-lua-nginx-module:latest as template
  106. Error parsing reference: "harbor02.juejin.id/infrastructure/nginx-1.10.3-centos-with-lua-nginx-module:latest as template" is not a valid repository/tag: invalid reference format
  107. ```
  108. 由于线上集群是docker 17.03.2-ce, 不支持多段构建. 所以 xxx as template 语法不能使用.
  109. - 镜像push问题
  110. ```
  111. + docker push harbor02.juejin.id/test/suid-generator:1ea59b9
  112. The push refers to a repository [harbor02.juejin.id/test/suid-generator]
  113. 1c91f1c79983: Preparing
  114. f070d45fa624: Preparing
  115. 129f1de793ff: Preparing
  116. 0c195f651c3f: Preparing
  117. 289b7a478aed: Preparing
  118. ae39cf183283: Preparing
  119. ce860bbdcfdd: Preparing
  120. 129b697f70e9: Preparing
  121. ae39cf183283: Waiting
  122. ce860bbdcfdd: Waiting
  123. 129b697f70e9: Waiting
  124. denied: requested access to the resource is denied
  125. ```
  126. 线上harbor集群push镜像需要docker登录, 因此需要将使用的构建镜像的docker增加登录信息. 登录信息在/root/.docker/config.json
  127. - 镜像缓存问题
  128. containerTemplate中特定tag的image 一旦被下载到本地一次就不会再从harbor下载了, 无论harbor是否更新了image.
  129. 所以要使用新的image一定要修改tag.
  130. - kubeapi版本问题
  131. ```
  132. + kubectl --kubeconfig=/root/.kube/config apply -f ./Deployment.yaml
  133. error: unable to recognize "./Deployment.yaml": no matches for kind "Deployment" in version "v1"
  134. ```
  135. Deployment 支持的版本是 apps/v1, 具体可以用kubectl api-versions查看.