一、CICD和DevOps

前面已经了解了CI/CD,其实CI/CD已经存在多年了,只是最近软件工程方面又提出了敏捷开发、DevOps,又把CI/CD炒火了。

那么什么是DevOps?DevOps和CI/CD有又什么关系呢?

以下内容摘自https://en.wikipedia.org/wiki/DevOps

DevOps (a clipped compound of "development" and "operations") is a software development methodology that combines software development (Dev) with information technology operations (Ops). The goal of DevOps is to shorten the systems development life cycle while delivering features, fixes, and updates frequently in close alignment with business objectives.

DevOps(是由"development" and "operations"两个单词合成)是一个软件开发工程的方法论,它包含了软件开发和IT操作(测试和部署)。DevOps的目标是在持续交付、修改、更新时,紧密结合业务,旨在缩短系统开发的生命周期。

我这里为什么提出DevOps呢?因为DevOps其实是一套方法论,涵盖了我们将要说的CI/CD。从上图可以看出,DevOps包含了以下内容:

  1. Coding – code development and review, source code management tools, code merging(代码)
  2. Building – continuous integration tools, build status(构建)
  3. Testing – continuous testing tools that provide feedback on business risks(测试)
  4. Packaging – artifact repository, application pre-deployment staging(打包)
  5. Releasing – change management, release approvals, release automation(发版)
  6. Configuring – infrastructure configuration and management, infrastructure as code tools(发布)
  7. Monitoring – applications performance monitoring, end-user experience(监控)

CI基本上包括了编码、构建、测试、打包、发版。

CD基本上主要就是发布。

二、CI/CD和Docker结合

结合Docker,我们可以快速实现CI/CD,当然有少不了版本管理和编译工具。

具体流程如下:

流程解析:

  1. 开发人员提交代码到代码库(Git Push)
  2. Jenkins从版本库拉取最新代码(Pull Code)
  3. Jenkins通过Maven进行构建打包(Build Package)
  4. 通过Docker将最新版本做成镜像,并推算至镜像仓库(Push/Harbor)
  5. 测试环境直接拉取最新版本镜像,并部署到测试环境(Pull/Docker Build)

服务器分布:

软件环境:

三、实现

1、安装Docker

Docker服务三台机器上都需要安装

  • 191上的Jenkins需要通过Docker编译打包;
  • 192上的Docker需要进行版本发布,即将191上打好的最新版本包发布到线上;
  • 192上安装Harbor需要依赖Docker;

Docker安装过程在Docker安装一文中有介绍,三步就搞定。

最后能够正常输出docker info算是完成。

  1. [root@localhost local]# docker info
  2. Containers: 1
  3. Running: 1
  4. Paused: 0
  5. Stopped: 0
  6. Images: 5
  7. Server Version: 18.09.0
  8. Storage Driver: overlay2
  9. Backing Filesystem: extfs
  10. Supports d_type: true
  11. Native Overlay Diff: true
  12. Logging Driver: json-file
  13. Cgroup Driver: cgroupfs
  14. Plugins:
  15. Volume: local
  16. Network: bridge host macvlan null overlay
  17. Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
  18. Swarm: inactive
  19. Runtimes: runc
  20. Default Runtime: runc
  21. Init Binary: docker-init
  22. containerd version: c4446665cb9c30056f4998ed953e6d4ff22c7c39
  23. runc version: 4fc53a81fb7c994640722ac585fa9ca548971871
  24. init version: fec3683
  25. Security Options:
  26. seccomp
  27. Profile: default
  28. Kernel Version: 3.10.0-693.el7.x86_64
  29. Operating System: CentOS Linux 7 (Core)
  30. OSType: linux
  31. Architecture: x86_64
  32. CPUs: 4
  33. Total Memory: 7.639GiB
  34. Name: localhost.localdomain
  35. ID: GUA5:BZVI:PA5N:7ASK:RZQN:I6VL:IGXE:XCRC:TBFN:7UFI:Y5WS:4O7L
  36. Docker Root Dir: /var/lib/docker
  37. Debug Mode (client): false
  38. Debug Mode (server): false
  39. Registry: https://index.docker.io/v1/
  40. Labels:
  41. Experimental: false
  42. Insecure Registries:
  43. 127.0.0.0/8
  44. Registry Mirrors:
  45. https://registry.docker-cn.com/
  46. Live Restore Enabled: false
  47. Product License: Community Engine

  

2、安装Jenkins

在191服务器上安装Jenkins,包括JDK、Tomcat、Maven环境的安装。因为我们会模拟一个java工程,通过maven进行编译打包,通过Tomcat跑起来。

将apache-maven-3.5.0-bin.tar.gz、apache-tomcat-8.0.46.tar.gz、jdk-8u45-linux-x64.tar.gz解压到/usr/local目录下。

  1. [root@localhost local]# ll
  2. total 52
  3. drwxr-xr-x 6 root root 4096 Dec 20 11:15 apache-maven-3.5.0
  4. drwxr-xr-x 9 root root 4096 Dec 20 11:44 apache-tomcat-8.0.46
  5. drwxr-xr-x. 2 root root 4096 Nov 5 2016 bin
  6. drwxr-xr-x. 2 root root 4096 Nov 5 2016 etc
  7. drwxr-xr-x. 2 root root 4096 Nov 5 2016 games
  8. drwxr-xr-x. 2 root root 4096 Nov 5 2016 include
  9. drwxr-xr-x 8 10 143 4096 Apr 11 2015 jdk1.8.0_45
  10. drwxr-xr-x. 2 root root 4096 Nov 5 2016 lib
  11. drwxr-xr-x. 2 root root 4096 Nov 5 2016 lib64
  12. drwxr-xr-x. 2 root root 4096 Nov 5 2016 libexec
  13. drwxr-xr-x. 2 root root 4096 Nov 5 2016 sbin
  14. drwxr-xr-x. 5 root root 4096 Mar 19 2018 share
  15. drwxr-xr-x. 2 root root 4096 Dec 20 09:35 src

  

通过Docker运行Jenkins,Dockerfile如下:

  1. FROM jenkins
  2. USER root
  3. RUN echo '' > /etc/apt/sources.list.d/jessie-backports.list && \
  4. wget http://mirrors.163.com/.help/sources.list.jessie -O /etc/apt/sources.list
  5. RUN apt-get update && apt-get install -y git libltdl-dev

  使用线上的Jenkins基础镜像,替换apt源,初始化安装git客户端和libltdl-dev包

构建Jenkins镜像:

  1. docker build -t jenkins:v1 .

  

启动Jenkins

  1. docker run -d --name jenkins -p 8080:8080 \
  2. -v /var/jenkins_home/:/var/jenkins_home \
  3. -v /usr/local/apache-maven-3.5.0:/usr/local/maven \
  4. -v /usr/local/jdk1.8.0_45:/usr/local/jdk \
  5. -v /var/run/docker.sock:/var/run/docker.sock \
  6. -v $(which docker):/usr/bin/docker \
  7. -v ~/.ssh:/root/.ssh \
  8. jenkins:v1

  

3、安装Git Server

  1. 1 、安装 Git
  2. yum install git
  3. 2 2 、创建 Git 用户
  4. useradd git
  5. passwd git
  6. 3 3 、创建仓库
  7. su git
  8. mkdir app.git
  9. git -bare init

  

4、安装Harbor

  1. [root@reg harbor]# ll
  2. total 651416
  3. drwxr-xr-x 4 root root 4096 Dec 20 10:40 common
  4. -rw-r--r-- 1 root root 813 Nov 19 15:02 docker-compose.chartmuseum.yml
  5. -rw-r--r-- 1 root root 863 Nov 19 15:02 docker-compose.clair.yml
  6. -rw-r--r-- 1 root root 1258 Nov 19 15:02 docker-compose.notary.yml
  7. -rw-r--r-- 1 root root 3675 Nov 19 15:02 docker-compose.yml
  8. drwxr-xr-x 3 root root 4096 Nov 19 15:02 ha
  9. -rw-r--r-- 1 root root 7928 Dec 20 14:57 harbor.cfg
  10. -rw-r--r-- 1 root root 665406909 Nov 19 15:02 harbor.v1.6.2.tar.gz
  11. -rwxr-xr-x 1 root root 6162 Nov 19 15:02 install.sh
  12. -rw-r--r-- 1 root root 10768 Nov 19 15:02 LICENSE
  13. -rw-r--r-- 1 root root 482 Nov 19 15:02 NOTICE
  14. -rw-r--r-- 1 root root 1535603 Nov 19 15:02 open_source_license
  15. -rw-r--r-- 1 root root 18 Dec 24 22:30 password
  16. -rwxr-xr-x 1 root root 39132 Nov 19 15:02 prepare

  配置harbor.cfg,修改hostname。

prepare

  1. Generated and saved secret to file: /data/secretkey
  2. Generated configuration file: ./common/config/nginx/nginx.conf
  3. Generated configuration file: ./common/config/adminserver/env
  4. Generated configuration file: ./common/config/ui/env
  5. Generated configuration file: ./common/config/registry/config.yml
  6. Generated configuration file: ./common/config/db/env
  7. Generated configuration file: ./common/config/jobservice/env
  8. Generated configuration file: ./common/config/jobservice/config.yml
  9. Generated configuration file: ./common/config/log/logrotate.conf
  10. Generated configuration file: ./common/config/registryctl/env
  11. Generated configuration file: ./common/config/ui/app.conf
  12. Generated certificate, key file: ./common/config/ui/private_key.pem, cert file: ./common/config/registry/root.crt
  13. The configuration files are ready, please use docker-compose to start the service.
  14. [root@localhost harbor]#

  

错误提示:

  1. [root@localhost harbor]# ./install.sh
  2. [Step 0]: checking installation environment ...
  3. Need to install docker(1.10.0+) first and run this script again.
  4. [root@localhost harbor]#
  5. [root@localhost harbor]# ./install.sh
  6. [Step 0]: checking installation environment ...
  7. Note: docker version: 18.09.0
  8. Need to install docker-compose(1.7.1+) by yourself first and run this script again.

  

