肝2022天下杯,怒写企业级镜像私仓Docker+Harbor实践

打印 上一主题 下一主题

主题 650|帖子 650|积分 1950

2022-12-09 揭幕2022卡塔尔天下杯4强比赛的第一天,越来越精彩了
同时记录程序猿的成长~
1.配景

由于盼望搭建一个企业级CICD的环境,开始尝试常规的gitlab+jenkins+k8s+docker harbor+springboot开始练手
其中版本如下:
1.gitlab: GitLab Community Edition 12.2.1
2.jenkins:
3.Mac kubernetes: v1.24.2
4.Mac docker: Engine: 20.10.17 Compose: 1.29.2
5.harbor: v2.5.1-b0506782
1.1 条件



  • 企业级代码库 Gitlab
  • 企业级CICD持续化集成工具 Jenkins
  • 企业级容器化技能架构:Docker+Kubernetes
以下就开始搭建Harbor,构造企业级私有化镜像仓库
1.2 为什么选择Harbor?

因为它的功能相对较强,可以或许基本满足企业级运作
上图先浅看一下

2.使用离线包进行手动安装

请注意需要安装在有Docker指令的呆板上
2.1 下载资源包

从 github releases 官方下载offline离线包,这边选取一个稳固版本v2.5.1(online版本需要在线去持续下载资源,为了克制失败,直接下载离线包更为简朴)
  1. #通过wget
  2. wget https://github.com/goharbor/harbor/releases/download/v2.5.1/harbor-online-installer-v2.5.1.tgz
  3. #通过页面
  4. #直接点击Asserts中harbor-online-installer-v2.5.1.tgz 下载即可
复制代码
2.2 解压并配置

tgz的包直接解压
  1. tar zxf harbor-online-installer-v2.5.1.tgz ./harbor
复制代码
解压出来后的目录结构:
  1. -rw-r--r--@ 1 xxxxx  test      11347  5 26 23:59 LICENSE
  2. drwxr-xr-x  3 xxxxx  test         96  8 29 18:20 common
  3. -rw-r--r--@ 1 xxxxx  test       3361  5 26 23:59 common.sh
  4. -rw-r--r--  1 xxxxx  test       6057  8 30 08:25 docker-compose.yml
  5. -rw-r--r--@ 1 xxxxx  test  664492716  5 27 00:00 harbor.v2.5.1.tar.gz
  6. -rw-r--r--@ 1 xxxxx  test       9917  5 26 23:59 harbor.yml.tmpl
  7. -rwxr-xr-x@ 1 xxxxx  test       2500  5 26 23:59 install.sh
  8. -rwxr-xr-x@ 1 xxxxx  test       1881  5 26 23:59 prepare
复制代码
脚本有prepare install common三个,其中common是根本脚本,我们只会直接调用prepare和install两个脚本
其中harbor.yml.tmpl为焦点配置的模板,我们可以直接修改文件名称,或者拷贝一个文件来作为焦点配置文件,文件内容如下:
  1. # Configuration file of Harbor
  2. # The IP address or hostname to access admin UI and registry service.
  3. # DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
  4. # 以下主要填写仓库的IP或域名,最好为域名形式
  5. hostname: 127.0.0.1
  6. # http related config
  7. # 以下为HTTP的配置形式
  8. http:
  9.   # port for http, default is 80. If https enabled, this port will redirect to https port
  10.   # 默认80,基本是要调整
  11.   port: 18080
  12. # https related config
  13. # 以下为HTTPs的配置形式,正常生产环境需要打开,需要配置https证书
  14. #https:
  15.   # https port for harbor, default is 443
  16.   #port: 443
  17.   # The path of cert and key files for nginx
  18.   #certificate: /your/certificate/path
  19.   #private_key: /your/private/key/path
  20. # # Uncomment following will enable tls communication between all harbor components
  21. # internal_tls:
  22. #   # set enabled to true means internal tls is enabled
  23. #   enabled: true
  24. #   # put your cert and key files on dir
  25. #   dir: /etc/harbor/tls/internal
  26. # Uncomment external_url if you want to enable external proxy
  27. # And when it enabled the hostname will no longer used
  28. # external_url: https://reg.mydomain.com:8433
  29. # The initial password of Harbor admin
  30. # It only works in first time to install harbor
  31. # Remember Change the admin password from UI after launching Harbor.
  32. # 以下是Harbor admin的默认密码,可修改
  33. harbor_admin_password: Harbor123
  34. # Harbor DB configuration
  35. database:
  36.   # The password for the root user of Harbor DB. Change this before any production use.
  37.   # 内置DB的默认密码
  38.   password: root123
  39.   # The maximum number of connections in the idle connection pool. If it <=0, no idle connections are retained.
  40.   max_idle_conns: 100
  41.   # The maximum number of open connections to the database. If it <= 0, then there is no limit on the number of open connections.
  42.   # Note: the default number of connections is 1024 for postgres of harbor.
  43.   max_open_conns: 900
  44. # The default data volume
  45. # 挂载的目录
  46. data_volume: /files/docker/harbor
  47. # Harbor Storage settings by default is using /data dir on local filesystem
  48. # Uncomment storage_service setting If you want to using external storage
  49. # storage_service:
  50. #   # ca_bundle is the path to the custom root ca certificate, which will be injected into the truststore
  51. #   # of registry's and chart repository's containers.  This is usually needed when the user hosts a internal storage with self signed certificate.
  52. #   ca_bundle:
  53. #   # storage backend, default is filesystem, options include filesystem, azure, gcs, s3, swift and oss
  54. #   # for more info about this configuration please refer https://docs.docker.com/registry/configuration/
  55. #   filesystem:
  56. #     maxthreads: 100
  57. #   # set disable to true when you want to disable registry redirect
  58. #   redirect:
  59. #     disabled: false
  60. # Trivy configuration
  61. #
  62. # Trivy DB contains vulnerability information from NVD, Red Hat, and many other upstream vulnerability databases.
  63. # It is downloaded by Trivy from the GitHub release page https://github.com/aquasecurity/trivy-db/releases and cached
  64. # in the local file system. In addition, the database contains the update timestamp so Trivy can detect whether it
  65. # should download a newer version from the Internet or use the cached one. Currently, the database is updated every
  66. # 12 hours and published as a new release to GitHub.
  67. trivy:
  68.   # ignoreUnfixed The flag to display only fixed vulnerabilities
  69.   ignore_unfixed: false
  70.   # skipUpdate The flag to enable or disable Trivy DB downloads from GitHub
  71.   #
  72.   # You might want to enable this flag in test or CI/CD environments to avoid GitHub rate limiting issues.
  73.   # If the flag is enabled you have to download the `trivy-offline.tar.gz` archive manually, extract `trivy.db` and
  74.   # `metadata.json` files and mount them in the `/home/scanner/.cache/trivy/db` path.
  75.   skip_update: false
  76.   #
  77.   # The offline_scan option prevents Trivy from sending API requests to identify dependencies.
  78.   # Scanning JAR files and pom.xml may require Internet access for better detection, but this option tries to avoid it.
  79.   # For example, the offline mode will not try to resolve transitive dependencies in pom.xml when the dependency doesn't
  80.   # exist in the local repositories. It means a number of detected vulnerabilities might be fewer in offline mode.
  81.   # It would work if all the dependencies are in local.
  82.   # This option doesn’t affect DB download. You need to specify "skip-update" as well as "offline-scan" in an air-gapped environment.
  83.   offline_scan: false
  84.   #
  85.   # insecure The flag to skip verifying registry certificate
  86.   insecure: false
  87.   # github_token The GitHub access token to download Trivy DB
  88.   #
  89.   # Anonymous downloads from GitHub are subject to the limit of 60 requests per hour. Normally such rate limit is enough
  90.   # for production operations. If, for any reason, it's not enough, you could increase the rate limit to 5000
  91.   # requests per hour by specifying the GitHub access token. For more details on GitHub rate limiting please consult
  92.   # https://developer.github.com/v3/#rate-limiting
  93.   #
  94.   # You can create a GitHub token by following the instructions in
  95.   # https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
  96.   #
  97.   # github_token: xxx
  98. jobservice:
  99.   # Maximum number of job workers in job service
  100.   max_job_workers: 10
  101. notification:
  102.   # Maximum retry count for webhook job
  103.   webhook_job_max_retry: 10
  104. chart:
  105.   # Change the value of absolute_url to enabled can enable absolute url in chart
  106.   absolute_url: disabled
  107. # Log configurations
  108. log:
  109.   # options are debug, info, warning, error, fatal
  110.   level: info
  111.   # configs for logs in local storage
  112.   local:
  113.     # Log files are rotated log_rotate_count times before being removed. If count is 0, old versions are removed rather than rotated.
  114.     rotate_count: 50
  115.     # Log files are rotated only if they grow bigger than log_rotate_size bytes. If size is followed by k, the size is assumed to be in kilobytes.
  116.     # If the M is used, the size is in megabytes, and if G is used, the size is in gigabytes. So size 100, size 100k, size 100M and size 100G
  117.     # are all valid.
  118.     rotate_size: 200M
  119.     # The directory on your host that store log
  120.     location: /var/log/harbor
  121.   # Uncomment following lines to enable external syslog endpoint.
  122.   # external_endpoint:
  123.   #   # protocol used to transmit log to external endpoint, options is tcp or udp
  124.   #   protocol: tcp
  125.   #   # The host of external endpoint
  126.   #   host: localhost
  127.   #   # Port of external endpoint
  128.   #   port: 5140
  129. #This attribute is for migrator to detect the version of the .cfg file, DO NOT MODIFY!
  130. _version: 2.5.0
  131. # Uncomment external_database if using external database.
  132. # external_database:
  133. #   harbor:
  134. #     host: harbor_db_host
  135. #     port: harbor_db_port
  136. #     db_name: harbor_db_name
  137. #     username: harbor_db_username
  138. #     password: harbor_db_password
  139. #     ssl_mode: disable
  140. #     max_idle_conns: 2
  141. #     max_open_conns: 0
  142. #   notary_signer:
  143. #     host: notary_signer_db_host
  144. #     port: notary_signer_db_port
  145. #     db_name: notary_signer_db_name
  146. #     username: notary_signer_db_username
  147. #     password: notary_signer_db_password
  148. #     ssl_mode: disable
  149. #   notary_server:
  150. #     host: notary_server_db_host
  151. #     port: notary_server_db_port
  152. #     db_name: notary_server_db_name
  153. #     username: notary_server_db_username
  154. #     password: notary_server_db_password
  155. #     ssl_mode: disable
  156. # Uncomment external_redis if using external Redis server
  157. # external_redis:
  158. #   # support redis, redis+sentinel
  159. #   # host for redis: <host_redis>:<port_redis>
  160. #   # host for redis+sentinel:
  161. #   #  <host_sentinel1>:<port_sentinel1>,<host_sentinel2>:<port_sentinel2>,<host_sentinel3>:<port_sentinel3>
  162. #   host: redis:6379
  163. #   password:
  164. #   # sentinel_master_set must be set to support redis+sentinel
  165. #   #sentinel_master_set:
  166. #   # db_index 0 is for core, it's unchangeable
  167. #   registry_db_index: 1
  168. #   jobservice_db_index: 2
  169. #   chartmuseum_db_index: 3
  170. #   trivy_db_index: 5
  171. #   idle_timeout_seconds: 30
  172. # Uncomment uaa for trusting the certificate of uaa instance that is hosted via self-signed cert.
  173. # uaa:
  174. #   ca_file: /path/to/ca
  175. # Global proxy
  176. # Config http proxy for components, e.g. http://my.proxy.com:3128
  177. # Components doesn't need to connect to each others via http proxy.
  178. # Remove component from `components` array if want disable proxy
  179. # for it. If you want use proxy for replication, MUST enable proxy
  180. # for core and jobservice, and set `http_proxy` and `https_proxy`.
  181. # Add domain to the `no_proxy` field, when you want disable proxy
  182. # for some special registry.
  183. proxy:
  184.   http_proxy:
  185.   https_proxy:
  186.   no_proxy:
  187.   components:
  188.     - core
  189.     - jobservice
  190.     - trivy
  191. # metric:
  192. #   enabled: false
  193. #   port: 9090
  194. #   path: /metrics
  195. # Trace related config
  196. # only can enable one trace provider(jaeger or otel) at the same time,
  197. # and when using jaeger as provider, can only enable it with agent mode or collector mode.
  198. # if using jaeger collector mode, uncomment endpoint and uncomment username, password if needed
  199. # if using jaeger agetn mode uncomment agent_host and agent_port
  200. # trace:
  201. #   enabled: true
  202. #   # set sample_rate to 1 if you wanna sampling 100% of trace data; set 0.5 if you wanna sampling 50% of trace data, and so forth
  203. #   sample_rate: 1
  204. #   # # namespace used to differenciate different harbor services
  205. #   # namespace:
  206. #   # # attributes is a key value dict contains user defined attributes used to initialize trace provider
  207. #   # attributes:
  208. #   #   application: harbor
  209. #   # # jaeger should be 1.26 or newer.
  210. #   # jaeger:
  211. #   #   endpoint: http://hostname:14268/api/traces
  212. #   #   username:
  213. #   #   password:
  214. #   #   agent_host: hostname
  215. #   #   # export trace data by jaeger.thrift in compact mode
  216. #   #   agent_port: 6831
  217. #   # otel:
  218. #   #   endpoint: hostname:4318
  219. #   #   url_path: /v1/traces
  220. #   #   compression: false
  221. #   #   insecure: true
  222. #   #   timeout: 10s
  223. # enable purge _upload directories
  224. upload_purging:
  225.   enabled: true
  226.   # remove files in _upload directories which exist for a period of time, default is one week.
  227.   age: 168h
  228.   # the interval of the purge operations
  229.   interval: 24h
  230.   dryrun: false
