Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.NET Core Workers in Azure Container Instances

#1
.NET Core Workers in Azure Container Instances

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances.jpg" width="58" height="58" title="" alt="" /></div><div><div class="row justify-content-center">
<div class="col-md-2">
<div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances.jpg" width="58" height="58" alt="Avatar" class="avatar avatar-58 wp-user-avatar wp-user-avatar-58 photo avatar-default"></div>
</div>
</div>
<div class="entry-meta">
<p>April 15th, 2019</p>
<p> &lt;!–<span class="posted-on">Posted on <a href="https://devblogs.microsoft.com/aspnet/dotnet-core-workers-in-azure-container-instances/" rel="bookmark"><time class="entry-date published" datetime="2019-04-15T10:00:41+00:00">April 15, 2019</time><time class="updated" datetime="2019-04-15T09:50:36+00:00"> (April 15, 2019) </time></a></span><span class="byline"> by <span class="author vcard"><a class="url fn n" href="https://devblogs.microsoft.com/aspnet/author/bradygmicrosoft-com/">Brady Gaster</a></span></span>–&gt; </div>
<p><!-- .entry-meta --> </p>
<p>In .NET Core 3.0 we are introducing a new type of application template called Worker Service. This template is intended to give you a starting point for writing long running services in .NET Core. In this walkthrough you’ll learn how to use a Worker with <a href="http://aka.ms/acr/docs">Azure Container Registry</a> and <a href="https://docs.microsoft.com/en-us/azure/container-instances/">Azure Container Instances</a> to get your Worker running as a microservice in the cloud.</p>
<p>Since the Worker template <a href="https://devblogs.microsoft.com/aspnet/net-core-workers-as-windows-services/">Glenn blogged about</a> is also available via the <code>dotnet new</code> command line, I can create one on my Mac and edit the code using Visual Studio for Mac or Visual Studio Code (which I’ll be using here to take advantage of the integrated <a href="https://code.visualstudio.com/docs/azure/docker">Docker extension</a>).</p>
<pre><code class="bash">dotnet new worker
</code></pre>
<p>I’ll use the default from the Worker template. As it will write to logs during execution via <code>ILogger</code>, I’ll be able to tell quickly from looking in the logs if the Worker is running.</p>
<pre><code class="csharp">public class Worker : BackgroundService
{ private readonly ILogger&lt;Worker&gt; _logger; public Worker(ILogger&lt;Worker&gt; logger) { _logger = logger; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); await Task.Delay(1000, stoppingToken); } }
}
</code></pre>
<p>Visual Studio Code’s Docker tools are intelligent enough to figure out this is a .NET Core app, and will suggest the correct Docker file via the Command Palette’s <code>Add Docker files to workspace</code> option.</p>
<p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances.png" alt class="alignnone size-medium wp-image-21752"></p>
<p>By right-clicking the resulting <code>Dockerfile</code> I can build the Worker into a Docker image in one click.</p>
<p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances-1.png" alt class="alignnone size-medium wp-image-21753"></p>
<p>The <strong>Build Image</strong> option will package my Worker’s code into a Docker container locally. The second option, <strong>ACR Tasks: Build Image</strong> would use Azure Container Registry Tasks to build the image in the cloud, rather than on disk. This is helpful for scenarios when the base image is larger than I want to download locally or when I’m building an application on a Windows base image from Linux or Mac. You can learn more about ACR Tasks in the <a href="https://docs.microsoft.com/en-us/cli/azure/?view=azure-cli-latest">ACR docs</a>. The <a href="https://docs.microsoft.com/en-us/cli/azure/?view=azure-cli-latest">Azure CLI</a> makes it easy to login to the Azure Container Registry using the Azure CLI. This results in my Docker client being authenticated to the Azure Container Registry in my subscription.</p>
<pre><code class="bash">az acr login -n BackgroundWorkerImages
</code></pre>
<p>This can be done in the VS Code integrated terminal <em>or</em> in the local terminal, as the setting will be persisted across the terminals’ environment. It can’t be done using the cloud shell, since logging into the Azure Container Registry requires local shell access so local Docker images can be accessed. Before I push the container image into my registry, I need to tag the image with the URI of the image once it has been pushed into my registry. I can easily get the ACR instance URI from the portal.</p>
<p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances-2.png" alt class="alignnone size-medium wp-image-21751"></p>
<p>I’ll copy the URI of the registry’s login server in the portal so I can paste it when I tag the image later.</p>
<p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances-3.png" alt class="alignnone size-medium wp-image-21755"></p>
<p>By selecting the <code>backgroundworker:latest</code> image in Visual Studio Code’s Docker explorer pane, I can select <strong>Tag Image</strong>.</p>
<p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances-4.png" alt class="alignnone size-medium wp-image-21760"></p>
<p>I’ll be prompted for the tag, and I can easily paste in the URI I copied from the portal.</p>
<p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances-5.png" alt class="alignnone size-medium wp-image-21761"></p>
<p>Finally, I can right-click the image tag I created and select <strong>Push</strong>, and the image will be pushed into the registry. Once I have a Docker image in the registry, I can use the CLI or tools to deploy it to Azure Container Instances, Kubernetes, or even Azure App Service.</p>
<p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances-6.png" alt class="alignnone size-medium wp-image-21757"></p>
<p>Now that the worker is containerized and stored in the registry, starting an instance of it is one click away.</p>
<p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances-7.png" alt class="alignnone size-medium wp-image-21759"></p>
<p>Once the container instance starts up, I’ll see some logs indicating the worker is executing, but these are just the basic startup logs and not my information-level logs I have in my Worker code.</p>
<p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances-8.png" alt class="alignnone size-medium wp-image-21754"></p>
<p>Since I added Information-level logs during the worker’s execution, the configuration in <code>appsettings.json</code> (or the environment variable for the container instance) will need to be updated to see more verbose logs.</p>
<pre><code class="json">{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.Hosting.Lifetime": "Information" } }
}
</code></pre>
<p>Once the code is re-packaged into an updated Docker image and pushed into the Azure Container Registry, following a simple Restart…</p>
<p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances-9.png" alt class="alignnone size-medium wp-image-21758"></p>
<p>… more details will be visible in the container instance’s logging output.</p>
<p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances-10.png" alt class="alignnone size-medium wp-image-21762"></p>
<p>The Worker template makes it easy to create long-running background workers that you can run for as long as you need in Azure Container Instances. New container instances can be created using the portal or the Azure Command Line. Or, you can opt for more advanced scenarios using Azure DevOps or Logic Apps. With the Worker template making it easy to get started building microservices using your favorite ASP.NET Core idioms and Azure’s arsenal of container orchestration services you can get your microservices up and running in minutes.</p>
<div class="authorinfoarea">
<div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/net-core-workers-in-azure-container-instances-1.jpg" width="96" height="96" alt="Avatar" class="avatar avatar-96 wp-user-avatar wp-user-avatar-96 photo avatar-default"></div>
<div>
<h5><a class="no-underline" aria-label=" Brady Gaster" target="_blank" href="https://devblogs.microsoft.com/aspnet/author/bradygmicrosoft-com/" rel="noopener noreferrer"> Brady Gaster</a></h5>
<p>Senior Program Manager,&nbsp;ASP.NET Core</p>
<p><strong>Follow </strong>&nbsp;&nbsp;&nbsp;<a class="no-underline stayinformed" aria-label=" Brady Gaster Twitter profile" target="_blank" href="https://twitter.com/bradygaster" rel="noopener noreferrer"><i class="fa fa-twitter"></i></a><a class="no-underline stayinformed" aria-label=" Brady Gaster GitHub profile" target="_blank" href="https://github.com/bradygaster" rel="noopener noreferrer"><i class="fa fa-github"></i></a><a class="no-underline stayinformed hvr-pop" aria-label=" Brady Gaster RSS Feed" target="_blank" href="https://devblogs.microsoft.com/aspnet/author/bradygmicrosoft-com/feed/" rel="noopener noreferrer"></a></p>
<p> &lt;!–</p>
<hr class="authorarea">–&gt; </div>
</p></div>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016