18.4 配置Registry

Docker Registry提供了一些样例配置,用户可以直接使用它们来进行开发或生产部署。

笔者将以下面的示例配置来介绍如何使用配置文件来管理私有仓库。

18.4.1 示例配置

代码如下:


  1. version: 0.1
  2. log:
  3. level: debug
  4. fields:
  5. service: registry
  6. environment: development
  7. hooks:
  8. - type: mail
  9. disabled: true
  10. levels:
  11. - panic
  12. options:
  13. smtp:
  14. addr: mail.example.com:25
  15. username: mailuser
  16. password: password
  17. insecure: true
  18. from: sender@example.com
  19. to:
  20. - errors@example.com
  21. storage:
  22. delete:
  23. enabled: true
  24. cache:
  25. blobdescriptor: redis
  26. filesystem:
  27. rootdirectory: /var/lib/registry
  28. maintenance:
  29. uploadpurging:
  30. enabled: false
  31. http:
  32. addr: :5000
  33. debug:
  34. addr: localhost:5001
  35. headers:
  36. X-Content-Type-Options: [nosniff]
  37. redis:
  38. addr: localhost:6379
  39. pool:
  40. maxidle: 16
  41. maxactive: 64
  42. idletimeout: 300s
  43. dialtimeout: 10ms
  44. readtimeout: 10ms
  45. writetimeout: 10ms
  46. notifications:
  47. endpoints:
  48. - name: local-5003
  49. url: http://localhost:5003/callback
  50. headers:
  51. Authorization: [Bearer <an example token>]
  52. timeout: 1s
  53. threshold: 10
  54. backoff: 1s
  55. disabled: true
  56. - name: local-8083
  57. url: http://localhost:8083/callback
  58. timeout: 1s
  59. threshold: 10
  60. backoff: 1s
  61. disabled: true
  62. health:
  63. storagedriver:
  64. enabled: true
  65. interval: 10s
  66. threshold: 3

18.4.2 选项

这些选项以yaml文件格式提供,用户可以直接进行修改,也可以添加自定义的模板段。

默认情况下,变量可以从环境变量中读取,例如log.level:debug可以配置为:


  1. export LOG_LEVEL=debug

比较重要的选项包括版本信息、log选项、hooks选项、存储选项、认证选项、HTTP选项、通知选项、redis选项、健康监控选项、代理选项和验证选项等。下面分别介绍这些选项。

1.版本信息


  1. version0.1

2.log选项

日志相关:


  1. log:
  2. level: debug
  3. formatter: text
  4. fields:
  5. service: registry
  6. environment: staging

参数说明:

·level:字符串类型,标注输出调试信息的级别,包括debug、info、warn、error。

·fomatter:字符串类型,日志输出的格式,包括text、json、logstash等。

·fields:增加到日志输出消息中的键值对,可以用于过滤日志。

3.hooks选项

配置当仓库发生异常时,通过邮件发送日志时的参数:


  1. hooks:
  2. - type: mail
  3. levels:
  4. - panic
  5. options:
  6. smtp:
  7. addr: smtp.sendhost.com:25
  8. username: sendername
  9. password: password
  10. insecure: true
  11. from: name@sendhost.com
  12. to:
  13. - name@receivehost.com

4.存储选项

storage选项将配置存储的引擎,默认支持包括本地文件系统、Google云存储、AWS S3云存储和OpenStack Swift分布式存储等,如下所示:


  1. storage:
  2. filesystem:
  3. rootdirectory: /var/lib/registry
  4. azure:
  5. accountname: accountname
  6. accountkey: base64encodedaccountkey
  7. container: containername
  8. gcs:
  9. bucket: bucketname
  10. keyfile: /path/to/keyfile
  11. rootdirectory: /gcs/object/name/prefix
  12. s3:
  13. accesskey: awsaccesskey
  14. secretkey: awssecretkey
  15. region: us-west-1
  16. regionendpoint: http://myobjects.local
  17. bucket: bucketname
  18. encrypt: true
  19. keyid: mykeyid
  20. secure: true
  21. v4auth: true
  22. chunksize: 5242880
  23. multipartcopychunksize: 33554432
  24. multipartcopymaxconcurrency: 100
  25. multipartcopythresholdsize: 33554432
  26. rootdirectory: /s3/object/name/prefix
  27. swift:
  28. username: username
  29. password: password
  30. authurl: https://storage.myprovider.com/auth/v1.0 or https://storage.
  31. myprovider.com/v2.0 or https://storage.myprovider.com/v3/auth
  32. tenant: tenantname
  33. tenantid: tenantid
  34. domain: domain name for Openstack Identity v3 API
  35. domainid: domain id for Openstack Identity v3 API
  36. insecureskipverify: true
  37. region: fr
  38. container: containername
  39. rootdirectory: /swift/object/name/prefix
  40. oss:
  41. accesskeyid: accesskeyid
  42. accesskeysecret: accesskeysecret
  43. region: OSS region name
  44. endpoint: optional endpoints
  45. internal: optional internal endpoint
  46. bucket: OSS bucket
  47. encrypt: optional data encryption setting
  48. secure: optional ssl setting
  49. chunksize: optional size valye
  50. rootdirectory: optional root directory
  51. inmemory:
  52. delete:
  53. enabled: false
  54. cache:
  55. blobdescriptor: inmemory
  56. maintenance:
  57. uploadpurging:
  58. enabled: true
  59. age: 168h
  60. interval: 24h
  61. dryrun: false
  62. redirect:
  63. disable: false

