Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9328

マイクロサービス開発ツールのTyeのサンプルアプリケーションをAzure Kubernetes Serviceにデプロイする

$
0
0

先日、C#のマイクロサービス開発ツール "Project Tye" を試す記事を書きました。

https://tsubalog.hatenablog.com/entry/2020/08/02/234851

先日の記事ではこのような構成のサンプルアプリケーションを作りました。

  • フロントエンドアプリケーション (ASP.NET Core Razor Pages)
  • バックエンドアプリケーション (ASP.NET Core Web API + Redis)
  • Tyeの構成ファイル (tye.yaml)

https://github.com/tsubakimoto/project-tye-sample

今回は本家のブログに沿って、サンプルアプリケーションをKubernetesにデプロイしてみます。

https://devblogs.microsoft.com/aspnet/introducing-project-tye/

Azure Kubernetes Serviceを用意する

まずKubernetesを用意します。今回は手っ取り早く、Azure Kubernetes Service (AKS)を使います。

AKSリソースの作り方はドキュメントを参照してください。

https://docs.microsoft.com/ja-jp/azure/aks/kubernetes-walkthrough-portal

Kubernetesのバージョンは 1.16.10で検証しています。
20200816012926.png

デプロイ環境を構築する

これらのツールをインストールします。私はWSL2のUbuntu 20.04で試しています。

Azure CLI

https://docs.microsoft.com/ja-jp/cli/azure/install-azure-cli-apt?view=azure-cli-latest

kubectl

https://kubernetes.io/ja/docs/tasks/tools/install-kubectl/#curl%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6linux%E3%81%B8kubectl%E3%81%AE%E3%83%90%E3%82%A4%E3%83%8A%E3%83%AA%E3%82%92%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%99%E3%82%8B

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をデプロイする

サンプルアプリケーションで使用する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はこのようになっています。(前回の記事と同じ)

# 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:microserviceservices:-name:backendproject:backend\backend.csproj-name:frontendproject:frontend\frontend.csproj-name:redisimage:redisbindings:-port:6379connectionString:"${host}:${port}"-name:redis-cliimage:redisargs:"redis-cli-hredisMONITOR"

デプロイする際、サービスとなる各アプリケーションのコンテナーイメージが作成されます。

そのため、コンテナーイメージをホストするコンテナーレジストリーを指定する必要があります。

今回は自分のDockerhubアカウントを使用しました。

https://hub.docker.com/u/tsubakimoto

また、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

20200816013057.png

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を使うこともアリかなと思いました。


Viewing all articles
Browse latest Browse all 9328

Latest Images

Trending Articles