复制代码
2.3 将Harbor仓库地址配置在Docker的署理仓库地址中

找到自己当前部署Docker daemon.json所在目录
  1. cat /etc/docker/daemon.json
  2. {
  3.   "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"],
  4.   "insecure-registries" : ["127.0.0.1:18080"]
  5. }
复制代码
配置保存后,需要重新加载启动一下Docker服务使其生效
2.4 启动Harbor

  1. #使用当前目录下install指令即可启动
  2. ./install.sh
复制代码
假如修改了harbor.yml则需要先执行 prepare指令,再执行install指令
别的还可以在体系上将服务固定下来,不用手动启动,将其设置为基于docker服务的体系服务
  1. [Unit]
  2. Description=Harbor
  3. After=docker.service systemd-networkd.service systemd-resolved.service
  4. Requires=docker.service
  5. Documentation=http://github.com/vmware/harbor
  6. [Service]
  7. Type=simple
  8. Restart=on-failure
  9. RestartSec=5
  10. ExecStart=/usr/local/bin/docker-compose -f  [harbor dir]/docker-compose.yml up      ##中括号部分修改成harbor的文件夹
  11. ExecStop=/usr/local/bin/docker-compose -f [harbor dir]/docker-compose.yml down      ##中括号部分修改成harbor的文件夹
  12. [Install]
  13. WantedBy=multi-user.target