比较重要的选项如下:

·maintenance:配置维护相关的功能,包括对孤立旧文件的清理、开启只读模式等;

·delete:是否允许删除镜像功能,默认关闭;

·cache:开启对镜像层元数据的缓存功能,默认开启;

5.认证选项

对认证类型的配置,如下所示:


  1. auth:
  2. silly:
  3. realm: silly-realm
  4. service: silly-service
  5. token:
  6. realm: token-realm
  7. service: token-service
  8. issuer: registry-token-issuer
  9. rootcertbundle: /root/certs/bundle
  10. htpasswd:
  11. realm: basic-realm
  12. path: /path/to/htpasswd

比较重要的选项如下:

·silly:仅供测试使用,只要请求头带有认证域即可,不做内容检查;

·token:基于token的用户认证,适用于生产环境,需要额外的token服务来支持;

·htpasswd:基于Apache htpasswd密码文件的权限检查。

6.HTTP选项

跟HTTP服务相关的配置,如下所示:


  1. http:
  2. addr: localhost:5000
  3. net: tcp
  4. prefix: /my/nested/registry/
  5. host: https://myregistryaddress.org:5000
  6. secret: asecretforlocaldevelopment
  7. relativeurls: false
  8. tls:
  9. certificate: /path/to/x509/public
  10. key: /path/to/x509/private
  11. clientcas:
  12. - /path/to/ca.pem
  13. - /path/to/another/ca.pem
  14. letsencrypt:
  15. cachefile: /path/to/cache-file
  16. email: emailused@letsencrypt.com
  17. debug:
  18. addr: localhost:5001
  19. headers:
  20. X-Content-Type-Options: [nosniff]
  21. http2:
  22. disabled: false

其中的参数如下:

·addr:必选,服务监听地址;

·secret:必选,跟安全相关的随机字符串,用户可以自己定义;

·tls:证书相关的文件路径信息;

·http2:是否开启http2支持,默认关闭。

7.通知选项

有事件发生时候的通知系统。


  1. notifications:
  2. endpoints:
  3. - name: alistener
  4. disabled: false
  5. url: https://my.listener.com/event
  6. headers: <http.Header>
  7. timeout: 500
  8. threshold: 5
  9. backoff: 1000

8.redis选项

Registry可以用Redis来缓存文件块,这里可以配置相关选项:


  1. redis:
  2. addr: localhost:6379
  3. password: asecret
  4. db: 0
  5. dialtimeout: 10ms
  6. readtimeout: 10ms
  7. writetimeout: 10ms
  8. pool:
  9. maxidle: 16
  10. maxactive: 64
  11. idletimeout: 300s

9.健康监控选项

跟健康监控相关,主要是对配置服务进行检测判断系统状态,如下所示:


  1. health:
  2. storagedriver:
  3. enabled: true
  4. interval: 10s
  5. threshold: 3
  6. file:
  7. - file: /path/to/checked/file
  8. interval: 10s
  9. http:
  10. - uri: http://server.to.check/must/return/200
  11. headers:
  12. Authorization: [Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==]
  13. statuscode: 200
  14. timeout: 3s
  15. interval: 10s
  16. threshold: 3
  17. tcp:
  18. - addr: redis-server.domain.com:6379
  19. timeout: 3s
  20. interval: 10s
  21. threshold: 3

默认并未启用。

10.代理选项

配置Registry作为一个pull代理,从远端(目前仅支持官方仓库)下拉Docker镜像,如下所示:


  1. proxy:
  2. remoteurl: https://registry-1.docker.io
  3. username: [username]
  4. password: [password]

之后,用户可以通过如下命令来配置Docker使用代理:


  1. $ docker --registry-mirror=https://myrepo.com:5000 daemon

11.验证选项

限定来自指定地址的客户端才可以执行push操作:


  1. validation:
  2. enabled: true
  3. manifests:
  4. urls:
  5. allow:
  6. - ^https?://([^/]+\.)*example\.com/
  7. deny:
  8. - ^https?://www\.example\.com/