通过之前的文章,相信大家已经熟悉了 Serving、Eventing 以及 Tekton。那么在实际使用中,我们往往会遇到一些复杂的场景,这时候就需要各个组件之间进行协作处理。例如我们提交源代码之后是否直接可以部署服务到 K8s 中? 这个场景对于用户来说很有吸引力。那么现在就让我们来看一下,在 Knative 中如何实现从代码到服务?

场景介绍

现在的场景是这样的:代码构建->事件驱动->服务部署。那么对应到 Knative 中,需要 Eventing、Tekton 和 Serving 一起协作来实现这个场景。

准备

  • 部署 Knative。参考在阿里云容器服务上部署 Knative;

  • 部署 Tekton。通过阿里云容器服务控制台,应用目录选择 ack-tekton-pipelines 进行安装部署 Tekton;

    ![](https://img-blog.csdnimg.cn/2019080910002040.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2FsaXN5c3RlbXNvZnR3YXJl,size_16,color_FFFFFF,t_70)
    
  • 部署 GitHub 事件源。阿里云容器服务控制台 Knative 组件管理中选择安装 GitHub 组件,如图所示:

从源代码到服务

{"action": "closed",... ..."merge_commit_sha": "f37cb28b1777a28cd34ea1f8df1b7ebcc6c16397",... ..."base": {"ref": "master",... ...},... ...
}

本文涉及到的代码与资源文件地址:

接下来我们开始一步步搞起。

部署 Tekton 服务

我们看一下创建代码构建 Task 和 部署服务Task。

代码构建Task:

apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:name: source-to-image
spec:inputs:resources:- name: git-sourcetype: gitparams:- name: pathToContextdescription: The path to the build context, used by Kaniko - within the workspacedefault: .- name: pathToDockerFiledescription: The path to the dockerfile to build (relative to the context)default: Dockerfile- name: imageUrldescription: Url of image repository- name: imageTagdescription: Tag to apply to the built imagedefault: "latest"steps:- name: build-and-pushimage: registry.cn-hangzhou.aliyuncs.com/knative-sample/kaniko-project-executor:v0.10.0command:- /kaniko/executorargs:- --dockerfile=${inputs.params.pathToDockerFile}- --destination=${inputs.params.imageUrl}:${inputs.params.imageTag}- --context=/workspace/git-source/${inputs.params.pathToContext}env:- name: DOCKER_CONFIGvalue: /builder/home/.docker

这里通过 deployer-deployer 执行服务部署,部署服务Task:

apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:name: image-to-deploy
spec:inputs:resources:- name: git-sourcetype: gitparams:- name: pathToYamlFiledescription: The path to the yaml file to deploy within the git source- name: imageUrldescription: Url of image repository- name: imageTagdescription: Tag of the images to be used.default: "latest"steps:- name: deployimage: "registry.cn-hangzhou.aliyuncs.com/knative-sample/deployer-deployer:7620096e"args:- "--namespace=default"- "--serivce-name=hello-sample"- "--image=${inputs.params.imageUrl}:${inputs.params.imageTag}"

另外需要设置一下镜像仓库的 secret:

apiVersion: v1
kind: Secret
metadata:name: ack-cr-push-secretannotations:tekton.dev/docker-0: https://registry.cn-hangzhou.aliyuncs.com
type: kubernetes.io/basic-auth
stringData:username: <cleartext non-encoded>password: <cleartext non-encoded>

执行如下命令:

# Create Pipeline
kubectl apply -f tekton/pipeline/build-and-deploy-pipeline.yaml# Create PipelineResource
kubectl apply -f tekton/resources/picalc-git.yaml# Create image secret
kubectl apply -f tekton/image-secret.yaml# Create task: soruce to image
kubectl apply -f tekton/tasks/source-to-image.yaml# Create task: deploy the image to cluster
kubectl apply -f tekton/tasks/image-to-deployer.yaml

部署 Knative Serving 服务

先创建 deployer-github-trigger 服务,用于接收 GitHub 事件,并触发 Tekton Pipeline 构建任务。其中 service.yaml 如下:

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: deployer-github-trigger
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/deployer-trigger:tekton-v1_74647e3a-20190806093544args:- --trigger-config=/app/config/deployer-trigger.yamlvolumeMounts:- name: config-volume mountPath: /app/configserviceAccountName: tektonvolumes:- name: config-volume configMap:name: deployer-trigger-configitems:- key: deployer-trigger.yamlpath: deployer-trigger.yaml

这里通过 ConfigMap deployer-trigger-config, 设置 PipelineRun。deployer-github-trigger 能根据 github Event 信息获取代码仓库的最新信息但不能自动决定 PipelineRun 的定义,所以需要指定一个 PipelineRun 的模板。Trigger 通过 --trigger-config 参数指定 PipelineRun 的模板, 模板内容如下:

apiVersion: v1
kind: ConfigMap
metadata:name: deployer-trigger-confignamespace: default
data:"deployer-trigger.yaml": |-apiVersion: tekton.dev/v1alpha1kind: PipelineRunmetadata:name: tekton-kn-samplespec:pipelineRef:name: build-and-deploy-pipelineresources:- name: git-sourceresourceRef:name: eventing-tekton-serving-gitparams:- name: pathToContextvalue: "src"- name: pathToYamlFilevalue: ""- name: imageUrlvalue: "registry.cn-hangzhou.aliyuncs.com/knative-sample/eventing-tekton-serving-helloworld"- name: imageTagvalue: "1.0"trigger:type: manualserviceAccount: pipeline-account

执行命令如下:

# Create clusterrole
kubectl apply -f serving/clusterrole.yaml# Create clusterrolebinding
kubectl apply -f serving/clusterrolebinding.yaml# Create serviceaccount
kubectl apply -f serving/serviceaccount.yaml# Create configmap
kubectl apply -f serving/configmap.yaml# Create service
kubectl apply -f serving/service.yaml

配置 Eventing 中 GitHub 事件源

代码 merge request 会触发对应的事件,通过 Knative Eventing 获取到事件之后直接将事件发送给 deployer-github-trigger 服务。

创建 GitHub Token

创建 Personal access tokens, 用于访问 GitHub API。另外你的代码将使用它验证来自 github 的传入 webhook(secret token)。token 的名称可以任意设置。Source 需要开启 repo:public_repoadmin:repo_hook , 以便通过公共仓库触发 Event 事件,并为这些公共仓库创建 webhooks 。

下面是设置一个 “GitHubSource Sample” token 的示例。

更新 githubsecret.yaml 内容。如果生成的是 personal_access_token_value token, 则需要设置 secretToken 如下:

apiVersion: v1
kind: Secret
metadata:name: githubsecret
type: Opaque
stringData:accessToken: personal_access_token_valuesecretToken: asdfasfdsaf

执行命令使其生效:

kubectl  apply -f eventing/githubsecret.yaml

创建 GitHub 事件源

为了接收 GitHub 产生的事件, 需要创建 GitHubSource 用于接收事件。

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: GitHubSource
metadata:name: deployer-github-sources
spec:eventTypes:- pull_requestownerAndRepository: knative-sample/eventing-tekton-servingaccessToken:secretKeyRef:name: githubsecretkey: accessTokensecretToken:secretKeyRef:name: githubsecretkey: secretTokensink:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: deployer-github-trigger

关键字段解释:

执行 kubectl 命令:

kubectl  apply -f eventing/github-source.yaml

如果集群中开启了 Istio 注入,需要开启 egress 访问:

kubectl  apply -f eventing/egress.yaml

deployer-github-sources 提交到 Kubernetes 之后,github source controller 会在 http://github.com/knative-sample/eventing-tekton-serving 下创建一个 webhook,回调地址就是我们的 github_receive_adapter 服务公网地址。

当 http://github.com/knative-sample/eventing-tekton-serving 有 pull request 发生时就会自动触发 deployer-github-trigger 的执行,deployer-github-trigger 首先编译镜像,然后更新 hello-sample service 镜像,从而完成自动化发布。

代码->镜像->服务

下面我们演示一下从代码到服务,自动化构建和部署过程:


服务访问体验地址:http://hello-sample.default.serverless.kuberun.com

结论

从代码到服务,通过上面的示例,Knative 是否给你带来了不一样的体验?希望通过 Knative 给你带来更轻松的代码构建和服务部署,让你更专注于业务本身。欢迎对 Knative 有兴趣的一起交流。

欢迎加入 Knative 交流群

Knative 实践:从源代码到服务的自动化部署相关推荐

  1. Knative 实战:一个微服务应用的部署

    作者 | 元毅 阿里云智能事业群高级开发工程师 在 Istio 中提供了一个 Bookinfo 的示例,用于演示微服务之间的调用,那么如何在 Knative 中部署这个示例呢?本文将会给大家介绍一下在 ...

  2. 基于Gitlab Jenkins Docker集群 微服务搭建自动化部署平台

    随着公司应用系统的不断增多,原有手工部署流程越来越不满足上线的需求.为了各个系统能快速迭代与测试,减少上线流程时间和人为出错,迫切需要一套自动化部署系统. 转载原文:https://luoji.liv ...

  3. linux配置中心自动化,centos下搭建git服务 基于git服务配置自动化部署 遇到的Linux知识补充...

    安装 git su root yum install -y git 添加 git 用户,用于管理/使用 git 服务/仓库 创建用户 git # 添加名为git的用户, 并且会默认创建一个名为git的 ...

  4. OpenStack高级控制服务之使用编配服务(Heat)实现自动化部署云主机

    一.编配服务的基本概念  Heat是一个基于模板来编排复合云应用的服务.Heat 目前支持两种格式的模板,一种是基于 JSON 格式的 CFN 模板,另外一种是基于 YAML 格式的 HOT 模板.C ...

  5. 脚本自动化部署docker微服务,取代Jenkins

    由于Jenkins容器化部署,容器容器之间拷贝文件及其繁琐,如果在Jenkins部署在系统外层也需要配置复杂的流程才能实现微服务的自动化部署,本文主要通过脚本方式取代Jenkins实现自动化部署.脚本 ...

  6. 【maven实战】41-使用cargo-maven2-plugin实现自动化部署

    Cargo是一组帮助用户操作Web容器的工具,能够实现自动化部署,并且支持几乎所有的Web容器,如Tomcat.JBoss.Jetty和Glassfish等.Cargo通过cargo-maven2-p ...

  7. 使用netlify实现自动化部署前端项目(无服务器版本)

    介绍 本文以 github仓库进行介绍关联netlify的无服务前端自动化部署. 用途:个人网站设计.小游戏等 当然这只是让你入门~具体细节等待你自己去探索 实现 打开官方网站 如果没有注册过的账户, ...

  8. Knative 实战:基于 Knative Serverless 技术实现天气服务-下篇

    上一期我们介绍了如何基于 Knative Serverless 技术实现天气服务-上篇,首先我们先来回顾一下上篇介绍的内容: 通过高德天气 API 接口,每隔 3 个小时定时发送定时事件,将国内城市未 ...

  9. 持续集成在Hexo自动化部署上的实践

      曾经听到过这样一句话,"不要用战术上的勤奋掩盖战略上的懒惰",所以战术和战略更像是抽象类和具体类,而面向对象设计实际上是现实等级制度的一种映射.因此我们注意到,决策者通常关注的 ...

最新文章

  1. 批标准归一化(Batch Normalization)解析
  2. LIVE 预告 | CVPR 2021 预讲 · 迁移学习前沿与进展
  3. SD-WAN — 核心能力
  4. 基于PowerShell 3.0的web接口测试
  5. 为什么人生气时说话用喊的
  6. mybatis3 oracle,Mybatis3中對oracle的批量插入
  7. vue父子组件生命周期执行顺序_关于Vue组件的生命周期及执行顺序
  8. 百面机器学习!算法工程师面试宝典!
  9. ZOJ-1010 奇偶剪枝
  10. TTL转USB 六合一
  11. javascript中Promise深入理解(一)
  12. 安装Seay代码审计工具
  13. 如何鉴别硬盘是否是全新的,及HD Tune的使用方法
  14. 关于a标签 中特有的tilte属性 和 字符串文字换行
  15. 面试题:找出数列中间未排序好的子数列
  16. unsplash 图片版权
  17. 蓝牙学习笔记之建立蓝牙连接的过程
  18. 使用CloudFormation将Docker容器自动部署到AWS
  19. GPRS模块--完整的内核修改兼容GPRS模块
  20. R语言曲面拟合代码详细分析(1)

热门文章

  1. 在Centos 7 上跑 vue 项目 以及 Vue 热更新失效
  2. ResultSet 结果集
  3. bs4之标签树的下行遍历
  4. ACM入门之【分块】
  5. 实验: 集线器和交换机的区别
  6. Spring boot配置文件值注入
  7. Tomcat在自定义xml文件中配置虚拟目录
  8. MySQL带ALL关键字的子查询
  9. 「吐血」我把大学4年、研究生3年的全部学习资源都分享在这里了
  10. 只需 5 分钟看完这篇 HTTPS,去阿里面试和面试官扯皮就没问题了!