3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

.NET 6 と Daprを使った分散サービス開発 その12 コンテナイメージの作成 実行 Push

Last updated at Posted at 2022-03-24

コンテナイメージの作成 実行 Push

前回は、Secret管理について触れてきました。今までの、トピックでいわゆる ローカル環境 でのデバッグや開発は一通り行いやすくなったと思います。
今回は、いよいよ コンテナイメージの作成からKubernetes にデプロイする下準備をしていきます、といっても今まで プロジェクト単位でコンテナをデプロイする事もDockerfileすら作っていませんが、楽にできます。


ローカル環境でコンテナの作成

今までは、特にプロジェクトでコンテナを作ったりという事を、行わないまま作業を進めてきました。
tyeを使ってローカルでのコンテナイメージの作成と実行を行います。

新規にプロジェクトとソリューションを作ります

今までの検証で、あまりに検証プロジェクトが増えたので、新しく作ることにします。今までのおさらいと思って進めてください。
以下コマンドでプロジェクトとソリューションを作成します。いずれもプロジェクトルートとなるフォルダでの作業です。

$ dotnet new webapi -n ServiceA
テンプレート "ASP.NET Core Web API" が正常に作成されました。
$ dotnet new webapi -n ServiceB
テンプレート "ASP.NET Core Web API" が正常に作成されました。
$ dotnet new sln
テンプレート "ソリューション ファイル" が正常に作成されました。
$ dotnet sln add ServiceA ServiceB
プロジェクト `ServiceA\ServiceA.csproj` をソリューションに追加しました。
プロジェクト `ServiceB\ServiceB.csproj` をソリューションに追加しました。

プロジェクト修正

ServiceAプロジェクトのControllersに、以下のファイルを追加します。

JobController.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace ServiceA.Controllers;

[ApiController]
[Route("[controller]")]
public class JobController : ControllerBase
{
    private readonly ILogger<JobController> _logger;

    public JobController(ILogger<JobController> logger)
    {
        _logger = logger;
    }

    [HttpPost(Name = "PostJob")]
    public JobResponse Post()
    {
        JobResponse response = new JobResponse
        {
            Date = DateTime.Now
        };

        Console.WriteLine(response.ToString());
        return response;
    }
}

ServiceAプロジェクトルートには、レスポンス用のクラスを追加します。

JobResponse.cs
namespace ServiceA;

public class JobResponse
{
    public DateTime Date { get; set; }

}

ServiceAとServiceB共通

app.UseHttpsRedirection();をコメントアウトしておきます。

Program.cs
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

//app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

tye 初期化とコンポーネントフォルダの作成

以下コマンドでtye.yamlを作成しましょう。

$ tye init
$ mkdir components

tye.yaml編集

作っておいたプロジェクトは、あらかじめ自動で追加されています。これを修正します。

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: daprqiitaaks
services:
- name: servicea
  project: ServiceA/ServiceA.csproj
- name: serviceb
  project: ServiceB/ServiceB.csproj

以下のように、daprに関するextensionsの項目を追加します。

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: daprqiitaaks
extensions:
- name: dapr
  log-level: debug
  components-path: "./components/"
services:
- name: servicea
  project: ServiceA/ServiceA.csproj
- name: serviceb
  project: ServiceB/ServiceB.csproj

コンポーネントの追加

コンポーネントの動作も確認できるように、今回はスケジューラーを追加します。componentフォルダにcron-binding.yamlを設定します。

cron-binding.yaml
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: Job
spec:
  type: bindings.cron
  version: v1
  metadata:
  - name: schedule
    value: "@every 10s"
scopes:
- servicea

tyeからDocker Imageをビルド

今までは、tye runやtye run --watchで起動してきました、その際にプロジェクトは自動でビルドされていたと思います。今回は tye build でビルドしてみます。

$ tye build
Loading Application Details...
Processing Service 'servicea'...
    Applying container defaults...
    Compiling Services...
    Publishing Project...
    Building Docker Image...
            #1 [internal] load build definition from Dockerfile
            # ... ... ...中略 ... ... ...
            #8 DONE 0.1s

            Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
        Created Docker Image: 'servicea:1.0.0'
