歡迎光臨
每天分享高質量文章

結合使用 Draft 與 Tencent Kubernetes Engine (TKE)

Draft 是一種開源工具,有助於在 Kubernetes 群集中打包和部署應用程式容器,讓你專註於開發週期 – 專註開發的“內部迴圈”。 在開發程式碼期間,但尚未將程式碼提交到版本控制之前,Draft 將會執行。 藉助 Draft,可在程式碼發生更改時快速將應用程式重新部署到 Kubernetes。本文介紹如何對 TKE上的 Kubernetes 群集使用 Draft。

必備元件

本文中所有的步驟假設已建立 TKE群集並已與該群集建立 kubectl 連線。 如果你還沒有,請參閱 TKE快速入門https://cloud.tencent.com/product/tke/getting-started 。

Tencent Hub容器登錄檔 (tencenthub) 中需有一個專用 Docker 登錄檔。 有關建立 tencenthub實體的步驟,請參閱 TencentHub容器註冊錶快速入門https://cloud.tencent.com/document/product/857/17143 。

Helm 也必須安裝在 TKE群集中。 TKE的新版本還在內測中,需求申請內測開通 https://cloud.tencent.com/document/product/457/31699。

最後,必須安裝 Docker: https://www.docker.com/ 。

安裝 Draft

Draft CLI 是一個在開發環境上執行的客戶端,可將程式碼部署到 Kubernetes 群集中。  請參閱 Draft 安裝指南:https://github.com/Azure/draft/tree/master/docs 。本文我們使用windows環境。

從https://github.com/Azure/draft/releases 下載最新的0.16版本,我把它解壓放在目錄C:\workshop\draft,把這個目錄路徑加入到環境變數Path 即完成客戶端的安裝。

使用 draft init 命令初始化 Draft。

接下來也需要安裝draft 依賴的Helm ,Helm的安裝參考 https://helm.sh/docs/using_helm/#installing-helm,我直接從https://github.com/helm/helm/releases 下載,我也把它解壓在C:\workshop\draft。

c:\workshop\draft>helm init

Creating C:\Users\geffzhang\.helm\repository\repositories.yaml

Adding stable repo with URL: https://kubernetes-charts.storage.googleapis.com

Error: Looks like “https://kubernetes-charts.storage.googleapis.com” is not a valid chart repository or cannot be reached: Get https://kubernetes-charts.storage.googleapis.com/index.yaml: read tcp 192.168.5.238:50765->172.217.160.80:443: wsarecv: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

helm init會連線kubectl預設連線的kubernetes叢集(可以透過kubectl config view檢視),一旦連線叢集成功,tiller會被安裝到kube-system namespace中。

但是,在國內環境中,我們直接使用helm init會無法拉取到tiller映象,需要手動指定映象地址,同時如果Kubernetes叢集開啟了rbac,還需要指定執行tiller的servicaccount,併為該serviceaccount作合適的授權。

從https://github.com/Azure/draft/releases 下載最新的0.16版本,我把它解壓放在目錄C:\workshop\draft,把這個目錄路徑加入到環境變數Path 即完成客戶端的安裝。

使用 draft init 命令初始化 Draft。

接下來也需要安裝draft 依賴的Helm ,Helm的安裝參考 https://helm.sh/docs/using_helm/#installing-helm,我直接從https://github.com/helm/helm/releases 下載,我也把它解壓在C:\workshop\draft。

c:\workshop\draft>helm init

Creating C:\Users\geffzhang\.helm\repository\repositories.yaml

Adding stable repo with URL: https://kubernetes-charts.storage.googleapis.com

Error: Looks like “https://kubernetes-charts.storage.googleapis.com” is not a valid chart repository or cannot be reached: Get https://kubernetes-charts.storage.googleapis.com/index.yaml: read tcp 192.168.5.238:50765->172.217.160.80:443: wsarecv: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

helm init會連線kubectl預設連線的kubernetes叢集(可以透過kubectl config view檢視),一旦連線叢集成功,tiller會被安裝到kube-system namespace中。

