Skip to content

Instantly share code, notes, and snippets.

@spboyer
Last active November 9, 2019 14:55
Show Gist options
  • Save spboyer/d059a2bc5184a73b92e8bde7abac558b to your computer and use it in GitHub Desktop.
Save spboyer/d059a2bc5184a73b92e8bde7abac558b to your computer and use it in GitHub Desktop.
.NET Core Microservices, Docker, Kubernetes demo script

Simplest Microservice

File > New Project > Empty Web

  • This shows the minimal microservices that .NET Core offers, receiving a request, returning a route.
  • We have found that when talking to customers that they were building APIs having MVC was more than needed. The "V" was unused, the folder structure may have not made sense etc.
  • Talk to Endpoint Routing
  • Add Health Check - speak to the built in capabilities if Docker and K8s looking for a health endpoint and acting on fail/success

Worker Template

File > New Project > Worker

  • Talk about building Windows/Linux (systemd) services
  • Everything you already love about .NET Core is there. IoC, DI, Logging
  • Add Windows Service nuget, Add Systemd Nuget package, mention that if the app is not running as a service it simply does a NoOp.
  • Build
  • use sc.exe to install and start the Windows Service on your machine
  • Show logs in Event Viewer
  • Can take the same code and put in a container and run in Azure Container Instances or AppService (which is bootrapped by WebJobs)

Docker Support in Visual Studio

File > New Project > Web

  • Add Docker Support to project. Right click project > Add > Docker Support
  • Detects type of project, Adds Dockerfile
  • F5 app, add breakpoint, show that debug works while running inside container
  • Show Docker tools support in VS : has ENV variables, logs, file system browsing now.
  • Show publish dialog option - AppService (Windows, Linux), ACR, Docker Hub. ACR is the best option allowing for any target deployment

Kubernetes Support

Please use Visual Studio Preview 16.4 - minor bug in 16.3.x; If you are using 16.3.x please add ENV ASPNETCORE_URLS=http://+:80 to Dockerfile.develop above EXPOSE 80

File New > Container Application for Kubernetes

Follow this for creating your cluster

DO NOT SELECT "default" as the name of the space, create a new space; "default" doesn't work. Inform audience as well

  • Project Name : api
  • Solution Name : k8swebapp
  • Show output doing work to send files to AKS cluster. All pull, build, deploy work is done in the cluster. NO DOCKER needed locally
  • Add Web app to project, right click (Add Orchestration Support > Kubernetes/Helm).
  • Add the following code to Index.cshtml.cs
public WeatherForecast[] WeatherData { get; set; }
        
        public async Task OnGetAsync()
        {
            var client = new HttpClient();
            var result = await client.GetStringAsync("http://api/weatherforecast");

            WeatherData = JsonSerializer.Deserialize<WeatherForecast[]>(result);
        }
  • Create WeatherForcast.cs
using System;

public class WeatherForecast
{
    public DateTime date { get; set; }
    public int temperatureC { get; set; }
    public int temperatureF { get; set; }
    public string summary { get; set; }
}

  • Add code to Index.cshtml
 <div class="container">
        <h2>Weather Results</h2>
        <table class="table">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Temp C</th>
                    <th>Temp F</th>
                    <th>Summary</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.WeatherData)
                {
                <tr>
                    <td>@item.date.ToShortDateString()</td>
                    <td>@item.temperatureC</td>
                    <td>@item.temperatureF</td>
                    <td>@item.summary</td>
                </tr>
                }
            </tbody>
        </table>
    </div>
  • Save all, set both projects as startup, F5 to start.
  • Mention var result = await client.GetStringAsync("http://api/weatherforecast") that we are referencing the api by DNS name. And DevSpace in combination with AKS is handling the routing.
  • Can set breakpoints in both apps to show debugging in AKS / Kuberentes.

PAUSE FOR APPLAUSE...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment