将Tye微服务开发工具的示例应用部署到Azure Kubernetes Service
前几天,我写了一篇试用C#微服务开发工具”Project Tye”的文章。
在前几天的文章中,我制作了一个类似的样本应用程序。
-
- フロントエンドアプリケーション (ASP.NET Core Razor Pages)
-
- バックエンドアプリケーション (ASP.NET Core Web API + Redis)
- Tyeの構成ファイル (tye.yaml)
本次我们将根据原始博客的指南,尝试将示例应用部署到Kubernetes上。
准备Azure Kubernetes Service。
首先准备Kubernetes。本次使用Azure Kubernetes Service (AKS)以便快速操作。
请参考文档以了解如何创建AKS资源。
以下是链接网址的中文原文:
https://docs.microsoft.com/ja-jp/azure/aks/kubernetes-walkthrough-portal
構築部署环境
我将安装这些工具。我在WSL2的Ubuntu 20.04上进行测试。
根据所提供的信息,以下是对“Azure CLI”的本地化中文释义:
蓝色命令行界面
kubectl -> kubectl
连接到 AKS
使用Azure CLI来获取连接到AKS的信息。
$ az aks get-credentials --resource-group rg-project-tye --name aks-project-tye
Merged "aks-project-tye" as current context in /home/yuta/.kube/config
在AKS上部署Redis。
我們將在AKS上部署用於示範應用的Redis。按照博客中的指示,我們將使用事先準備好的YAML文件。
$ kubectl apply -f https://raw.githubusercontent.com/dotnet/tye/master/docs/tutorials/hello-tye/redis.yaml
deployment.apps/redis created
service/redis created
我们应该确认已部署。
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-58897bf8c-mrh9g 1/1 Running 0 5m2s
将示例应用程序部署到AKS。
使用Tye的部署命令将示例应用程序部署到AKS。
此时的tye.yaml文件如下所示。(与上篇文章相同)
# tye application configuration file
# read all about it at https://github.com/dotnet/tye
#
# when you've given us a try, we'd love to know what you think:
# https://aka.ms/AA7q20u
#
name: microservice
services:
- name: backend
project: backend\backend.csproj
- name: frontend
project: frontend\frontend.csproj
- name: redis
image: redis
bindings:
- port: 6379
connectionString: "${host}:${port}"
- name: redis-cli
image: redis
args: "redis-cli -h redis MONITOR"
在部署时,将会创建用于每个应用程序的服务的容器镜像。
因此,需要指定容器镜像托管的容器注册表。
这次我使用了自己的Dockerhub账户。
另外,当被询问Redis连接字符串时,请输入 redis:6379。
$ tye deploy --interactive
Loading Application Details...
Verifying kubectl installation...
Verifying kubectl connection to cluster...
Enter the Container Registry (ex: 'example.azurecr.io' for Azure or 'example' for dockerhub): tsubakimoto
Processing Service 'backend'...
Applying container defaults...
Compiling Services...
Publishing Project...
Building Docker Image...
Created Docker Image: 'tsubakimoto/backend:1.0.0'
Pushing Docker Image...
Pushed docker image: 'tsubakimoto/backend:1.0.0'
Validating Secrets...
Enter the connection string to use for service 'redis': redis:6379
Created secret 'binding-production-redis-secret'.
Generating Manifests...
Processing Service 'frontend'...
Applying container defaults...
Compiling Services...
Publishing Project...
Building Docker Image...
Created Docker Image: 'tsubakimoto/frontend:1.0.0'
Pushing Docker Image...
Pushed docker image: 'tsubakimoto/frontend:1.0.0'
Validating Secrets...
Generating Manifests...
Processing Service 'redis'...
Applying container defaults...
Service 'redis' does not have a project associated. Skipping.
Compiling Services...
Publishing Project...
Service 'redis' does not have a project associated. Skipping.
Building Docker Image...
Service 'redis' does not have a project associated. Skipping.
Pushing Docker Image...
Service 'redis' does not have a project associated. Skipping.
Validating Secrets...
Generating Manifests...
Service 'redis' does not have a container. Skipping.
Processing Service 'redis-cli'...
Applying container defaults...
Service 'redis-cli' does not have a project associated. Skipping.
Compiling Services...
Publishing Project...
Service 'redis-cli' does not have a project associated. Skipping.
Building Docker Image...
Service 'redis-cli' does not have a project associated. Skipping.
Pushing Docker Image...
Service 'redis-cli' does not have a project associated. Skipping.
Validating Secrets...
Generating Manifests...
Service 'redis-cli' does not have a container. Skipping.
Deploying Application Manifests...
Verifying kubectl installation...
Verifying kubectl connection to cluster...
Writing output to '/tmp/tmph3XlUg.tmp'.
Deployed application 'microservice'.
Time Elapsed: 00:00:59:77
确保部署已经完成,并验证前端、后端和Redis的Pod已经被创建。
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
backend-5d7b894dcf-2zjkd 1/1 Running 0 5m24s
frontend-84698d8fcf-xjglj 1/1 Running 0 5m24s
redis-58897bf8c-mrh9g 1/1 Running 0 42m
确认部署的应用程序。
通过将AKS的80号端口进行端口转发,并在浏览器中访问 http://localhost:5000 来确认应用程序正在运行。
$ kubectl port-forward svc/frontend 5000:80
Forwarding from 127.0.0.1:5000 -> 80
Forwarding from [::1]:5000 -> 80
删除在AKS上部署的应用程序
由于在AKS上部署了示例应用并确认其正常运行,因此可以使用Tye的undeploy命令从AKS中删除该应用。
(Once the sample application is deployed and confirmed to be operational on AKS, it will be removed from AKS using the undeploy command of Tye for now.)
$ tye undeploy
Loading Application Details...
Found 7 resource(s).
Deleting 'Service' 'backend' ...
Deleting 'Service' 'frontend' ...
Deleting 'Service' 'redis' ...
Deleting 'Deployment' 'backend' ...
Deleting 'Deployment' 'frontend' ...
Deleting 'Deployment' 'redis' ...
Deleting 'Secret' 'binding-production-redis-secret' ...
Time Elapsed: 00:00:06:51
$ kubectl get pods
No resources found in default namespace.
总结
可以使用命令行在Kubernetes上部署样例微服务应用。
我认为,在将实际的AKS部署到CI/CD环境时,很可能会使用Tye来进行开发环境阶段的功能验证。