结合使用 Draft 与 Tencent Kubernetes Engine (TKE)
Draft 是一種開源工具,有助于在 Kubernetes 群集中打包和部署應(yīng)用程序容器,讓你專注于開發(fā)周期 - 專注開發(fā)的“內(nèi)部循環(huán)”。 在開發(fā)代碼期間,但尚未將代碼提交到版本控制之前,Draft 將會運行。 借助 Draft,可在代碼發(fā)生更改時快速將應(yīng)用程序重新部署到 Kubernetes。本文介紹如何對 TKE上的 Kubernetes 群集使用 Draft。
必備組件
本文中所有的步驟假設(shè)已創(chuàng)建 TKE群集并已與該群集建立 kubectl 連接。 如果你還沒有,請參閱 TKE快速入門https://cloud.tencent.com/product/tke/getting-started 。
Tencent Hub容器注冊表 (tencenthub) 中需有一個專用 Docker 注冊表。 有關(guān)創(chuàng)建 tencenthub實例的步驟,請參閱 TencentHub容器注冊表快速入門https://cloud.tencent.com/document/product/857/17143 。
Helm 也必須安裝在 TKE群集中。 TKE的新版本還在內(nèi)測中,需求申請內(nèi)測開通 https://cloud.tencent.com/document/product/457/31699。
最后,必須安裝 Docker: https://www.docker.com/ 。
安裝 Draft
Draft CLI 是一個在開發(fā)環(huán)境上運行的客戶端,可將代碼部署到 Kubernetes 群集中。 ?請參閱 Draft 安裝指南:https://github.com/Azure/draft/tree/master/docs 。本文我們使用windows環(huán)境。
從https://github.com/Azure/draft/releases 下載最新的0.16版本,我把它解壓放在目錄C:\workshop\draft,把這個目錄路徑加入到環(huán)境變量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中。
但是,在國內(nèi)環(huán)境中,我們直接使用helm init會無法拉取到tiller鏡像,需要手動指定鏡像地址,同時如果Kubernetes集群開啟了rbac,還需要指定運行tiller的servicaccount,并為該serviceaccount作合適的授權(quán)。
從https://github.com/Azure/draft/releases 下載最新的0.16版本,我把它解壓放在目錄C:\workshop\draft,把這個目錄路徑加入到環(huán)境變量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中。
但是,在國內(nèi)環(huán)境中,我們直接使用helm init會無法拉取到tiller鏡像,需要手動指定鏡像地址,同時如果Kubernetes集群開啟了rbac,還需要指定運行tiller的servicaccount,并為該serviceaccount作合適的授權(quán)。
最簡單方法是手動訪問https://kubernetes-charts.storage.googleapis.com/index.yaml, 下載保存到C:\Users\geffzhang\.helm\repository\repositories.yaml
配置 Draft
Draft 在本地生成容器映像,然后從本地注冊表部署這些映像(例如使用 Minikube),或使用指定的映像注冊表。 本文使用 Tencent Hub容器注冊表 (Tecenthub),因此,必須在 TKE群集與 TencentHub注冊表之間建立信任關(guān)系,然后將 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`
在本地測試應(yīng)用程序
若要測試應(yīng)用程序,請使用 draft connect 命令。 此命令將代理與 Kubernetes pod 之間的安全連接。 完成后,可在提供的 URL 上訪問應(yīng)用程序。
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
循環(huán)訪問應(yīng)用程序
已配置 Draft 并且應(yīng)用程序正在 Kubernetes 中運行,現(xiàn)在應(yīng)開始迭代代碼。 每當(dāng)要測試更新的代碼時,請運行 draft up 命令來更新正在運行的應(yīng)用程序。
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
原文地址:https://cloud.tencent.com/developer/article/1409013
.NET社區(qū)新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com
總結(jié)
以上是生活随笔為你收集整理的结合使用 Draft 与 Tencent Kubernetes Engine (TKE)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET Core 3.0 上的g
- 下一篇: 基于IdentityServer的系统对