Processing Service 'serviceb'...
    Applying container defaults...
    Compiling Services...
    Publishing Project...
    Building Docker Image...
            #1 [internal] load build definition from Dockerfile
            # ... ... ...中略 ... ... ...
            #8 DONE 0.1s

            Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
        Created Docker Image: 'serviceb:1.0.0'

Dockerfileも作っていませんが、これでDocker Imageがビルドできています。また、これは.NET標準で提供されているmcr.microsoft.com/dotnet/aspnet:6.0をベースイメージとして、自動でビルドしてくれています。

作成されたDocker Imageの確認

以下の通り、serviceaとservicebのイメージが作成されています。

$ docker images
REPOSITORY          TAG            IMAGE ID       CREATED         SIZE
serviceb            1.0.0          dc943a517d2e   4 minutes ago   212MB
servicea            1.0.0          135a4f48497e   4 minutes ago   212MB
... ... ...中略 ... ... ...

作成されたDocker Imageの起動

今までは、デバッグのしやすさなどを考慮して、コンテナで起動していませんでした。コンテナからローカル起動してみます。

$ tye run --docker

Tye Dashboard (http://localhost:8000) から各プロセスを確認してみましょう。TypeがContainerとなり、Sourceがmcr.microsoft.com/dotnet/aspnet:6.0となっているのがわかります。

image.png

この時点で、スケジュールコンポーネントによるトリガ動作も確認しておきましょう。

image.png

レジストリへイメージを登録

DockerHub や Azure Container Registry など、パブリック、プライベートなどの目的別に、様々なコンテナレジストリがあると思います。今回は Azure Container Registry を使いますが、認証ができて、ローカルからレジストリの操作ができれば、どこのコンテナレジストリでもOKです。

image.png

認証

Azure Container Registry の場合、事前に認証を行っておく必要があります。
今回は、対話型でログインしますが、Azure DevOps や GitHub Actionsで、CI/CDパイプラインを構成する場合は AD Service Principal や Managed IDを使います。これらの操作は各クラウドやレジストリによって異なりますので、それぞれ確認してください。

Azure Container Registry の場合

----
# Azure CLIでログインしてない人は、以下も実行しましょう
$ az login # Azureへのログイン
$ az account set --subscription mysubscription #複数サブスクリプションの人はサブスクリプションの指定
----
$ az acr login -n レジストリ名
Login Succeeded

レジストリにPush

Pushもtyeによって、一括でビルド、イメージビルド、Pushを行うことができます。tye push でコンテナイメージを作成後Pushします。

(イメージのPush)
$ tye push --interactive
Loading Application Details...
Enter the Container Registry (ex: 'example.azurecr.io' for Azure or 'example' for dockerhub): レジストリ名.azurecr.io
Processing Service 'servicea'...
    Applying container defaults...
    Compiling Services...
    Publishing Project...
    Building Docker Image...
            #1 [internal] load build definition from Dockerfile
            # ... ... ...中略 ... ... ...
            #8 DONE 0.0s

            Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
        Created Docker Image: 'レジストリ名.azurecr.io/servicea:1.0.0'
    Pushing Docker Image...
        Pushed docker image: 'レジストリ名.azurecr.io/servicea:1.0.0'
Processing Service 'serviceb'...
    Applying container defaults...
    Compiling Services...
    Publishing Project...
    Building Docker Image...
            #1 [internal] load build definition from Dockerfile
            # ... ... ...中略 ... ... ...
            #8 DONE 0.0s

            Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
        Created Docker Image: 'レジストリ名.azurecr.io/serviceb:1.0.0'
    Pushing Docker Image...
        Pushed docker image: 'レジストリ名.azurecr.io/serviceb:1.0.0'

これでレジストリに登録されました。

image.png

tye.yamlにレジストリを追加

毎回、interactiveモードで行うのも面倒なので、tye.yamlにあらかじめレジストリを追加しておく事ができます。
これでtye pushだけでpushする事ができます。ビルドパイプラインに組み込んでも、とても便利です。

image.png

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: daprqiitaaks
registry: レジストリ名.azurecr.io
extensions:
- name: dapr
  log-level: debug
  components-path: "./components/"
services:
- name: servicea
  project: ServiceA/ServiceA.csproj
- name: serviceb
  project: ServiceB/ServiceB.csproj
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?