1. 简述

本工具基于https://github.com/adnanh/webhook.git定制, Dockerfile地址: https://github.com/cnrancher/docker-webhook.git

镜像下载地址: registry.cn-shenzhen.aliyuncs.com/rancher/webhook:latest

  1. 支持镜像仓库类型:

    阿里云镜像仓库: https://cr.console.aliyun.com
    Docker Hub: http://hub.docker.com
    自定义 webhooks

  2. 支持邮件通知

2. 准备配置文件

建议把 webhooks 作为系统服务运行在system项目下

  1. 登录 Rancher UI 切换到system项目下,然后依次进入 资源\配置映射,点击页面右上角的添加配置映射

  2. 修改模板中对应的参数:

    • <webhooks_id>: 此webhooks-id具有唯一性,不能重复。建议设置为服务名,比如cnrancher_website
    • <token>: 设置一个 token 值用于匹配校验;
    • <workload>: 指定一个应用,书写格式为类型/Workload,例如: deployment/webhooks、daemonset/webhooks
    • <namespaces>: 指定服务所在的命名空间;
    • <container>: 指定容器名称,对于一个有多容器的 Pod,升级时需要指定容器名称;
    • <MAIL_TO>: 收件人邮箱地址;
    • <NET_TYPE>: 如果阿里云的镜像仓库,可在 url 中添加net_type指定网络类型: 1.公共网络: 不指定默认为公共网络,2.专有网络: net_type=vpc,3.经典网络: net_type=internal
    1. [
    2. {
    3. "id": "<webhooks-id>",
    4. "execute-command": "/webhooks.sh",
    5. "command-working-directory": "/home",
    6. "response-message": "I got the payload!",
    7. "include-command-output-in-response": true,
    8. "include-command-output-in-response-on-error": true,
    9. "trigger-rule-mismatch-http-response-code": 500,
    10. "response-headers": [
    11. {
    12. "name": "Access-Control-Allow-Origin",
    13. "value": "*"
    14. }
    15. ],
    16. "pass-arguments-to-command": [
    17. {
    18. "source": "entire-payload"
    19. }
    20. ],
    21. "pass-environment-to-command": [
    22. {
    23. "envname": "APP_NS",
    24. "source": "url",
    25. "name": "ns"
    26. },
    27. {
    28. "envname": "APP_WORKLOAD",
    29. "source": "url",
    30. "name": "workload"
    31. },
    32. {
    33. "envname": "APP_CONTAINER",
    34. "source": "url",
    35. "name": "container"
    36. },
    37. {
    38. "envname": "REPO_TYPE",
    39. "source": "url",
    40. "name": "repo_type"
    41. },
    42. {
    43. "envname": "NET_TYPE",
    44. "source": "url",
    45. "name": "net_type"
    46. },
    47. {
    48. "envname": "MAIL_TO",
    49. "source": "string",
    50. "name": "<MAIL_TO>"
    51. }
    52. ],
    53. "trigger-rule": {
    54. "and": [
    55. {
    56. "match": {
    57. "type": "value",
    58. "value": "<token>",
    59. "parameter": {
    60. "source": "url",
    61. "name": "token"
    62. }
    63. }
    64. },
    65. {
    66. "match": {
    67. "type": "value",
    68. "value": "<namespaces>",
    69. "parameter": {
    70. "source": "url",
    71. "name": "ns"
    72. }
    73. }
    74. },
    75. {
    76. "match": {
    77. "type": "value",
    78. "value": "<workload>",
    79. "parameter": {
    80. "source": "url",
    81. "name": "workload"
    82. }
    83. }
    84. },
    85. {
    86. "match": {
    87. "type": "value",
    88. "value": "<container>",
    89. "parameter": {
    90. "source": "url",
    91. "name": "container"
    92. }
    93. }
    94. }
    95. ]
    96. }
    97. }
    98. ]
  3. 填写添加配置映射参数,其中:

    • 名称: 可以随意填写;
    • : 以.json结尾的文件名,比如cnrancher.json;
    • : 设置为上一步中修改的配置文件;
    • 如果有多个服务,可以添加多个键值对,如图:

    image-20190314173250612