install

  1. [root@localhost harbor]# ./install.sh
  2.  
  3. [Step 0]: checking installation environment ...
  4.  
  5. Note: docker version: 18.09.0
  6.  
  7. Note: docker-compose version: 1.15.0
  8.  
  9. [Step 1]: loading Harbor images ...
  10. 4de51055f30c: Loading layer [==================================================>] 133.2MB/133.2MB
  11. e42dc4492c57: Loading layer [==================================================>] 23.38MB/23.38MB
  12. 6fd7d92da0ec: Loading layer [==================================================>] 3.072kB/3.072kB
  13. 92c622c62d9c: Loading layer [==================================================>] 2.56kB/2.56kB
  14. eee26e869426: Loading layer [==================================================>] 2.56kB/2.56kB
  15. 0bdc2068fdfa: Loading layer [==================================================>] 2.048kB/2.048kB
  16. 1161820c2669: Loading layer [==================================================>] 22.8MB/22.8MB
  17. 1eebb5c60237: Loading layer [==================================================>] 22.8MB/22.8MB
  18. Loaded image: goharbor/registry-photon:v2.6.2-v1.6.2
  19. 0155cb3a636c: Loading layer [==================================================>] 23.38MB/23.38MB
  20. 62f917db5fed: Loading layer [==================================================>] 12.16MB/12.16MB
  21. 2e192a070c25: Loading layer [==================================================>] 17.3MB/17.3MB
  22. 64fa72e486ec: Loading layer [==================================================>] 11.26kB/11.26kB
  23. 23afd47b0f1a: Loading layer [==================================================>] 3.072kB/3.072kB
  24. 3fa7415d357e: Loading layer [==================================================>] 29.46MB/29.46MB
  25. Loaded image: goharbor/notary-server-photon:v0.5.1-v1.6.2
  26. 2f06068ec40a: Loading layer [==================================================>] 158MB/158MB
  27. d6e5bcc842f3: Loading layer [==================================================>] 10.93MB/10.93MB
  28. c272c6b03ae0: Loading layer [==================================================>] 2.048kB/2.048kB
  29. 7b0653de0007: Loading layer [==================================================>] 48.13kB/48.13kB
  30. 484f0b8e979d: Loading layer [==================================================>] 3.072kB/3.072kB
  31. 72004696fb26: Loading layer [==================================================>] 10.98MB/10.98MB
  32. Loaded image: goharbor/clair-photon:v2.0.6-v1.6.2
  33. c5362d9a52ab: Loading layer [==================================================>] 158MB/158MB
  34. 547ee492a9fc: Loading layer [==================================================>] 35.08MB/35.08MB
  35. 72ca312cce32: Loading layer [==================================================>] 2.048kB/2.048kB
  36. ba7a5e9f2574: Loading layer [==================================================>] 3.072kB/3.072kB
  37. 8fabfc794eb2: Loading layer [==================================================>] 35.08MB/35.08MB
  38. Loaded image: goharbor/chartmuseum-photon:v0.7.1-v1.6.2
  39. a86040096f1b: Loading layer [==================================================>] 73.32MB/73.32MB
  40. d81fe13ca34f: Loading layer [==================================================>] 3.584kB/3.584kB
  41. a25703e967fd: Loading layer [==================================================>] 3.072kB/3.072kB
  42. 5a619498aaf0: Loading layer [==================================================>] 4.096kB/4.096kB
  43. 490efa0d32bb: Loading layer [==================================================>] 3.584kB/3.584kB
  44. 0a8ef8ce5e5e: Loading layer [==================================================>] 9.728kB/9.728kB
  45. Loaded image: goharbor/harbor-log:v1.6.2
  46. 192ffc0c6a5f: Loading layer [==================================================>] 95.86MB/95.86MB
  47. a0f6ec07aba5: Loading layer [==================================================>] 6.656kB/6.656kB
  48. 5cb4047d9a6f: Loading layer [==================================================>] 2.048kB/2.048kB
  49. 3c5d322a1758: Loading layer [==================================================>] 7.68kB/7.68kB
  50. d69b5a088645: Loading layer [==================================================>] 2.56kB/2.56kB
  51. 38a2b4654f0b: Loading layer [==================================================>] 2.56kB/2.56kB
  52. 4f04d5805632: Loading layer [==================================================>] 2.56kB/2.56kB
  53. Loaded image: goharbor/harbor-db:v1.6.2
  54. b6bb4bf71953: Loading layer [==================================================>] 23.38MB/23.38MB
  55. 2c121a1131b7: Loading layer [==================================================>] 21.15MB/21.15MB
  56. bdea637333e2: Loading layer [==================================================>] 21.15MB/21.15MB
  57. Loaded image: goharbor/harbor-jobservice:v1.6.2
  58. 15e806b56692: Loading layer [==================================================>] 5.124MB/5.124MB
  59. Loaded image: goharbor/nginx-photon:v1.6.2
  60. b777c542e104: Loading layer [==================================================>] 10.95MB/10.95MB
  61. c2ccff7df242: Loading layer [==================================================>] 17.3MB/17.3MB
  62. e188e4d1b597: Loading layer [==================================================>] 11.26kB/11.26kB
  63. ca7cd6746e0b: Loading layer [==================================================>] 3.072kB/3.072kB
  64. c7d958c5de1a: Loading layer [==================================================>] 28.24MB/28.24MB
  65. Loaded image: goharbor/notary-signer-photon:v0.5.1-v1.6.2
  66. fbc524a787eb: Loading layer [==================================================>] 684MB/684MB
  67. e8e8215cd36d: Loading layer [==================================================>] 7.68kB/7.68kB
  68. d061c1c55f93: Loading layer [==================================================>] 197.6kB/197.6kB
  69. Loaded image: goharbor/harbor-migrator:v1.6.2
  70. 77719882ce23: Loading layer [==================================================>] 23.38MB/23.38MB
  71. 1136e0b049e1: Loading layer [==================================================>] 15.58MB/15.58MB
  72. 4469c6f64c47: Loading layer [==================================================>] 15.36kB/15.36kB
  73. 91ffefa33975: Loading layer [==================================================>] 15.58MB/15.58MB
  74. Loaded image: goharbor/harbor-adminserver:v1.6.2
  75. 0d6ec75380ac: Loading layer [==================================================>] 23.38MB/23.38MB
  76. 5ffcef8af51b: Loading layer [==================================================>] 26.88MB/26.88MB
  77. 334a9c59109a: Loading layer [==================================================>] 7.168kB/7.168kB
  78. 15b85ff320f4: Loading layer [==================================================>] 11.32MB/11.32MB
  79. 5118ce7d7887: Loading layer [==================================================>] 26.87MB/26.87MB
  80. Loaded image: goharbor/harbor-ui:v1.6.2
  81. 4316b32f3d05: Loading layer [==================================================>] 84.34MB/84.34MB
  82. 0ba9b0933327: Loading layer [==================================================>] 3.072kB/3.072kB
  83. 65e524929f77: Loading layer [==================================================>] 59.9kB/59.9kB
  84. 8675c8d64203: Loading layer [==================================================>] 61.95kB/61.95kB
  85. Loaded image: goharbor/redis-photon:v1.6.2
  86.  
  87. [Step 2]: preparing environment ...
  88. Clearing the configuration file: ./common/config/ui/app.conf
  89. Clearing the configuration file: ./common/config/ui/private_key.pem
  90. Clearing the configuration file: ./common/config/ui/env
  91. Clearing the configuration file: ./common/config/log/logrotate.conf
  92. Clearing the configuration file: ./common/config/registryctl/config.yml
  93. Clearing the configuration file: ./common/config/registryctl/env
  94. Clearing the configuration file: ./common/config/db/env
  95. Clearing the configuration file: ./common/config/nginx/nginx.conf
  96. Clearing the configuration file: ./common/config/jobservice/config.yml
  97. Clearing the configuration file: ./common/config/jobservice/env
  98. Clearing the configuration file: ./common/config/adminserver/env
  99. Clearing the configuration file: ./common/config/registry/config.yml
  100. Clearing the configuration file: ./common/config/registry/root.crt
  101. loaded secret from file: /data/secretkey
  102. Generated configuration file: ./common/config/nginx/nginx.conf
  103. Generated configuration file: ./common/config/adminserver/env
  104. Generated configuration file: ./common/config/ui/env
  105. Generated configuration file: ./common/config/registry/config.yml
  106. Generated configuration file: ./common/config/db/env
  107. Generated configuration file: ./common/config/jobservice/env
  108. Generated configuration file: ./common/config/jobservice/config.yml
  109. Generated configuration file: ./common/config/log/logrotate.conf
  110. Generated configuration file: ./common/config/registryctl/env
  111. Generated configuration file: ./common/config/ui/app.conf
  112. Generated certificate, key file: ./common/config/ui/private_key.pem, cert file: ./common/config/registry/root.crt
  113. The configuration files are ready, please use docker-compose to start the service.
  114.  
  115. [Step 3]: checking existing instance of Harbor ...
  116.  
  117. [Step 4]: starting Harbor ...
  118. Creating network "harbor_harbor" with the default driver
  119. Creating harbor-log ...
  120. Creating harbor-log ... done
  121. Creating registry ...
  122. Creating harbor-db ...
  123. Creating redis ...
  124. Creating harbor-adminserver ...
  125. Creating registry
  126. Creating redis
  127. Creating harbor-adminserver
  128. Creating registry ... done
  129. Creating harbor-ui ...
  130. Creating harbor-ui ... done
  131. Creating nginx ...
  132. Creating harbor-jobservice ...
  133. Creating nginx
  134. Creating nginx ... done
  135.  
  136. ----Harbor has been installed and started successfully.----
  137.  
  138. Now you should be able to visit the admin portal at http://reg.xuequn.com.
  139. For more details, please visit https://github.com/goharbor/harbor .

  

