Deploying a .NET Core Web API to Amazon EKS

Neil HaddleyAugust 16, 2021

Deploying a .NET Core Web API to Amazon Elastic Kubernetes Service (Amazon EKS)

Amazon Elastic Kubernetes Service (Amazon EKS) makes it possible to deploy, manage, and scale containerized applications using Kubernetes on AWS.

A .NET Core Web API can be deployed to Amazon EKS from Visual Studio Code using the Visual Studio Code terminal window.

install eksctl

> choco install -y eksctl

install eksctl on Windows

install eksctl on Windows

choco install -y eksctl

choco install -y eksctl

.NET Core Web API

> dotnet new webapi --no-https

dotnet new webapi --no-https

dotnet new webapi --no-https

Docker: Add Docker Files to Workspace

Docker: Add Docker Files to Workspace

.Net: ASP.NET Core platform

.Net: ASP.NET Core platform

Linux

Linux

Port 5000

Port 5000

Dockerfile

Dockerfile

Docker Images: Build Image...

Docker Images: Build Image...

Building

Building

Create ECR repository

Create ECR repository

Repository created

Repository created

View push commands macOS/Linux

View push commands macOS/Linux

View push commands Windows

View push commands Windows

(Get-ECRLoginCommand)...

(Get-ECRLoginCommand)...

docker build -t dotnetapi .

docker build -t dotnetapi .

docker tag...

docker tag...

docker push...

docker push...

eksctl create cluster --name dotnetapi

eksctl create cluster --name dotnetapi

Cluster EC2 nodes

Cluster EC2 nodes

Workloads

Workloads

kubectl apply -f .\dotnetapi.yaml

kubectl apply -f .\dotnetapi.yaml

dotnetapi-service created

dotnetapi-service created

Workloads

Workloads

kubectl get service/dotnetapi-service

kubectl get service/dotnetapi-service

updated containerPort, port and targetPort

updated containerPort, port and targetPort

Navigating to the loadbalancer/cluster

Navigating to the loadbalancer/cluster

WeatherForecastController.cs

TEXT
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading.Tasks;
5using Microsoft.AspNetCore.Mvc;
6using Microsoft.Extensions.Logging;
7
8namespace dotnetapi.Controllers
9{
10    [ApiController]
11    [Route("[controller]")]
12    public class WeatherForecastController : ControllerBase
13    {
14        private static readonly string[] Summaries = new[]
15        {
16            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
17        };
18
19        private readonly ILogger<WeatherForecastController> _logger;
20
21        public WeatherForecastController(ILogger<WeatherForecastController> logger)
22        {
23            _logger = logger;
24        }
25
26        [HttpGet]
27        public IEnumerable<WeatherForecast> Get()
28        {
29            var rng = new Random();
30            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
31            {
32                Date = DateTime.Now.AddDays(index),
33                TemperatureC = rng.Next(-20, 55),
34                Summary = Summaries[rng.Next(Summaries.Length)]
35            })
36            .ToArray();
37        }
38    }
39}

dotnetapi.yaml

YAML
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: dotnetapi-deployment
5  labels:
6    app: dotnetapi
7spec:
8  replicas: 2
9  selector:
10    matchLabels:
11      app: dotnetapi
12  template:
13    metadata:
14      labels:
15        app: dotnetapi
16    spec:
17      containers:
18      - name: dotnetapi
19        image: 575062151998.dkr.ecr.us-east-1.amazonaws.com/dotnetapi
20        ports:
21        - containerPort: 5000
22---
23apiVersion: v1
24kind: Service
25metadata:
26  name: dotnetapi-service
27spec:
28  selector:
29    app: dotnetapi
30  ports:
31    - protocol: TCP
32      port: 80
33      targetPort: 5000
34  type: LoadBalancer