但是,在國內環境中,我們直接使用helm init會無法拉取到tiller映象,需要手動指定映象地址,同時如果Kubernetes叢集開啟了rbac,還需要指定執行tiller的servicaccount,併為該serviceaccount作合適的授權。

最簡單方法是手動訪問https://kubernetes-charts.storage.googleapis.com/index.yaml, 下載儲存到C:\Users\geffzhang\.helm\repository\repositories.yaml

配置 Draft

Draft 在本地生成容器映像,然後從本地登錄檔部署這些映像(例如使用 Minikube),或使用指定的映像登錄檔。 本文使用 Tencent Hub容器登錄檔 (Tecenthub),因此,必須在 TKE群集與 TencentHub登錄檔之間建立信任關係,然後將 Draft 配置為向 TencentHub推送容器映像。

c:\workshop\Github\draft\examples\example-dotnet>docker login –username=geffzhang_weyhd_com hub.tencentyun.com

Password:

Login Succeeded

c:\workshop\Github\draft\examples\example-dotnet>draft up

Draft Up Started: ‘example-dotnet’: 01D7BCHQ4HK1P67582WWFZ92W6

example-dotnet: Building Docker Image: SUCCESS ⚓ (0.9996s)

example-dotnet: Pushing Docker Image: SUCCESS ⚓ (2.5611s)

example-dotnet: Releasing Application: SUCCESS ⚓ (3.5553s)

Inspect the logs with `draft logs 01D7BCHQ4HK1P67582WWFZ92W6`

在本地測試應用程式

若要測試應用程式,請使用 draft connect 命令。 此命令將代理與 Kubernetes pod 之間的安全連線。 完成後,可在提供的 URL 上訪問應用程式。

c:\workshop\Github\draft\examples\example-dotnet>draft connect

Connect to example-dotnet:80 on localhost:52672

[example-dotnet]: Hosting environment: Production

[example-dotnet]: Content root path: /app

[example-dotnet]: Now listening on: http://[::]:80

[example-dotnet]: Application started. Press Ctrl+C to shut down.

[example-dotnet]: [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]

[example-dotnet]: Request starting HTTP/1.1 GET http://localhost:52672/

[example-dotnet]: [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]

[example-dotnet]: Request finished in 67.1166ms 200

[example-dotnet]: [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]

[example-dotnet]: Request starting HTTP/1.1 GET http://localhost:52672/favicon.ico

[example-dotnet]: [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]

[example-dotnet]: Request finished in 0.1648ms 200

迴圈訪問應用程式

已配置 Draft 並且應用程式正在 Kubernetes 中執行,現在應開始迭代程式碼。 每當要測試更新的程式碼時,請執行 draft up 命令來更新正在執行的應用程式。

c:\workshop\Github\draft\examples\example-dotnet>draft up

Draft Up Started: ‘example-dotnet’: 01D7BCR2AHENBZHGYMPHMKQ9VD

example-dotnet: Building Docker Image: SUCCESS ⚓ (26.0217s)

example-dotnet: Pushing Docker Image: SUCCESS ⚓ (293.0051s)

example-dotnet: Releasing Application: SUCCESS ⚓ (4.1565s)

Inspect the logs with `draft logs 01D7BCR2AHENBZHGYMPHMKQ9VD`

c:\workshop\Github\draft\examples\example-dotnet>draft connect

Connect to example-dotnet:80 on localhost:52980

[example-dotnet]: Hosting environment: Production

[example-dotnet]: Content root path: /app

[example-dotnet]: Now listening on: http://[::]:80

[example-dotnet]: Application started. Press Ctrl+C to shut down.

[example-dotnet]: [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]

[example-dotnet]: Request starting HTTP/1.1 GET http://localhost:52980/

[example-dotnet]: [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]

[example-dotnet]: Request finished in 63.1118ms 200

[example-dotnet]: [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]

[example-dotnet]: Request starting HTTP/1.1 GET http://localhost:52980/favicon.ico

[example-dotnet]: [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]

[example-dotnet]: Request finished in 0.1467ms 200

贊(0)

分享創造快樂