启动harbor:

  1. [root@reg harbor]# docker-compose -f docker-compose.yml up -d
  2. harbor-log is up-to-date
  3. harbor-adminserver is up-to-date
  4. registry is up-to-date
  5. harbor-db is up-to-date
  6. Starting redis ...
  7. Starting harbor-ui ...
  8. Starting redis
  9. Starting harbor-ui ... done
  10. nginx is up-to-date
  11. Starting harbor-jobservice ...
  12. Starting harbor-jobservice ... done

  

Harbor支持Https配置(后面pull镜像的时候需要使用,从安全角度来说,最好也是https)

  1. [root@reg harbor]# cat harbor.cfg |grep -v "#"|grep -v ^$
  2. _version = 1.6.0
  3. hostname = reg.xuequn.com
  4. ui_url_protocol = https
  5. max_job_workers = 10
  6. customize_crt = on
  7. ssl_cert = /data/cert/reg.xuequn.com.crt
  8. ssl_cert_key = /data/cert/reg.xuequn.com.key
  9. secretkey_path = /data

  

证书生成:

  1. [root@reg data]# openssl req \
  2. > -newkey rsa:4096 -nodes -sha256 -keyout ca.key \
  3. > -x509 -days 365 -out ca.crt
  4. Generating a 4096 bit RSA private key
  5. .........................................++
  6. ..................................................................................................................................................................................++
  7. writing new private key to 'ca.key'
  8. -----
  9. You are about to be asked to enter information that will be incorporated
  10. into your certificate request.
  11. What you are about to enter is what is called a Distinguished Name or a DN.
  12. There are quite a few fields but you can leave some blank
  13. For some fields there will be a default value,
  14. If you enter '.', the field will be left blank.
  15. -----
  16. Country Name (2 letter code) [XX]:china
  17. string is too long, it needs to be less than 2 bytes long
  18. Country Name (2 letter code) [XX]:ch
  19. State or Province Name (full name) []:zh
  20. Locality Name (eg, city) [Default City]:zhuhai
  21. Organization Name (eg, company) [Default Company Ltd]:king
  22. Organizational Unit Name (eg, section) []:seasun
  23. Common Name (eg, your name or your server's hostname) []:reg.xuequn.com
  24. Email Address []:xuequn@qq.com
  25.  
  26. [root@reg data]# openssl req \
  27. > -newkey rsa:4096 -nodes -sha256 -keyout reg.xuequn.com.key \
  28. > -out reg.xuequn.com.csr
  29. Generating a 4096 bit RSA private key
  30. ..................++
  31. ..............................................................................................................................................++
  32. writing new private key to 'reg.xuequn.com.key'
  33. -----
  34. You are about to be asked to enter information that will be incorporated
  35. into your certificate request.
  36. What you are about to enter is what is called a Distinguished Name or a DN.
  37. There are quite a few fields but you can leave some blank
  38. For some fields there will be a default value,
  39. If you enter '.', the field will be left blank.
  40. -----
  41. Country Name (2 letter code) [XX]:ch
  42. State or Province Name (full name) []:zh
  43. Locality Name (eg, city) [Default City]:zhuhai
  44. Organization Name (eg, company) [Default Company Ltd]:king
  45. Organizational Unit Name (eg, section) []:seasun
  46. Common Name (eg, your name or your server's hostname) []:reg.xuequn.com
  47. Email Address []:xuequn@qq.com
  48.  
  49. Please enter the following 'extra' attributes
  50. to be sent with your certificate request
  51. A challenge password []:xuequn123
  52. An optional company name []:king
  53. [root@reg data]#
  54.  
  55. [root@reg data]# openssl x509 -req -days 365 -in reg.xuequn.com.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out reg.xuequn.com.crt
  56. Signature ok
  57. subject=/C=ch/ST=zh/L=zhuhai/O=king/OU=seasun/CN=xuequn/emailAddress=xuequn@qq.com
  58. Getting CA Private Key
  59. [root@reg data]#
  60.  
  61. [root@localhost solo]# docker login reg.xuequn.com
  62. Username: xuequn
  63. Password:
  64. Error response from daemon: Get https://reg.xuequn.com/v2/: x509: certificate signed by unknown authority
  65. [root@localhost solo]# mkdir -p /etc/docker/
  66. daemon.json key.json
  67. [root@localhost solo]# mkdir -p /etc/docker/
  68. daemon.json key.json
  69. [root@localhost solo]# mkdir -p /etc/docker/cert.d/
  70. [root@localhost solo]# cd /etc/docker/cert.d/
  71. [root@localhost cert.d]# ls
  72. [root@localhost cert.d]# rz -y
  73. rz waiting to receive.
  74. zmodem trl+C ȡ
  75.  
  76. 100% 1 KB 1 KB/s 00:00:01 0 Errors
  77.  
  78. [root@localhost cert.d]# ls
  79. reg.xuequn.com.crt
  80. [root@localhost cert.d]# systemctl restart docker

  注意:修改harbor.cfg文件后,需要重新prepare,生成配置文件。

登陆镜像仓库,提示x509: certificate signed by unknown authority错误,解决办法如下:

  1. [root@localhost cert.d]# docker login reg.xuequn.com
  2. Username: xuequn
  3. Password:
  4. Error response from daemon: Get https://reg.xuequn.com/v2/: x509: certificate signed by unknown authority
  5. [root@localhost cert.d]# chmod 644 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
  6.  
  7. [root@localhost cert.d]#
  8. [root@localhost cert.d]# cat reg.xuequn.com.crt >>/etc/pki/tls/certs/ca-bundle.crt
  9. [root@localhost cert.d]# chmod 444 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
  10. [root@localhost cert.d]# systemctl restart docker
  11. [root@localhost cert.d]# docker login reg.xuequn.com
  12. Username: xuequn
  13. Password:
  14. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  15. Configure a credential helper to remove this warning. See
  16. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  17.  
  18. Login Succeeded
  19. [root@localhost cert.d]# docker logout reg.xuequn.com
  20. Removing login credentials for reg.xuequn.com
  21. [root@localhost cert.d]#

  

https方式访问Harbor,并创建项目:

新建test项目:

5、Jenkins配置

全局工具配置,JDK、Git、Maven环境:

构建配置,配置项目名称

配置源码仓库

创建构建触发器,每分钟拉取一次,如果有新的版本生成的话,会自动构建:

跳过测试样例,节省构建时间

正式构建,在工作目录下新建Dockerfile,构建好镜像文件后,推送到远程镜像仓库,以便部署时可以直接从远程镜像仓库拉取:

远程部署,即从镜像仓库拉取最新镜像文件,并进行部署:

6、测试服务是否正常

1、构建

2、查看构建log,中间省略若干文字

  1. Started by user xuequn
  2. Building in workspace /var/jenkins_home/workspace/solo_blog
  3. > git rev-parse --is-inside-work-tree # timeout=10
  4. Fetching changes from the remote Git repository
  5. > git config remote.origin.url git@10.11.97.193:/home/git/solo.git # timeout=10
  6. Fetching upstream changes from git@10.11.97.193:/home/git/solo.git
  7. > git --version # timeout=10
  8. > git fetch --tags --progress git@10.11.97.193:/home/git/solo.git +refs/heads/*:refs/remotes/origin/*
  9. > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
  10. > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
  11. Checking out Revision ff738c19ebc781c2adbe5907a24df824a4a787d9 (refs/remotes/origin/master)
  12. > git config core.sparsecheckout # timeout=10
  13. > git checkout -f ff738c19ebc781c2adbe5907a24df824a4a787d9
  14. Commit message: "aaa"
  15. > git rev-list --no-walk ff738c19ebc781c2adbe5907a24df824a4a787d9 # timeout=10
  16. No emails were triggered.
  17. Parsing POMs
  18. Established TCP socket on 33529
  19. [solo_blog] $ /usr/local/jdk/bin/java -cp /var/jenkins_home/plugins/maven-plugin/WEB-INF/lib/maven35-agent-1.12.jar:/usr/local/maven/boot/plexus-classworlds-2.5.2.jar:/usr/local/maven/conf/logging jenkins.maven3.agent.Maven35Main /usr/local/maven /var/jenkins_home/war/WEB-INF/lib/remoting-3.7.jar /var/jenkins_home/plugins/maven-plugin/WEB-INF/lib/maven35-interceptor-1.12.jar /var/jenkins_home/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.12.jar 33529
  20. <===[JENKINS REMOTING CAPACITY]===>channel started
  21. Executing Maven: -B -f /var/jenkins_home/workspace/solo_blog/pom.xml clean package -Dmaven.test.skip=true
  22. [INFO] Scanning for projects...
  23. [WARNING]
  24. [WARNING] Some problems were encountered while building the effective model for org.b3log:solo:war:2.7.0
  25. [WARNING] 'dependencies.dependency.systemPath' for org.patchca:patchca:jar should not point at files within the project directory, ${project.basedir}/src/main/resources/lib/net/pusuo/patchca-0.5.0.jar will be unresolvable by dependent projects @ line 237, column 25
  26. [WARNING]
  27. [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
  28. [WARNING]
  29. [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
  30. [WARNING]
  31. [INFO]
  32. [INFO] ------------------------------------------------------------------------
  33. [INFO] Building Solo 2.7.0
  34. [INFO] ------------------------------------------------------------------------
  35. [INFO]
  36. [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ solo ---
  37. [INFO] Deleting /var/jenkins_home/workspace/solo_blog/target
  38. [INFO]
  39. [INFO] --- maven-resources-plugin:2.7:resources (default-resources) @ solo ---
  40. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  41. [INFO] Copying 9 resources
  42. [INFO]
  43. [INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ solo ---
  44. [INFO] Changes detected - recompiling the module!
  45. [INFO] Compiling 159 source files to /var/jenkins_home/workspace/solo_blog/target/classes
  46. [INFO] /var/jenkins_home/workspace/solo_blog/src/main/java/org/b3log/solo/util/Markdowns.java: /var/jenkins_home/workspace/solo_blog/src/main/java/org/b3log/solo/util/Markdowns.java uses or overrides a deprecated API.
  47. [INFO] /var/jenkins_home/workspace/solo_blog/src/main/java/org/b3log/solo/util/Markdowns.java: Recompile with -Xlint:deprecation for details.
  48. [INFO] /var/jenkins_home/workspace/solo_blog/src/main/java/org/b3log/solo/repository/impl/ArticleRepositoryImpl.java: Some input files use unchecked or unsafe operations.
  49. [INFO] /var/jenkins_home/workspace/solo_blog/src/main/java/org/b3log/solo/repository/impl/ArticleRepositoryImpl.java: Recompile with -Xlint:unchecked for details.
  50. [INFO]
  51. [INFO] --- maven-resources-plugin:2.7:testResources (default-testResources) @ solo ---
  52. [INFO] Not copying test resources
  53. [INFO]
  54. [INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) @ solo ---
  55. [INFO] Not compiling test sources
  56. [INFO]
  57. [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ solo ---
  58. [INFO] Tests are skipped.
  59. [INFO]
  60. [INFO] --- maven-war-plugin:3.0.0:war (default-war) @ solo ---
  61. [INFO] Packaging webapp
  62. [INFO] Assembling webapp [solo] in [/var/jenkins_home/workspace/solo_blog/target/solo]
  63. [INFO] Processing war project
  64. [INFO] Copying webapp webResources [/var/jenkins_home/workspace/solo_blog/src/main/resources/lib/net/pusuo] to [/var/jenkins_home/workspace/solo_blog/target/solo]
  65. [INFO] Copying webapp resources [/var/jenkins_home/workspace/solo_blog/src/main/webapp]
  66. [INFO] Webapp assembled in [1831 msecs]
  67. [INFO] Building war: /var/jenkins_home/workspace/solo_blog/target/solo.war
  68. [INFO] ------------------------------------------------------------------------
  69. [INFO] BUILD SUCCESS
  70. [INFO] ------------------------------------------------------------------------
  71. [INFO] Total time: 12.332 s
  72. [INFO] Finished at: 2018-12-31T05:34:01Z
  73. [INFO] Final Memory: 32M/294M
  74. [INFO] ------------------------------------------------------------------------
  75. Waiting for Jenkins to finish collecting data
  76. [JENKINS] Archiving /var/jenkins_home/workspace/solo_blog/pom.xml to org.b3log/solo/2.7.0/solo-2.7.0.pom
  77. [JENKINS] Archiving /var/jenkins_home/workspace/solo_blog/target/solo.war to org.b3log/solo/2.7.0/solo-2.7.0.war
  78. [solo_blog] $ /bin/sh -xe /tmp/jenkins1057186757167035727.sh
  79. channel stopped
  80. + cd /var/jenkins_home/workspace/solo_blog
  81. + cat
  82. + docker build -t reg.xuequn.com/test/solo:v1 .
  83. Sending build context to Docker daemon 81.43MB
  84.  
  85. Step 1/5 : FROM reg.xuequn.com/test/tomcat:v1
  86. ---> f2cc90fa1b2d
  87. Step 2/5 : MAINTAINER xuequn
  88. ---> Using cache
  89. ---> cbf693fd58b6
  90. Step 3/5 : COPY target/solo.war /tmp/ROOT.war
  91. ---> a0f92a38817e
  92. Step 4/5 : RUN rm -rf /usr/local/tomcat/webapps/* && unzip /tmp/ROOT.war -d /usr/local/tomcat/webapps/ROOT && rm -rf /tmp/ROOT.war
  93. ---> Running in e5fe01176375
  94. Archive: /tmp/ROOT.war
  95. inflating: /usr/local/tomcat/webapps/ROOT/META-INF/MANIFEST.MF
  96. creating: /usr/local/tomcat/webapps/ROOT/css/
  97. creating: /usr/local/tomcat/webapps/ROOT/css/fonts/
  98. 中间省略若干字.........
  99. inflating: /usr/local/tomcat/webapps/ROOT/META-INF/maven/org.b3log/solo/pom.properties
  100. Removing intermediate container e5fe01176375
  101. ---> df6ccb273fff
  102. Step 5/5 : ENTRYPOINT ["./bin/catalina.sh", "run"]
  103. ---> Running in 5fbd157b4bca
  104. Removing intermediate container 5fbd157b4bca
  105. ---> f3973c67b6d9
  106. Successfully built f3973c67b6d9
  107. Successfully tagged reg.xuequn.com/test/solo:v1
  108. + docker login -uxuequn -pXUEqun123 reg.xuequn.com
  109. WARNING! Using --password via the CLI is insecure. Use --password-stdin.
  110. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  111. Configure a credential helper to remove this warning. See
  112. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  113.  
  114. Login Succeeded
  115. + docker push reg.xuequn.com/test/solo:v1
  116. The push refers to repository [reg.xuequn.com/test/solo]
  117. b2ea9726881e: Preparing
  118. 39ec58847bc2: Preparing
  119. dbfd362fd452: Preparing
  120. 8e53cd053a9e: Preparing
  121. 7914b85f4bf8: Preparing
  122. 071d8bd76517: Preparing
  123. 071d8bd76517: Waiting
  124. 8e53cd053a9e: Layer already exists
  125. 7914b85f4bf8: Layer already exists
  126. dbfd362fd452: Layer already exists
  127. 071d8bd76517: Layer already exists
  128. 39ec58847bc2: Pushed
  129. b2ea9726881e: Pushed
  130. v1: digest: sha256:17c9dcb2ea28bab46adebd5c38ee8acd34abc2d63eef55e449a1338598904447 size: 1587
  131. [SSH] script:
  132.  
  133. docker rmi -f reg.xuequn.com/test/solo:v1|true
  134. docker rm -f solo|true
  135. docker login -uxuequn -pXUEqun123 reg.xuequn.com
  136. docker run -d --name solo -p 8888:8080 -v /usr/local/jdk1.8.0_45:/usr/local/jdk reg.xuequn.com/test/solo:v1
  137.  
  138. [SSH] executing...
  139. WARNING! Using --password via the CLI is insecure. Use --password-stdin.
  140. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  141. Configure a credential helper to remove this warning. See
  142. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  143.  
  144. Unable to find image 'reg.xuequn.com/test/solo:v1' locally
  145. v1: Pulling from test/solo
  146. a02a4930cb5d: Already exists
  147. 498cfd4001de: Already exists
  148. 5338299d7f49: Already exists
  149. 13ead51b14a6: Already exists
  150. 33bbfcb012e2: Pulling fs layer
  151. cba7031d67c2: Pulling fs layer
  152. Login Succeeded
  153. cba7031d67c2: Verifying Checksum
  154. cba7031d67c2: Download complete
  155. 33bbfcb012e2: Download complete
  156. 33bbfcb012e2: Pull complete
  157. cba7031d67c2: Pull complete
  158. Digest: sha256:17c9dcb2ea28bab46adebd5c38ee8acd34abc2d63eef55e449a1338598904447
  159. Status: Downloaded newer image for reg.xuequn.com/test/solo:v1
  160. 4c24f68a36ac688d723b6d9df1862038144139141fdb8fa61faceeb5592f3743
  161. [SSH] completed
  162. [SSH] exit-status: 0
  163.  
  164. No emails were triggered.
  165. Finished: SUCCESS

  

3、查看服务

在docker服务器上,查看服务是否正常运行:

web访问服务:

至此,整个流程已完成。

7、注意事项

1、Jenkins服务器到git服务器无密码登陆:ssh-copy-id  git@10.11.97.193

  1. [root@localhost t]# git clone git@10.11.97.193:/home/git/solo.git
  2. Cloning into 'solo'...
  3. remote: Counting objects: 2534, done.
  4. remote: Compressing objects: 100% (1878/1878), done.
  5. remote: Total 2534 (delta 646), reused 2462 (delta 587)
  6. Receiving objects: 100% (2534/2534), 28.00 MiB | 43.45 MiB/s, done.
  7. Resolving deltas: 100% (646/646), done.

  

2、Jenkins服务和Docker服务器都需要能够login镜像仓库,第一次需要输入用户名和密码,后续可无密码登陆

将证书加入信任:

  1. [root@localhost cert.d]# docker login reg.xuequn.com
  2. Username: xuequn
  3. Password:
  4. Error response from daemon: Get https://reg.xuequn.com/v2/: x509: certificate signed by unknown authority
  5. [root@localhost cert.d]# chmod 644 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
  6.  
  7. [root@localhost cert.d]#
  8. [root@localhost cert.d]# cat reg.xuequn.com.crt >>/etc/pki/tls/certs/ca-bundle.crt
  9. [root@localhost cert.d]# chmod 444 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
  10. [root@localhost cert.d]# systemctl restart docker
  11. [root@localhost cert.d]# docker login reg.xuequn.com
  12. Username: xuequn
  13. Password:
  14. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  15. Configure a credential helper to remove this warning. See
  16. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  17.  
  18. Login Succeeded
  19. [root@localhost cert.d]# docker logout reg.xuequn.com
  20. Removing login credentials for reg.xuequn.com
  21. [root@localhost cert.d]#

  

再次登陆,无需输入密码:

  1. [root@localhost t]# docker login reg.xuequn.com
  2. Authenticating with existing credentials...
  3. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  4. Configure a credential helper to remove this warning. See
  5. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  6.  
  7. Login Succeeded

  

Docker和CI/CD实战的更多相关文章

  1. Jenkins与Docker的自动化CI/CD实战

    Jenkins与Docker的自动化CI/CD实战 互联网Java架构 2018-09-19 15:46:13 一.发布流程设计 工作流程: 开发人员提交代码到Git版本仓库:Jenkins人工/定时 ...

  2. CI/CD系列

    一.CI/CD系列 什么是CI/CD(译) Docker与CI/CD(转) Docker和CI/CD实战 二.Git 三.GitLab

  3. 超简单让.NET Core开发者快速拥有CI/CD的能力-Docker版本

    超简单让.NET Core开发者快速拥有CI/CD的能力-Docker版本 前言 上一篇自动化测试,全面且详细的介绍了从零开始到发布版本的步骤,这是传统的方式,本次为大家带来的是如何在5分钟内使用上d ...

  4. CI Weekly #19 | 关于软件开发模型的思考,以及最新 CI/CD 实践分享

    五月一来,夏天便悄然而至.flow.ci 也带来了几个新的变化,帮你进一步优化开发工作流.一起来看看这几个重点功能: 支持 iOS 项目 Xcode8.3 构建 iOSer 们重点来了,flow.ci ...

  5. CI/CD

    CI/CD 啥是CI/CD CI: continuous integration, 持续集成.就是频繁地把开发的工作提交到主线代码.主要是为了解决集成问题.什么是集成问题呢,白话说,就是从你本地的代码 ...

  6. 7款不错的 CI/CD工具

    时至今日,越来越多的工程团队开始实行敏捷开发,借以推动更短.更快的发布周期.而代码库的增长与更高的生产构建频率,也带动持续集成与持续部署/交付工具快速兴起.如果您有意提升发布频率,或者是不太清楚哪些工 ...

  7. 8款不错的 CI/CD工具

    Jenkins Jenkins是CI市场中最知名且最常见的名号之一.其最初是由Sun公司的一位工程师打造的一个辅助项目,并迅速扩展为最大的开源CI工具之一,可帮助工程团队实现自动化部署.顺带一提:我们 ...

  8. Docker: Jenkins与Docker的自动化CI/CD流水线实战

    什么是CI/CD 持续集成(Continuous Integration,CI):代码合并.构建.部署.测试都在一起,不断地执行这个过程,并对结果反馈.持续部署(Continuous Deployme ...

  9. .Net微服务实战之CI/CD

    系列文章 .Net微服务实战之技术选型篇 .Net微服务实战之技术架构分层篇 .Net微服务实战之DevOps篇 .Net微服务实战之负载均衡(上) 相关源码:https://github.com/S ...

随机推荐

  1. jenkins+gitlib+git+mysql5.6+sonarqube+sonarrunner

    1. sonarrunner配置: 2. 未完待续....

  2. Python实例---CRM管理系统分析180331

    注意:一个项目基本都设计增删改查,且第一个需要做的就是设计表结构 思维导图: 组件使用: Django  +   bootStrap  +   Jquery 数据库表结构设计: 外键关联: 2种方式, ...

  3. 铁乐学python_day05-作业

    1,有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2": [&q ...

  4. 最小生成数 克鲁斯卡尔 普里姆 matlab

    克鲁斯卡尔: function T=MST_Kruskal(G) n=0; if isfield(G,'w') && ~isempty(G.w) && size(G.w ...

  5. 绕过CDN查找网站真实IP方法收集

    方法1很简单,使用各种多地 ping 的服务,查看对应 IP 地址是否唯一,如果不唯一多半是使用了CDN, 多地 Ping 网站有: http://ping.chinaz.com/ http://pi ...

  6. September 19th 2017 Week 38th Tuesday

    Live boldly. Push yourself. Don't settle. 勇敢生活,突破自我,永不设限! Don't indulge in the past, whether it was ...

  7. November 13th 2016 Week 47th Sunday The 1st Day

    Adventure may hurt you, but monotony will kill you. 也许冒险会让你受伤,但一成不变会让你灭亡. Just change a bit, let the ...

  8. 获取索引--------用range()和len()

    a = ['Google', 'Baidu', 'Runoob', 'Taobao', 'QQ'] for i in range(len(a)): print(i+1,a[i])

  9. Django之Model (ORM)

    传统操作数据库 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层 ...

  10. ui-sref

    angularjs中路由跳转可以在模板页面上使用ui-sref="a-state({param1: value})"; 如果想为当前state的导航按钮添加一个激活class,可以 ...