Skip to content

Commit

Permalink
Include the sample repo "radix-sample-keda" (#435)
Browse files Browse the repository at this point in the history
* Include the sample repo "radix-sample-keda"

* remove unused dependencies, replace used dependencies with CDN

* update KEDA readme

* Add link to Radix Docs for WI

* Cleanup[

* Move keda sample som foldername matches the existing project

* Update keda sample location, add examples readme

* Update public-site/docs/start/examples/index.md

Co-authored-by: Sergey Smolnikov <[email protected]>

* Update public-site/docs/start/examples/index.md

Co-authored-by: Sergey Smolnikov <[email protected]>

---------

Co-authored-by: Richard87 <[email protected]>
Co-authored-by: Richard Hagen <[email protected]>
Co-authored-by: Sergey Smolnikov <[email protected]>
  • Loading branch information
4 people authored Jan 10, 2025
1 parent fcd30c2 commit 19925c5
Show file tree
Hide file tree
Showing 41 changed files with 1,171 additions and 10 deletions.
123 changes: 123 additions & 0 deletions examples/radix-example-keda-servicebus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# .NET Core worker processing Azure Service Bus Queue scaled by KEDA
A simple Docker container written in .NET that will receive messages from a Service Bus queue and scale via KEDA.

The message processor will receive a single message at a time (per instance), and sleep for 2 second to simulate performing work. When adding a massive amount of queue messages, KEDA will drive the container to scale out according to the event source (Service Bus Queue).

![Scenario](images/scenario.png)

This sample is a refactored version of the one found at https://github.com/kedacore/sample-dotnet-worker-servicebus-queue.
We Have removed all authentication options, and added AzureDefaultCredentials.
The sample is also upgraded from Dotnet core 3.1 to Dotnet 8.

## Configuring resources

You need the following resources in Azure to run this example (these can be created in Azure Portal, or installed automatically with Terraform):

- Azure ServiceBus Queue
- Managed Identity
- Assigned Roles:
- `Azure Service Bus Data Owner` role for your ServiceBus
- Federated credentials:
- Processor subject: `"system:serviceaccount:<YOUR-APP-NAME>-<YOUR-ENV-NAME>:processor-sa"`
- Web subject: `"system:serviceaccount:<YOUR-APP-NAME>-<YOUR-ENV-NAME>:web-sa"`
- Keda subject: `"system:serviceaccount:keda:keda-operator"`

All Federated Credentials **Issuers** is the same, and can be found in the About page in the cluster https://console.radix.equinor.com/about (or in https://console.playground.radix.equinor.com/about for playground). It will look something like this: `https://northeurope.oic.prod-aks.azure.com/00000000-0000-0000-0000-000000000000/00000000-0000-0000-0000-000000000000/`.

See [Radix Documentation](https://radix.equinor.com/guides/workload-identity/#configure-workload-identity-in-radix) for more details about working with Managed Identities and Workload Identity in Radx.

### Configure resources with Terraform
Configure `terraform.tf` and modify `resource_group_name`, `location`, `name`, and `radix_app_name` (minium).

```shell
terraform init
# ...
terraform apply
# ...
# Outputs:

# client_id = "c2f17b62-7c2f-4541-acbc-22d7cfc66e0b"
# endpoint = "https://<YOUR NAME>.servicebus.windows.net:443/"
# queue_name = "orders"
```

### App Settings

Configure `appsettings.json` in `./Radix.Samples.DotNet.Web`, `./Radix.Samples.DotNet.Processor` and `./Radix.Samples.DotNet.Generator`.

## Development
Start web server:
```shell
cd src/Radix.Samples.DotNet.Web; dotnet run
```

Start the processor:
```shell
cd src/Radix.Samples.Dotnet.OrderProcessor; dotnet run
```

Generate some events:
```shell
cd src/Radix.Samples.Dotnet.OrderGenerator; dotnet run
# Let's queue some orders, how many do you want?
# 2
# Queuing order 719a7b19-f1f7-4f46-a543-8da9bfaf843d - A Hat for Reilly Davis
# Queuing order 5c3a954c-c356-4cc9-b1d8-e31cd2c04a5a - A Salad for Savanna Rowe
# That's it, see you later!
```

## Deployment

Update Radixconfig with your ClientID and endpoint details and enjoy 🎉


### Radix Config Trigger details:
```yaml
metadata:
name: <Your Radix App Name> # Change this!
components:

- name: web
horizontalScaling:
# defaults to using CPU scaling with 80% target utilization, and min 1 replica
maxReplicas: 3

- name: processor
horizontalScaling:
maxReplicas: 10
minReplicas: 0
triggers:
- name: azuresb
azureServiceBus:
namespace: <AzureServiceBusNamespace> #.servicebus.windows.net
queueName: orders
messageCount: 2 # How many messages should each replica handle?

# Workload Identity for KEDA to access service bus
authentication:
identity:
azure:
clientId: 00000000-0000-0000-0000-000000000000 # Replace with Client ID of your managed identity
```
### Terraform Keda details:
To configure Keda, create a managed identity, and assign it a federated credential, like this if you are using Terraform:
```terraform
resource "azurerm_federated_identity_credential" "keda" {
audience = ["api://AzureADTokenExchange"]
issuer = local.radix_oidc_issuer_url # https://console.radix.equinor.com/about
name = "keda"
resource_group_name = azurerm_servicebus_namespace.main.resource_group_name
subject = "system:serviceaccount:keda:keda-operator" # RADIX Keda operator
parent_id = azurerm_user_assigned_identity.main.id # Your managed identity that have access to the ServiceBus
}
```

> ⚠️ Giving Keda access to your servicebus to count messages eables any other Radix Application to scale their apps based on messages in your queues.
>
> Note: This does not give their app access to your ServiceBus, only the trigger to read the count of messages.
## Cleanup
To cleanup your resources run `terraform destroy` and delete your app in Radix
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions examples/radix-example-keda-servicebus/radixconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/equinor/radix-operator/release/json-schema/radixapplication.json
apiVersion: radix.equinor.com/v1
kind: RadixApplication
metadata:
name: <YOUR APP NAME> #FIXME
spec:
environments:
- name: dev
build:
from: main
build:
useBuildKit: true
components:
- name: web
src: src
# runtime:
# architecture: arm64
dockerfileName: Dockerfile.Web

# Workload Identity for the running application:
identity:
azure:
clientId: 00000000-0000-0000-0000-000000000000 # FIXME

horizontalScaling:
# defaults to using CPU scaling with 80% target utilization, and min 1 replica
maxReplicas: 3

publicPort: http
ports:
- name: http
port: 8080

variables:
ASPNETCORE_ENVIRONMENT: Development
OrderQueueOptions__FullyQualifiedNamespace: your-service-bus-namespace.servicebus.windows.net # FIXME
OrderQueueOptions__QueueName: orders

- name: processor
src: src
dockerfileName: Dockerfile.Processor
# runtime:
# architecture: arm64

# Workload Identity for the running application:
identity:
azure:
clientId: 00000000-0000-0000-0000-000000000000 # FIXME

variables:
ASPNETCORE_ENVIRONMENT: Development
OrderQueueOptions__FullyQualifiedNamespace: your-service-bus-namespace.servicebus.windows.net # FIXME
OrderQueueOptions__QueueName: orders


horizontalScaling:
maxReplicas: 10
minReplicas: 0
triggers:
- name: azuresb
azureServiceBus:
namespace: your-service-bus-namespace #.servicebus.windows.net # FIXME
queueName: orders
messageCount: 2

# Workload Identity for KEDA to access service bus
authentication:
identity:
azure:
clientId: 00000000-0000-0000-0000-000000000000 # FIXME
20 changes: 20 additions & 0 deletions examples/radix-example-keda-servicebus/src/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.vs
**/.vscode
**/*.*proj.user
**/azds.yaml
**/charts
**/bin
**/obj
**/Dockerfile
**/Dockerfile.develop
**/docker-compose.yml
**/docker-compose.*.yml
**/*.dbmdl
**/*.jfm
**/secrets.dev.yaml
**/values.dev.yaml
**/.toolstarget
17 changes: 17 additions & 0 deletions examples/radix-example-keda-servicebus/src/Dockerfile.Processor
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
WORKDIR /src
COPY ["Radix.Samples.Dotnet.OrderProcessor/Radix.Samples.Dotnet.OrderProcessor.csproj", "Radix.Samples.Dotnet.OrderProcessor/"]
COPY ["Radix.Samples.Dotnet.Contracts/Radix.Samples.Dotnet.Contracts.csproj", "Radix.Samples.Dotnet.Contracts/"]
RUN dotnet restore "Radix.Samples.Dotnet.OrderProcessor/Radix.Samples.Dotnet.OrderProcessor.csproj"
COPY . .
WORKDIR "/src/Radix.Samples.Dotnet.OrderProcessor"
RUN dotnet build "Radix.Samples.Dotnet.OrderProcessor.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "Radix.Samples.Dotnet.OrderProcessor.csproj" -c Release -o /app

FROM mcr.microsoft.com/dotnet/runtime:8.0-alpine
WORKDIR /app
COPY --from=publish /app .
USER 1000
ENTRYPOINT ["dotnet", "Radix.Samples.Dotnet.OrderProcessor.dll"]
20 changes: 20 additions & 0 deletions examples/radix-example-keda-servicebus/src/Dockerfile.Web
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
WORKDIR /src
COPY ["Radix.Samples.DotNet.Web/Radix.Samples.DotNet.Web.csproj", "Radix.Samples.DotNet.Web/"]
COPY ["Radix.Samples.Dotnet.Contracts/Radix.Samples.Dotnet.Contracts.csproj", "Radix.Samples.Dotnet.Contracts/"]
RUN dotnet restore "Radix.Samples.DotNet.Web/Radix.Samples.DotNet.Web.csproj"
COPY . .
WORKDIR "/src/Radix.Samples.DotNet.Web"
RUN dotnet build "Radix.Samples.DotNet.Web.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Radix.Samples.DotNet.Web.csproj" -c Release -o /app/publish


FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine
WORKDIR /app
COPY --from=publish /app/publish .

EXPOSE 8080
USER 1000
ENTRYPOINT ["dotnet", "Radix.Samples.DotNet.Web.dll"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
<PropertyGroup Label="Globals">
<ProjectVersion>2.1</ProjectVersion>
<DockerTargetOS>Linux</DockerTargetOS>
<ProjectGuid>b74f58d6-eb10-4116-8845-7455b83145e3</ProjectGuid>
<DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
<DockerServiceUrl>{Scheme}://localhost:{ServicePort}/swagger</DockerServiceUrl>
<DockerServiceName>keda.samples.dotnet.api</DockerServiceName>
</PropertyGroup>
<ItemGroup>
<None Include="docker-compose.override.yml">
<DependentUpon>docker-compose.yml</DependentUpon>
</None>
<None Include="docker-compose.yml" />
<None Include=".dockerignore" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@page
@{
ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

<h3>Development Mode</h3>
<p>
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@page
<div class="row">
<div class="col-12">
<hr />
</div>
</div>
<div class="row">
<div id='container' class="col-12">
<svg />
</div>

</div>

<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="~/js/chat.js"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@page
@{
ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>

<p>Use this page to detail your site's privacy policy.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>KEDA Samples - Order Portal</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-page="/Index">KEDA Samples - Order Portal</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>

<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+" crossorigin="anonymous"></script>
<script src="~/js/site.js" asp-append-version="true"></script>

@RenderSection("Scripts", required: false)
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@using Radix.Samples.DotNet.Web
@namespace Radix.Samples.DotNet.Web.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = "_Layout";
}
Loading

0 comments on commit 19925c5

Please sign in to comment.