结合使用 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/,我直接从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/,我直接从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

本站文章资源均来源自网络,除非特别声明,否则均不代表站方观点,并仅供查阅,不作为任何参考依据!
如有侵权请及时跟我们联系,本站将及时删除!
如遇版权问题,请查看 本站版权声明
THE END
分享
二维码
海报
结合使用 Draft 与 Tencent Kubernetes Engine (TKE)
Draft 是一种开源工具,有助于在 Kubernetes 群集中打包和部署应用程序容器,让你专注于开发周期 - 专注开发的“内部循环”。 在开发代码期间,但尚...
<<上一篇
下一篇>>