本篇文章為大家展示了從源代碼到服務(wù)的自動化部署Knative實踐如何理解,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供楚雄州網(wǎng)站建設(shè)、楚雄州做網(wǎng)站、楚雄州網(wǎng)站設(shè)計、楚雄州網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、楚雄州企業(yè)網(wǎng)站模板建站服務(wù),10余年楚雄州做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
通過之前的文章,相信大家已經(jīng)熟悉了 Serving、Eventing 以及 Tekton。那么在實際使用中,我們往往會遇到一些復(fù)雜的場景,這時候就需要各個組件之間進行協(xié)作處理。例如我們提交源代碼之后是否直接可以部署服務(wù)到 K8s 中? 這個場景對于用戶來說很有吸引力。那么現(xiàn)在就讓我們來看一下,在 Knative 中如何實現(xiàn)從代碼到服務(wù)?
現(xiàn)在的場景是這樣的:代碼構(gòu)建->事件驅(qū)動->服務(wù)部署。那么對應(yīng)到 Knative 中,需要 Eventing、Tekton 和 Serving 一起協(xié)作來實現(xiàn)這個場景。
部署 Knative。參考在阿里云容器服務(wù)上部署 Knative;
部署 Tekton。通過阿里云容器服務(wù)控制臺,應(yīng)用目錄選擇 ack-tekton-pipelines
進行安裝部署 Tekton;
部署 GitHub 事件源。阿里云容器服務(wù)控制臺 Knative 組件管理中選擇安裝 GitHub 組件,如圖所示:
修改分支代碼,提交 merge request 合并到 master 分支;
Eventing 監(jiān)聽到 merge 事件,發(fā)送給 GitHub Trigger 服務(wù);
GitHub Trigger 服務(wù)接收事件, 通過 Tekton 執(zhí)行代碼構(gòu)建和并通過 deployer 執(zhí)行服務(wù)部署。GitHub Trigger 的作用就是解析 GitHub 事件的詳細信息,然后轉(zhuǎn)換成 Tekton 資源并且提交到 Kubernetes 中執(zhí)行 Pipeline。項目地址:https://github.com/knative-sample/tekton-serving。 這個項目中有兩個部分: Trigger 和 Deployer,Trigger 的作用是解析 github 事件, 并提交 PipelineRun 定義。Deployer 的作用就是更新 Service 的鏡像信息。github source pull_request body 的關(guān)鍵內(nèi)容如下:
{ "action": "closed", ... ... "merge_commit_sha": "f37cb28b1777a28cd34ea1f8df1b7ebcc6c16397", ... ... "base": { "ref": "master", ... ... }, ... ... }
action 表示當前的 pull request 事件細節(jié)。創(chuàng)建 pull request 時 action 是 opened ,關(guān)閉 pull request 時 action 就是 closed;
merge_commit_sha 可以獲得 merge commit 的 id;
base.ref 可以獲得 merge request 發(fā)生在哪個分支上。
本文涉及到的代碼與資源文件地址:
GitHubTrigger 和 Deployer:https://github.com/knative-sample/tekton-serving
部署示例文件:https://github.com/knative-sample/eventing-tekton-serving
接下來我們開始一步步搞起。
我們看一下創(chuàng)建代碼構(gòu)建 Task 和 部署服務(wù)Task。
代碼構(gòu)建Task:
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 執(zhí)行服務(wù)部署,部署服務(wù)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}"
另外需要設(shè)置一下鏡像倉庫的 secret:
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>
執(zhí)行如下命令:
# 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
先創(chuàng)建 deployer-github-trigger 服務(wù),用于接收 GitHub 事件,并觸發(fā) Tekton Pipeline 構(gòu)建任務(wù)。其中 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-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
, 設(shè)置 PipelineRun。deployer-github-trigger 能根據(jù) github Event 信息獲取代碼倉庫的最新信息但不能自動決定 PipelineRun 的定義,所以需要指定一個 PipelineRun 的模板。Trigger 通過 --trigger-config 參數(shù)指定 PipelineRun 的模板, 模板內(nèi)容如下:
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
執(zhí)行命令如下:
# 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 會觸發(fā)對應(yīng)的事件,通過 Knative Eventing 獲取到事件之后直接將事件發(fā)送給 deployer-github-trigger 服務(wù)。
創(chuàng)建 Personal access tokens, 用于訪問 GitHub API。另外你的代碼將使用它驗證來自 github 的傳入 webhook(secret token)。token 的名稱可以任意設(shè)置。Source
需要開啟 repo:public_repo
和 admin:repo_hook
, 以便通過公共倉庫觸發(fā) Event 事件,并為這些公共倉庫創(chuàng)建 webhooks 。
下面是設(shè)置一個 "GitHubSource Sample" token 的示例。
更新 githubsecret.yaml
內(nèi)容。如果生成的是 personal_access_token_value
token, 則需要設(shè)置 secretToken
如下:
apiVersion: v1 kind: Secret metadata: name: githubsecret type: Opaque stringData: accessToken: personal_access_token_value secretToken: asdfasfdsaf
執(zhí)行命令使其生效:
kubectl apply -f eventing/githubsecret.yaml
為了接收 GitHub 產(chǎn)生的事件, 需要創(chuàng)建 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
關(guān)鍵字段解釋:
指定 github 倉庫:ownerAndRepository: knative-sample/eventing-tekton-serving 表示監(jiān)聽 https://github.com/knative-sample/eventing-tekton-serving 倉庫的事件;
事件類型:eventTypes 是一個數(shù)組,這個數(shù)組中可以配置 github 事件列表;
認證信息:accessToken 和 secretToken 是通過 secret 引用 github 倉庫的認證信息;
目標 Service:sink 字段表示接收到的事件需要發(fā)送到哪個 Service , 這里是直接發(fā)送到前面定義的 deployer-github-trigger 服務(wù)。
執(zhí)行 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 下創(chuàng)建一個 webhook,回調(diào)地址就是我們的 github_receive_adapter 服務(wù)公網(wǎng)地址。
當 http://github.com/knative-sample/eventing-tekton-serving 有 pull request 發(fā)生時就會自動觸發(fā) deployer-github-trigger 的執(zhí)行,deployer-github-trigger 首先編譯鏡像,然后更新 hello-sample service 鏡像,從而完成自動化發(fā)布。
下面我們演示一下從代碼到服務(wù),自動化構(gòu)建和部署過程:
服務(wù)訪問體驗地址:http://hello-sample.default.serverless.kuberun.com
上述內(nèi)容就是從源代碼到服務(wù)的自動化部署Knative實踐如何理解,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞名稱:從源代碼到服務(wù)的自動化部署Knative實踐如何理解
本文路徑:http://www.chinadenli.net/article38/gcigsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、用戶體驗、自適應(yīng)網(wǎng)站、網(wǎng)站導(dǎo)航、商城網(wǎng)站、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)