經過以前的文章,相信你們已經熟悉了 Serving、Eventing 以及 Tekton。那麼在實際使用中,咱們每每會遇到一些複雜的場景,這時候就須要各個組件之間進行協做處理。例如咱們提交源代碼以後是否直接能夠部署服務到 K8s 中? 這個場景對於用戶來講頗有吸引力。那麼如今就讓咱們來看一下,在 Knative 中如何實現從代碼到服務?html
如今的場景是這樣的:代碼構建->事件驅動->服務部署。那麼對應到 Knative 中,須要 Eventing、Tekton 和 Serving 一塊兒協做來實現這個場景。git
ack-tekton-pipelines
進行安裝部署 Tekton;
{ "action": "closed", ... ... "merge_commit_sha": "f37cb28b1777a28cd34ea1f8df1b7ebcc6c16397", ... ... "base": { "ref": "master", ... ... }, ... ... }
本文涉及到的代碼與資源文件地址:github
接下來咱們開始一步步搞起。
web
咱們看一下建立代碼構建 Task 和 部署服務Task。docker
代碼構建Task:api
apiVersion: tekton.dev/v1alpha1 kind: Task metadata: name: source-to-image spec: inputs: resources: - name: git-source type: git params: - name: pathToContext description: The path to the build context, used by Kaniko - within the workspace default: . - name: pathToDockerFile description: The path to the dockerfile to build (relative to the context) default: Dockerfile - name: imageUrl description: Url of image repository - name: imageTag description: Tag to apply to the built image default: "latest" steps: - name: build-and-push image: registry.cn-hangzhou.aliyuncs.com/knative-sample/kaniko-project-executor:v0.10.0 command: - /kaniko/executor args: - --dockerfile=${inputs.params.pathToDockerFile} - --destination=${inputs.params.imageUrl}:${inputs.params.imageTag} - --context=/workspace/git-source/${inputs.params.pathToContext} env: - name: DOCKER_CONFIG value: /builder/home/.docker
這裏經過 deployer-deployer 執行服務部署,部署服務Task:數組
apiVersion: tekton.dev/v1alpha1 kind: Task metadata: name: image-to-deploy spec: inputs: resources: - name: git-source type: git params: - name: pathToYamlFile description: The path to the yaml file to deploy within the git source - name: imageUrl description: Url of image repository - name: imageTag description: Tag of the images to be used. default: "latest" steps: - name: deploy image: "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:app
apiVersion: v1 kind: Secret metadata: name: ack-cr-push-secret annotations: tekton.dev/docker-0: https://registry.cn-hangzhou.aliyuncs.com type: kubernetes.io/basic-auth stringData: username: <cleartext non-encoded> password: <cleartext non-encoded>
執行以下命令:less
# 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
先建立 deployer-github-trigger 服務,用於接收 GitHub 事件,並觸發 Tekton Pipeline 構建任務。其中 service.yaml 以下:ui
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-20190806093544 args: - --trigger-config=/app/config/deployer-trigger.yaml volumeMounts: - name: config-volume mountPath: /app/config serviceAccountName: tekton volumes: - name: config-volume configMap: name: deployer-trigger-config items: - key: deployer-trigger.yaml path: 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-config namespace: default data: "deployer-trigger.yaml": |- apiVersion: tekton.dev/v1alpha1 kind: PipelineRun metadata: name: tekton-kn-sample spec: pipelineRef: name: build-and-deploy-pipeline resources: - name: git-source resourceRef: name: eventing-tekton-serving-git params: - name: pathToContext value: "src" - name: pathToYamlFile value: "" - name: imageUrl value: "registry.cn-hangzhou.aliyuncs.com/knative-sample/eventing-tekton-serving-helloworld" - name: imageTag value: "1.0" trigger: type: manual serviceAccount: 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
代碼 merge request 會觸發對應的事件,經過 Knative Eventing 獲取到事件以後直接將事件發送給 deployer-github-trigger 服務。
建立 Personal access tokens, 用於訪問 GitHub API。另外你的代碼將使用它驗證來自 github 的傳入 webhook(secret token)。token 的名稱能夠任意設置。Source
須要開啓 repo:public_repo
和 admin: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_value secretToken: asdfasfdsaf
執行命令使其生效:
kubectl apply -f eventing/githubsecret.yaml
爲了接收 GitHub 產生的事件, 須要建立 GitHubSource 用於接收事件。
apiVersion: sources.eventing.knative.dev/v1alpha1 kind: GitHubSource metadata: name: deployer-github-sources spec: eventTypes: - pull_request ownerAndRepository: knative-sample/eventing-tekton-serving accessToken: secretKeyRef: name: githubsecret key: accessToken secretToken: secretKeyRef: name: githubsecret key: secretToken sink: apiVersion: serving.knative.dev/v1alpha1 kind: Service name: 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 給你帶來更輕鬆的代碼構建和服務部署,讓你更專一於業務自己。
本文做者:一綠舟
本文爲雲棲社區原創內容,未經容許不得轉載。