如何通过Rancher的webhook微服务来实现CI/CD的联动

这篇文章主要讲解了“如何通过Rancher的webhook微服务来实现CI/CD的联动”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何通过Rancher的webhook微服务来实现CI/CD的联动”吧!

webhook介绍

Rancher webhook的服务流程大致如下:

  1. router根据用户提交过来的method和url初始化对应的handler。

  2. handler解析请求参数里面的key和projectid初始化对应的webhook driver。

  3. driver调用升级接口,返回并相应触发webhook的请求。

如何通过Rancher的webhook微服务来实现CI/CD的联动

环境准备

Platform

Mac,Windows,Linux,Docker Cloud,AWS,Azure均可部署。

如何通过Rancher的webhook微服务来实现CI/CD的联动

本次准备的平台是Ubuntu发行版(14.04),为了兼容docker,选择linux发行版的时候内核需控制在3.10以上。

Docker

  • 根据用户选择的平台安装docker引擎,安装指导可参考https://docs.docker.com官方文档,搭配Rancher使用,docker引擎版本最优选择1.12.6或者1.13.1。

  • 本次准备的docker引擎版本是1.12.6。

Rancher

如何通过Rancher的webhook微服务来实现CI/CD的联动

CI/CD

Build应用镜像

示例应用基于NGX官方镜像build,修改了NGX welcome页面信息

如何通过Rancher的webhook微服务来实现CI/CD的联动

Push应用镜像

推送NGX应用镜像到指定的远程镜像仓库

如何通过Rancher的webhook微服务来实现CI/CD的联动

创建Stack&Service

通过API创建webapp stack,NGX service,命令行如下

curl -u "xxx:xxx" \
-X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"description": "validate the upgrade service using webhook",
"name": "webapp",
"system": false,
"dockerCompose": "version: '2'\nservices:\n  NGX:\n    image: anzersy/nginx:20170801\n    stdin_open: true\n    tty: true\n    cpuset: \"0\"\n    ports:\n    - 8787:80/tcp\n    cpu_shares: 1024\n    labels:\n      io.rancher.container.pull_image: always\n      servicename: nginx",
"rancherCompose": "version: '2'\nservices:\n  NGX:\n    scale: 1\n    start_on_create: true",
"binding": null,
"startOnCreate": true
}' 'http://a.b.c.d:e/v2-beta/projects/1a107/stacks'

验证服务

打开浏览器,访问NGX服务,确认应用的内容。

如何通过Rancher的webhook微服务来实现CI/CD的联动

创建webhook

进入webhook创建页面,通过UI为Dev,Beta,Prod 环境创建service upgrade webhook。

(注意设置好对应的镜像TAG和服务标签)

如何通过Rancher的webhook微服务来实现CI/CD的联动

更新并push应用镜像

更新NGX应用、构建镜像,并推送到远程仓库。

如何通过Rancher的webhook微服务来实现CI/CD的联动

触发upgrade webhook

触发upgrade webhoook,实现服务自动升级。

curl -u "xxx:xxx" \
-X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
    "push_data": {
        "tag": "20170801"
    },
    "repository": {
        "repo_name": "anzersy/nginx"
    }
}' 'http://a.b.c.d:e/v1-

验证更新

打开浏览器,访问NGX服务,验证服务升级内容是否正常。

如何通过Rancher的webhook微服务来实现CI/CD的联动如何通过Rancher的webhook微服务来实现CI/CD的联动

感谢各位的阅读,以上就是“如何通过Rancher的webhook微服务来实现CI/CD的联动”的内容了,经过本文的学习后,相信大家对如何通过Rancher的webhook微服务来实现CI/CD的联动这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!