Anto Subash.

abp
tye
microk8s
MicroK8s - Getting Started -Part : 4

Create and deploy .Net application in MicroK8s

Table of contents

Introduction

In this post we will create a simple abp application and deploy it in the microk8s instance. We will use tye to deploy the application in the cluster and setup the ingress for the application the ingress will use cert-manager to get the certificate for the application. this app will connect to a postgres database to store its data.

Parts

Part 1. Setup MicroK8s With Ubuntu

Part 2. Setup Nginx and cert-manager in MicroK8s

Part 3. Deploy docker registry and postgres database in MicroK8s

Part 4. Create and deploy .Net application in MicroK8s (this post)

Repository

you can find the sample application we will use in this post here and the yaml files used in this post here

Create a new application

We will use the abp cli to create a new application. If you want to create a new application you can use the following command.

1abp new TodoApp -t app-nolayers --preview -dbms PostgreSQL

We are using the app-nolayers template and we are using the PostgreSQL database. You can use the other templates as well. You can find more information about this template here.

Build the application

We will use the tye to build the application. You can find more information about tye here

1tye init # this will create a tye.yaml file
2tye build # this will build the application

Update the application

We will update the application and make some changes. we will remove the migration check and migrate the database on startup. We will also update the tye file with registry information.

lets update the Program.cs file with the following code.

1var builder = WebApplication.CreateBuilder(args);
2builder.Host.AddAppSettingsSecretsJson()
3    .UseAutofac()
4    .UseSerilog();
5await builder.AddApplicationAsync<TodoAppModule>();
6var app = builder.Build();
7await app.InitializeApplicationAsync();
8await app.Services.GetRequiredService<TodoAppDbMigrationService>(). MigrateAsync(); // add this line
9Log.Information("Starting TodoApp.");
10await app.RunAsync();
11return 0;

Since we are going to use nginx to serve the application we need to add the forwarded headers middleware. We will add the following code to the TodoAppModule.cs file.

1context.Services.Configure<ForwardedHeadersOptions>(options =>
2        {
3            options.ForwardedHeaders =
4                ForwardedHeaders.XForwardedProto;
5        }); // add this line in ConfigureServices method
6
7app.UseForwardedHeaders(); // add this line in  OnApplicationInitialization method

lets update the tye.yaml file with the following code.

1name: todoapp
2registry: registry.kdev.antosubash.com
3services:
4- name: todoapp
5  project: TodoApp/TodoApp.csproj

lets add the version to the TodoApp.csproj file.

1<Project Sdk="Microsoft.NET.Sdk.Web">
2  <PropertyGroup>
3    <TargetFramework>net6.0</TargetFramework>
4    <Version>1.0.0</Version> <!-- add this line -->
5  </PropertyGroup>
6</Project>

lets build the application again.

1tye build

Push the application to the registry

lets push the application to the registry. we will use tye to push the application to the registry. tye will build the application and push it to the registry.

1tye push

this will push the application to the registry. make sure you have logged in to the registry before pushing the application.

Deploy the application

lets deploy the application to the microk8s instance. the first thing is to generate the yaml files. we will use the tye to generate the yaml files.

1tye generate

this will generate the yaml file. we will rename the generated yaml file to todoapp-generate.yaml. we will use the todoapp-generate.yaml file to deploy the application. before deploying the application we need to update the todoapp-generate.yaml file. we will update the image name and add the environment variables. we will also add the ingress resource. we will use the following code to update the todoapp-generate.yaml file.

1kind: Deployment
2apiVersion: apps/v1
3metadata:
4  name: todoapp
5  labels:
6    app.kubernetes.io/name: 'todoapp'
7    app.kubernetes.io/part-of: 'todoapp'
8spec:
9  replicas: 1
10  selector:
11    matchLabels:
12      app.kubernetes.io/name: todoapp
13  template:
14    metadata:
15      labels:
16        app.kubernetes.io/name: 'todoapp'
17        app.kubernetes.io/part-of: 'todoapp'
18    spec:
19      imagePullSecrets:
20      - name: regcred
21      containers:
22      - name: todoapp
23        image: registry.kdev.antosubash.com/todoapp:1.0.4
24        imagePullPolicy: Always
25        resources:
26          limits:
27            cpu: "1"
28            memory: 1Gi
29          requests:
30            cpu: "0.5"
31            memory: 1Gi
32        env:
33        - name: ASPNETCORE_URLS
34          value: 'http://*'
35        - name: PORT
36          value: '80'
37        - name: CONNECTIONSTRINGS__Default
38          value: 'Host=postgres.default.svc.cluster.local;Port=5432;Database=TodoApp;User ID=postgres;Password=my_postgres_password;'
39        - name: ASPNETCORE_ENVIRONMENT
40          value: 'Production'
41        - name: App__SelfUrl
42          value: 'https://todoapp.kdev.antosubash.com'
43        ports:
44        - containerPort: 80
45...
46---
47kind: Service
48apiVersion: v1
49metadata:
50  name: todoapp
51  labels:
52    app.kubernetes.io/name: 'todoapp'
53    app.kubernetes.io/part-of: 'todoapp'
54spec:
55  selector:
56    app.kubernetes.io/name: todoapp
57  type: ClusterIP
58  ports:
59  - name: http
60    protocol: TCP
61    port: 80
62    targetPort: 80
63...
64---
65kind: Ingress
66apiVersion: networking.k8s.io/v1
67metadata:
68  name: http-ingress-todoapp
69  annotations:
70    cert-manager.io/cluster-issuer: "lets-encrypt"
71    kubernetes.io/ingress.class: "public"
72    nginx.ingress.kubernetes.io/rewrite-target: '/$2'
73  labels:
74    app.kubernetes.io/part-of: 'todoapp'
75spec:
76  tls:
77  - hosts:
78    - todoapp.kdev.antosubash.com
79    secretName: todoapp-tls
80  rules:
81  - host: todoapp.kdev.antosubash.com
82    http:
83      paths:
84      - backend:
85          service:
86            name: todoapp
87            port:
88              number: 80
89        pathType: Prefix
90        path: /()(.*)
91...

lets deploy the application to the microk8s instance.

1kubectl apply -f todoapp-generate.yaml

Access the application

lets access the application. we will use the following url to access the application.

1https://todoapp.kdev.antosubash.com

Conclusion

This is the final post of the series. In this post we have seen how to deploy the application to the microk8s instance. we have also seen how to use the tye to deploy the application, build the application and push the application to the registry. we have also seen how to use the ingress resource to access the application. I hope you have enjoyed this series. I will see you in the next post.

Buy Me a Coffee at ko-fi.com