You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2019-12-03-asp-net-core.md
+60-60Lines changed: 60 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,12 +9,15 @@ categories:
9
9
- kubernetes
10
10
- kubecon
11
11
- events
12
+
- dotnet
12
13
author_staff_member: alex
13
14
dark_background: true
14
15
15
16
---
16
17
In this tutorial I'll show you how to build an ASP.NET Core API that you can deploy to [Kubernetes](https://kubernetes.io/) easily using [OpenFaaS](https://openfaas.com/). We'll be using steps from the official tutorial provided by the [.NET team](https://devblogs.microsoft.com/dotnet/) and explaining any custom steps taken along the way.
17
18
19
+
> Last Updated: 2024-04-11
20
+
18
21
# Why is OpenFaaS + ASP.NET Core a good combination?
19
22
20
23
ASP.NET Core provides a high-performance, lean, and portable runtime that enterprises can use to build robust APIs. The .NET team has worked hard to provide upgrade paths for companies with existing codebases and where that isn't an option, the familiar language can mean that moving code across from legacy code-bases can be done piecemeal.
@@ -33,20 +36,20 @@ The complete code example is [available on GitHub](https://github.com/alexellis/
33
36
34
37
### Setup OpenFaaS
35
38
36
-
It's assumed that you already have Kubernetes and OpenFaaS set up, but if you do not then [k3d](https://github.com/rancher/k3d), [KinD](https://kind.sigs.k8s.io), and [minikube](https://kubernetes.io/docs/tasks/tools/install-minikube/) can make good local options. I like working with remote clusters since they don't affect the battery life of my laptop, or the CPU/memory of my desktop and are always ready. A good option for a cheap remote cluster may be [DigitalOcean.com](https://m.do.co/c/2962aa9e56a1) or [Civo.com](https://civo.com/).
39
+
It's assumed that you already have Kubernetes and OpenFaaS set up, but if you do not then [k3d](https://github.com/k3d-io/k3d), [KinD](https://kind.sigs.k8s.io), and [minikube](https://kubernetes.io/docs/tasks/tools/install-minikube/) can make good local options. I like working with remote clusters since they don't affect the battery life of my laptop, or the CPU/memory of my desktop and are always ready. A good option for a cheap remote cluster may be [DigitalOcean.com](https://m.do.co/c/2962aa9e56a1) or [Civo.com](https://civo.com/).
37
40
38
41
I'll provide two resources for you to get started:
39
42
40
43
*[OpenFaaS Deployment on Kubernetes](https://docs.openfaas.com/deployment/kubernetes/) - start here if you're confident with deploying Kubernetes
41
44
*[OpenFaaS step-by-step workshop](https://github.com/openfaas/workshop) - start here if Docker and Kubernetes are brand new to you
42
45
43
-
### Install .NET Core 3.1
46
+
### Install .NET Core 8.0
44
47
45
-
Head over to the following site and download .NET Core for your OS, 2.2 will also work if that's what you're currently using:
48
+
Head over to the following site and download .NET SDK for your OS:
I recommend you download the installer and the SDK.
52
+
> This tutorial also works for previous versions if that's what you're currently using.
50
53
51
54
The .NET product automatically reports telemetry, you can [turn this off](https://docs.microsoft.com/en-gb/dotnet/core/tools/telemetry) if you wish. I added `DOTNET_CLI_TELEMETRY_OPTOUT` to my `$HOME/.bash_profile` file.
52
55
@@ -82,51 +85,54 @@ This uses the `webapi` project type (that's a REST API endpoint)
82
85
dotnet new webapi -o openfaas-api --no-https
83
86
```
84
87
85
-
This is the controller that was generated for us:
88
+
The following code shows the contents of the Program.cs that was generated for us::
You can view it with [VSCode](https://code.visualstudio.com) at `./openfaas-api/Controllers/WeatherForecastController.cs`
135
+
You can view it with [VSCode](https://code.visualstudio.com) at `./openfaas-api/Program.cs`
130
136
131
137
We need to package the service in a container for OpenFaaS to be able to serve traffic, but for now let's try it out locally.
132
138
@@ -137,7 +143,7 @@ cd openfaas-api/
137
143
dotnet run
138
144
```
139
145
140
-
Then access the URL given such as http://localhost:5000 - add `WeatherForecast` to the path to form the URL for the controller: `http://localhost:5000/WeatherForecast`
146
+
Then access the URL given such as http://localhost:5026 - add `WeatherForecast` to the path to form the URL for the controller: `http://localhost:5026/WeatherForecast`
141
147
142
148

143
149
@@ -154,14 +160,14 @@ docker --version
154
160
We'll simply use the example [from the tutorial](https://dotnet.microsoft.com/learn/aspnet/microservice-tutorial/docker-file), but edit it for the name we picked:
155
161
156
162
```Dockerfile
157
-
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
@@ -254,21 +259,22 @@ You should see the image built successfully, but we need to make a couple of add
254
259
The OpenFaaS API expects Docker images to conform to a [runtime workload contract](https://docs.openfaas.com/reference/workloads/), we can either implement that in our code by changing the HTTP port and adding a health-check, or by using the [OpenFaaS watchdog component](https://docs.openfaas.com/architecture/watchdog/).
255
260
256
261
```Dockerfile
257
-
FROM openfaas/of-watchdog:0.7.2 as watchdog
262
+
FROM ghcr.io/openfaas/of-watchdog:0.9.15 as watchdog
258
263
259
-
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
@@ -280,12 +286,10 @@ The changes add the `fwatchdog` process as the entrypoint to the container. It r
280
286
281
287
> See also: [OpenFaaS workloads](https://docs.openfaas.com/reference/workloads/)
282
288
283
-
You can run the Docker image locally as a test before deploying it to OpenFaaS.
289
+
You can run the function image locally as a test before deploying it to OpenFaaS.
284
290
285
291
```sh
286
-
faas-cli build
287
-
288
-
docker run --rm -p 8080:8080 -ti alexellis2/api
292
+
faas-cli local-run
289
293
```
290
294
291
295
Then access the site as before: `http://localhost:8080/WeatherForecast`
@@ -420,15 +424,11 @@ Official Template: false
420
424
421
425
* Auto-scaling
422
426
423
-
OpenFaaS has built-in auto-scaling rules based upon requests per second, and support for [Kubernetes HPAv2 also](https://docs.openfaas.com/tutorials/kubernetes-hpa/).
Try [Lab 9 of the OpenFaaS workshop](https://github.com/openfaas/workshop#lab-9---advanced-feature---auto-scaling), where you can learn how to test auto-scaling for your new ASP.NET Core application.
427
+
Functions can be autoscaled horizontally or scaled to zero with the [OpenFaaS Standard autoscaler](https://docs.openfaas.com/architecture/autoscaling/)
428
428
429
429
* Secrets and API keys
430
430
431
-
You can learn how to securely manage APIs and secrets using the [lessons in Lab 10](https://github.com/openfaas/workshop#lab-10---advanced-feature---secrets)
431
+
You can learn how to securely manage APIs and secrets using the following blog post: [Configure your OpenFaaS functions for staging and production](https://www.openfaas.com/blog/custom-environments/)
432
432
433
433
* Versioning of .NET runtimes
434
434
@@ -440,11 +440,11 @@ That's fine, you can create different templates or you can just specify the runt
440
440
441
441
You can apply 12-factor configuration through the `environment` section of your OpenFaaS stack.yml file.
442
442
443
-
See also: [Lab 4: Inject configuration through environmental variables](https://github.com/openfaas/workshop/blob/master/lab4.md)
443
+
See also: [Configure your OpenFaaS functions for staging and production](https://www.openfaas.com/blog/custom-environments/)
444
444
445
445
## Wrapping up
446
446
447
-
In a short period of time we were able to deploy an ASP.NET Core application using .NET 3.1 or 2.x to Kubernetes, have it scale out and build into an immutable Docker image. OpenFaaS made this task much simpler than it would have been if we'd tried to program directly against Kubernetes.
447
+
In a short period of time we were able to deploy an ASP.NET Core application using .NET 8.0 or 9.0 to Kubernetes, have it scale out and build into an immutable Docker image. OpenFaaS made this task much simpler than it would have been if we'd tried to program directly against Kubernetes.
448
448
449
449
If you aren't quite convinced yet, then watch my KubeCon talk on the PLONK Stack that combines [OpenFaaS](https://openfaas.com/) with Kubernetes and several other CNCF projects like [Prometheus](https://prometheus.io) and [NATS](https://nats.io/) to create a platform for application developers.
0 commit comments