3. webhooks 安装

依次点击 system项目\工作负载\工作负载,点击右侧部署服务。

  1. 配置服务名称和镜像

    registry.cn-shenzhen.aliyuncs.com/rancher/webhook

    image-20190314173915552

  2. 对外服务

    • 服务默认监听端口为9000,如果使用 NodePort 提供服务,则安以下方式配置;

    image-20190923184019854

    • 如果使用负载均衡服务,在负载均衡页面添加相应规则,如果使用 https,记得配置 ssl 证书;

    image-20190923184601819

  3. 配置环境变量

    WEBHOOK_CMD=-template: 系统命令;
    MAIL_SMTP_PORT=: 邮箱 SMTP 服务器端口;
    MAIL_SMTP_SERVER=: 邮箱 SMTP 服务器地址,(需要 base64 加密: echo | base64 );
    MAIL_FROM= : 发件人邮箱,(需要 base64 加密: echo <发件人邮箱> | base64 );
    MAIL_PASSWORD=: 发件人邮箱密码,(需要 base64 加密: echo <密码> | base64 );
    MAIL_CACERT=: 自签名 CA 证书,邮箱服务器采用自签名 ssl 证书时使用(需要 base64 加密: cat | base64 );
    MAIL_TLS_CHECK=: 是否开启 TLS 认证(false/true,默认 true);

    请以文字说明为准,图片是早期版本截图,图片中有些地方未做 base64 加密

    • 常用邮箱配置(qq,163 等)

    image-20190315232842668

    • 自签名证书邮箱服务器

    image-20190315233201822

    • 不启用 TLS 认证邮箱

    image-20190315233320815

  4. 配置健康检查

    端口: 9000

    image-20190314174424562

  5. 配置数据卷

    • 选择配置映射卷

    image-20190314174524973

    • 配置映射名: 选择前面创建的配置映射;

    • 容器路径: /etc/webhook/source;

    • 其他参数保持默认;

  1. [![image-20190314174834334](/projects/rancher-2.4.4-zh/img/rancher/old-doc/image-20190314174834334.png)](/projects/rancher-2.4.4-zh/img/rancher/old-doc/image-20190314174834334.png)
  1. 最后点击启动,启动后查看日志,可以看到当前监听的服务

    image-20190314175308608

  2. 设置serviceaccounts

    这一步相对比较重要,webhooks 服务需要serviceaccounts才可以正常的与 K8S 通信。因为目前 Rancher UI 不支持设置serviceaccounts,所以需要编辑yaml文件来配置serviceaccounts。为了方便,这里复用了 rancher 组件使用的serviceaccounts账号cattle,具有集群管理员角色,您也可以根据需要定制serviceaccounts角色。

    • 如图,选择 查看/编辑YAML

    image-20190314212013510

    • securityContext: {} 下边添加serviceAccount: cattleserviceAccountName: cattle

    image-20190314212124745

    • 最后点击保存

4. webhooks 触发地址

  1. http(s)://<webhooks_url>/hooks/\
  2. <webhooks_id>?\
  3. token=<token>&\
  4. ns=<namespaces>&\
  5. workload=<workload>&\
  6. container=<container>&\
  7. repo_type=<repo_type>
  1. 如果是阿里云的镜像仓库,可在 url 中添加net_type指定网络类型:

    • 公共网络:

      如果不指定,则默认为公共网络拉取镜像

    • 专有网络:

      net_type=vpc

    • 经典网络:

      net_type=internal

  1. 其中`<webhooks_id>、<namespaces>、<workload>、<container>`对应模板中的参数,`<repo_type>`支持:`aliyun``dockerhub``custom`

5. 配置仓库触发

