起因
由于我们团队的开发环境是使用jenkins
+Harbor
+rancher2
进行部署ci/cd的,所以需要进行webhook调用
但是在通过官方文档提供的方法部署后出现问题,于是进行排查并进行修复,所以才有了这篇文章
1 配置rancher配置文件
建议把webhooks作为系统服务运行在system项目下
- 登录Rancher UI切换到
system
项目下,然后依次进入资源\配置映射
,点击页面右上角的添加配置映射
。 - 修改模板中对应的参数:
<webhooks_id>
: 此webhooks-id具有唯一性,不能重复。建议设置为服务名,比如springboot-demo
;<token>
: 设置一个token值用于匹配校验,自己生成,建议使用sha256摘要算法
对服务名进行摘要生成;<workload>
: 指定一个应用,书写格式为类型/服务名
,例如:deployment/springboot-demo
、daemonset/webhooks
;<namespaces>
: 指定服务所在的命名空间,如果你在rancher部署镜像没有特别设置,就是default
;<container>
: 指定容器名称,对于一个有多容器的Pod,升级时需要指定容器名称,一般都是设置服务名,比如springboot-demo
;<MAIL_TO>
: 收件人邮箱地址,(可选);<NET_TYPE>
: 发送webhook请求时发的,一般不指定就行了,如果阿里云的镜像仓库,可在url中添加net_type指定网络类型: 1.公共网络: 不指定默认为公共网络,2.专有网络:net_type=vpc
,3.经典网络:net_type=internal
;
[
{
"id": "<webhooks-id>",
"execute-command": "/webhooks.sh",
"command-working-directory": "/home",
"response-message": "I got the payload!",
"include-command-output-in-response": true,
"include-command-output-in-response-on-error": true,
"trigger-rule-mismatch-http-response-code": 500,
"response-headers":
[
{
"name": "Access-Control-Allow-Origin",
"value": "*"
}
],
"pass-arguments-to-command":
[
{
"source": "entire-payload"
}
],
"pass-environment-to-command":
[
{
"envname": "APP_NS",
"source": "url",
"name": "ns"
},
{
"envname": "APP_WORKLOAD",
"source": "url",
"name": "workload"
},
{
"envname": "APP_CONTAINER",
"source": "url",
"name": "container"
},
{
"envname": "REPO_TYPE",
"source": "url",
"name": "repo_type"
},
{
"envname": "NET_TYPE",
"source": "url",
"name": "net_type"
},
{
"envname": "MAIL_TO",
"source": "string",
"name": "<MAIL_TO>"
}
],
"trigger-rule":
{
"and":
[
{
"match":
{
"type": "value",
"value": "<token>",
"parameter":
{
"source": "url",
"name": "token"
}
}
},
{
"match":
{
"type": "value",
"value": "<namespaces>",
"parameter":
{
"source": "url",
"name": "ns"
}
}
},
{
"match":
{
"type": "value",
"value": "<workload>",
"parameter":
{
"source": "url",
"name": "workload"
}
}
},
{
"match":
{
"type": "value",
"value": "<container>",
"parameter":
{
"source": "url",
"name": "container"
}
}
}
]
}
}
]
3.填写添加配置映射
参数,其中:
- 名称: 可以随意填写;
- 键: 以
.json
结尾的文件名,比如springboot-demo.json
; - 值: 设置为上一步中修改的配置文件;
- 如果有多个服务,可以添加多个键值对,如图:
2 webhooks镜像安装
依次点击 system项目\工作负载\工作负载
,点击右侧部署服务
。
下面这个镜像是我自己打包的,官方的镜像包,在你使用自定义镜像仓库时会报错
registry.cn-hangzhou.aliyuncs.com/rancher2/webhook:1.0
-
配置服务名称和镜像
-
对外服务
服务默认监听端口为9000,如果使用NodePort提供服务,则安以下方式配置;
-
环境变量
如果不使用邮箱,只配置
WEBHOOK_CMD
就行WEBHOOK_CMD
=-template: 系统命令;MAIL_SMTP_PORT
=: 邮箱SMTP服务器端口;MAIL_SMTP_SERVER
=: 邮箱SMTP服务器地址;MAIL_FROM
= : 发件人邮箱;MAIL_PASSWORD
=: 发件人邮箱密码(需要base64加密: echo <密码> | base64 );MAIL_CACERT
=: 自签名CA证书,邮箱服务器采用自签名ssl证书时使用(需要base64加密: cat <ca文件> | base64 );MAIL_TLS_CHECK
=: 是否开启TLS认证(false/true,默认true);
-
配置健康检查 端口:
9000
-
配置数据卷
- 选择配置映射卷
- 配置映射名: 选择前面创建的配置映射;
- 容器路径:
/etc/webhook/source
; - 其他参数保持默认;
- 选择配置映射卷
-
最后点击启动,启动后查看日志,可以看到当前监听的服务
-
设置
serviceaccounts
这一步相对比较重要,
webhooks
服务需要serviceaccounts
才可以正常的与K8S通信。因为目前Rancher UI不支持设置serviceaccounts
,所以需要编辑yaml文件来配置serviceaccounts
。为了方便,这里复用了rancher组件使用的serviceaccounts
账号cattle
,具有集群管理员角色,您也可以根据需要定制serviceaccounts
角色。- 如图,选择
查看/编辑YAML
- 在
securityContext: {}
下边添加serviceAccount: cattle
和serviceAccountName: cattle
; - 保存
- 如图,选择
3 webhooks触发地址
http(s)://<webhooks_url>/hooks/\
<webhooks_id>?\
token=<token>&\
ns=<namespaces>&\
workload=<workload>&\
container=<container>&\
repo_type=<repo_type>
如果是阿里云的镜像仓库,可在url中添加net_type
指定网络类型:
- 公共网络:如果不指定,则默认为公共网络拉取镜像
- 专有网络:
net_type=vpc
- 经典网络:
net_type=internal
其中<webhooks_id>
、<namespaces>
、<workload>
、<container>
对应模板中的参数,<repo_type>
支持:aliyun
、dockerhub
、custom
。
4 配置仓库触发
4.1 aliyun
见官方文档
4.2 Docker
见官方文档
4.3 自定义webhooks
如果是使用Jenkins自定义构建镜像,可以设置repo_type=custom
。
在Jenkins构建项目中,在镜像push
操作后增加一个执行shell命令
的步骤。这个操作主要是在镜像成功推送到镜像仓库后发出POST消息
去触发webhooks
,这步中需要把上一步推送的镜像仓库地址
,镜像命名空间
,镜像名
,以及镜像tag作为变量传递到这一步,这样在发送POST消息
才可以把相关的镜像信息传递给webhooks,从而触发服务升级。
示例POST消息:
curl -X POST \
'http(s)://<webhooks_url>/hooks/<webhooks_id>?\
token=<token>&\
ns=<namespaces>&\
workload=<workload>&\
container=<container>&\
repo_type=custom' \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"push_data": {
"tag": "${images_tag}"
},
"repository": {
"repo_url": ${images_repo_url},
"name": "${images_name}",
"namespace": "${images_namespace}"
}
}'
5 触发webhooks
配置完以上参数,提交代码到git仓库后将会自动触发阿里云仓库或者dockerhub的自动构建,创建自动构建方法请自行查阅相关文档。
webhooks服务收到消息后,会马上触发服务的升级。查看webhooks服务的日志,可以看到已经成功升级。