复制代码
2.5 校验仓库的登录与推送

  1. # 一、使用页面登录,直接输入 127.0.0.1:18080的地址,即可出现登录页,可使用配置文件中的admin密码进行设置
  2. # 二、使用指令验证
  3. a@b:/etc/docker$ docker login 127.0.0.1:18080
  4. Authenticating with existing credentials...
  5. Login Succeeded
  6. #制作镜像
  7. docker tag nginx:latest 127.0.0.1:18080/cicd/nginx:latest
  8. #查看镜像
  9. a@b:/etc/docker$ docker images
  10. REPOSITORY                                                TAG                                                                          IMAGE ID       CREATED         SIZE
  11. 127.0.0.1:18080/cicd/nginx                                latest                                                                       2b7d6430f78d   6 days ago      142MB
  12. #推送镜像
  13. a@v:/etc/docker$  docker push 127.0.0.1:18080/cicd/nginx:latest
  14. The push refers to repository [127.0.0.1:18080/cicd/nginx]
  15. 73993eeb8aa2: Pushed
  16. 2c31eef17db8: Pushed
  17. 7b9055fc8058: Pushed
  18. 04ab349b7b3b: Pushed
  19. 226117031573: Pushed
  20. 6485bed63627: Pushed
  21. latest: digest: sha256:89020cd33be2767f3f894484b8dd77bc2e5a1ccc864350b92c53262213257dfc size: 1570
  22. #最终能够在页面查看到对应仓库中的镜像
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

玛卡巴卡的卡巴卡玛

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表