5.1. Aliyun

  1. 浏览器访问https://cr.console.aliyun.com进入容器镜像服务管理界面;

  2. 选择一个需要添加自动触发功能的仓库,点击右侧的管理;

  3. 在切换的新窗口左上角选择触发器;

    image-20190314181124857

  4. webhooks 触发消息示例:

    1. {
    2. "push_data": {
    3. "digest": "sha256:f66daa126e9fcac4e2d0b7131e78ffd5d8e0012a1e6cb150a953e5be8da5d 980",
    4. "pushed_at": "2019-03-13 23:38:07",
    5. "tag": "latest"
    6. },
    7. "repository": {
    8. "date_created": "2019-03-05 13:47:43",
    9. "name": "webhook",
    10. "namespace": "rancher_cn",
    11. "region": "cn-shanghai",
    12. "repo_authentication_type": "NO_CERTIFIED",
    13. "repo_full_name": "rancher_cn/webhook",
    14. "repo_origin_type": "NO_CERTIFIED",
    15. "repo_type": "PUBLIC"
    16. }
    17. }

5.2. Docker

  1. 浏览器访问https://cloud.docker.com/repository/list,输入账号和密码后将进入仓库列表;

  2. 点击需要添加 webhooks 仓库,然后点击 webhooks;

    image-20190314182034766

  3. 填写相关参数,点击右侧的加号;

    image-20190314182530479

  4. webhooks 触发消息示例:

    1. {
    2. "push_data": {
    3. "pushed_at": 1552553567,
    4. "images": [],
    5. "tag": "latest",
    6. "pusher": "hongxiaolu"
    7. },
    8. "callback_url": "",
    9. "repository": {
    10. "status": "Active",
    11. "description": "iperf3",
    12. "is_trusted": true,
    13. "full_description": "# iperf3\niperf3\n",
    14. "repo_url": "",
    15. "owner": "hongxiaolu",
    16. "is_official": false,
    17. "is_private": false,
    18. "name": "iperf3",
    19. "namespace": "hongxiaolu",
    20. "star_count": 0,
    21. "comment_count": 0,
    22. "date_created": 1540013520,
    23. "dockerfile": "# ",
    24. "repo_name": "hongxiaolu/iperf3"
    25. }
    26. }

5.3. 自定义 webhooks

如果是使用 Jenkins 自定义构建镜像,可以设置repo_type\=custom

在 Jenkins 构建task中,在镜像push操作后增加一个执行shell命令的步骤。这个操作主要是在镜像成功推送到镜像仓库后发出POST消息去触发 webhooks,这步中需要把上一步推送的镜像仓库地址,镜像命名空间,镜像名,以及镜像tag作为变量传递到这一步,这样在发送POST消息才可以把相关的镜像信息传递给 webhooks,从而触发服务升级。

示例POST消息:

  1. curl -X POST \
  2. 'http(s)://<webhooks_url>/hooks/<webhooks_id>?\
  3. token=<token>&\
  4. ns=<namespaces>&\
  5. workload=<workload>&\
  6. container=<container>&\
  7. repo_type=custom' \
  8. -H 'Content-Type: application/json' \
  9. -H 'cache-control: no-cache' \
  10. -d '{
  11. "push_data": {
  12. "tag": "${images_tag}"
  13. },
  14. "repository": {
  15. "repo_url": ${images_repo_url},
  16. "name": "${images_name}",
  17. "namespace": "${images_namespace}"
  18. }
  19. }'

6. 触发 webhooks

  1. 配置完以上参数,提交代码到 git 仓库后将会自动触发阿里云仓库或者 dockerhub 的自动构建,创建自动构建方法请自行查阅相关文档。

    image-20190314212701691

    image-20190314213123621

  2. 当镜像构建完成并推送到仓库后,会触发 webhooks 消息到预先配置的地址,从触发器也可以查看历史记录。

    image-20190314213215303

  3. webhooks 服务收到消息后,会马上触发服务的升级。查看 webhooks 服务的日志,可以看到已经成功升级。

    image-20190314213051682

    image-20190314205425237