先日、C#のマイクロサービス開発ツール "Project Tye" を試す記事を書きました。
tsubalog.hatenablog.com
先日の記事ではこのような構成のサンプルアプリケーションを作りました。
- フロントエンドアプリケーション (ASP.NET Core Razor Pages)
- バックエンドアプリケーション (ASP.NET Core Web API + Redis)
- Tyeの構成ファイル (tye.yaml)
github.com
今回は本家のブログに沿って、サンプルアプリケーションをKubernetesにデプロイしてみます。
devblogs.microsoft.com
まずKubernetesを用意します。今回は手っ取り早く、Azure Kubernetes Service (AKS)を使います。
AKSリソースの作り方はドキュメントを参照してください。
docs.microsoft.com
Kubernetesのバージョンは 1.16.10
で検証しています。
デプロイ環境を構築する
これらのツールをインストールします。私はWSL2のUbuntu 20.04で試しています。
docs.microsoft.com
kubectl
kubernetes.io
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をデプロイする
サンプルアプリケーションで使用するRedisをAKSにデプロイします。
ブログ通り、あらかじめ用意された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
はこのようになっています。(前回の記事と同じ)
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アカウントを使用しました。
hub.docker.com
また、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からアプリケーションを削除します。
$ 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を使うこともアリかなと思いました。