Full support for Kubernetes Deployment
Hi, From the Aspire project I would like to generate the deployment and service . yaml. If my app has a console application, then if that is not skipped then I need a Kubernetes Job deployment. I also need to have a facility for specifying of that is a cron job. I need a way to mount my application appSetting.json from Config map. If there are any secrets used in application then I want the K8s secrets also to be generated
In supporting kubernetes deployment it would be great if the component author could control the templating of kubernetes resouces and also control variations per environment.
A low hanging fruit would be to generate yaml files in a Kustomize folder structure. Kustomize deployent support is built-in into kubectl. From my point of view, helm is more appropriate for publishing generic purpose components for wide audiences (nginx, redis, postgres...) , it's overkill as a default for deploying your app's components.
I think supporting Kubernetes as a target will increase traction.
-
Kubernetes is Cloud Portable.
-
Aspire reduces or elliminates the need for sidecars which leads to leaner cluster workloads. (See requests for dapr components (including sidecar container) and you will realise that for a mid-size app with ~10 components (averaging 2 replicas each) you will allocate ~5Gb RAM (requests) with a limit of 20Gb RAM (limits) and ~2CPUs(requests) -> 6CPUs(limits) - only for Dapr to run) based on : https://docs.dapr.io/operations/hosting/kubernetes/kubernetes-production/
This would be a much saner option than targeting Dapr, at least for me, and it would be the trigger for adoption.
I would love one of you all to help spike an implementation of a deployment tool that can take this manifest. Here's an example:
https://github.com/dotnet/aspire/blob/50410e1ac8f14b5339ffbe9b2563dfff8db74ef3/samples/eShopLite/AppHost/aspire-manifest.json
The idea would be to parse this and build up a model, then spit out k8s manifest or helm manifests. It would be huge step forward in understanding what we could build here!
cc @brendandburns for thoughts as well.
Suggestion: instead of spitting out yaml, how about accessing k8s directly via the library/API?
Not exactly the same, but I talk about something similar here: https://nabeel.blog/2022/04/30/yaml-alternative (Especially the "skip yaml" part)
I'd love to collaborate on this, if time permits.
Suggestion: instead of spitting out yaml, how about accessing k8s directly via the library/API?
Sounds like a reasonable approach as well. You can just use the Kubernetes API to do the magic.
There are some steps missing though. Take a look at how azd works:
https://learn.microsoft.com/en-us/dotnet/aspire/deployment/azure/aca-deployment-azd-in-depth?tabs=windows
- You need a container registry
- You need to build containers for each service
- Push to the container registry
- Tell the apphost to generate the manifest
- Make sure the cluster can pull from the registry
- Read the manifest and call the k8s API server to deploy
Spitting out manifests or helm charts is more gitops friendly.
We'll be able to natively support steps 2-4 with aspire via msbuild. The tool needs to handle manifest -> target translation
I agree, kustomize / helm are more gitops friendly. I'm thinking flux here which is supported in AKS as a cluster extension.
If yaml is an issue, maybe json can be used for storing metadata (System.Text.Json can be used). Briefly looking through kustomize repo, I see it supports json files.
For now, I will be looking at Aspire to understand how the orchestration code feeds deployment metadata into deployment tooling (starting with the links above).
For now, I will be looking at Aspire to understand how the orchestration code feeds deployment metadata into deployment tooling (starting with the links above).
https://github.com/dotnet/aspire/blob/8203451250b032d0e7d0e98c1159d47ada9901d6/src/Aspire.Hosting.Azure/AzureResourceExtensions.cs#L21-L31
looking at that aspire-manifset there's a number of places that it's referencing a postgres connection string, where is it expected that the real connection string comes from? Is there an appsettings json file I'm missing? Or would it be expected that the k8s tool has logic for how to create a postgres connection string?
Or would it be expected that the k8s tool has logic for how to create a postgres connection string?
This is the current idea. In the AZD scenario, it queries the deployed resource after provisioning to get the connection string to inject into the containers that need it.
That said, there's another unimplemented idea for container resources that looks like this:
"postgres" : {
"type": "container.v0",
"connectionString": "Host={bindings.tcp.host};Port={bindings.tcp.port};UserName=pg;Password={GeneratePassword};",
"bindings": {
"tcp": {
"scheme": "tcp",
"protocol": "tcp",
"transport": "tcp"
}
}
}
The idea would be for the resource to define the connection string format and replace those tokens with the resolved values (whatever host and port they were deployed to or running on).
I think to make k8s work with external services we have a couple of options:
- Map those resource types to well-known containers and connection string formats
- Ask the user for the connection string and treat them as if they are going to be deployed separately.
Should this integrate with Porter/Duffle/CNAB?
@RMcD All 3? No 😄. We should scope this down to what the majority of people are using with a concrete end to end scenario in mind. I don't want to get distracted with what is possible (its possible to do anything really). Let's nail down the workflow for building an app, and deploying to k8s. First manually then using CI/CD and gitops
We use EKS in AWS, so I'd be interested in trying out there. As more Kubernetes support arrives, I'd be willing to try it out and potentially even help with AWS specific options if required.
My 2 cents here. I think generating vanilla K8s manifests based on the Aspire manifest would be the best starting point. Creating a Helm chart would be a bit overkill especially for those in the Kustomization camp and with vanilla K8s manifests, one can just as easily create the Kustomizations using a kustomize create --autodetect command if folks want to go that route.
I agree with @pauldotyu. We should start small just by generating the Yaml. I think connecting to cluster via Kubernetes API is sort of over kill. It would require to have the credentials be available to ASPIRE for the SDK to do the A&A. This might not go well with enterprise. For one of the client that I work they use OKTA and multifactor auth for even kubectl access. So we should have those enhancements only after we are able to give reasonably good output YAMLS.
I would love one of you all to help spike an implementation of a deployment tool that can take this manifest. Here's an example:
https://github.com/dotnet/aspire/blob/50410e1ac8f14b5339ffbe9b2563dfff8db74ef3/samples/eShopLite/AppHost/aspire-manifest.json
The idea would be to parse this and build up a model, then spit out k8s manifest or helm manifests. It would be huge step forward in understanding what we could build here!
cc @brendandburns for thoughts as well.
The Details provided here is not sufficient to generate the deployment yaml. We need to know the image with complete path. Second we also need to know the exposed port of the application. We need to know the type of application whether it is web or console. Based on whether it is console or Web we will have to see if we have generate job or generate an deployment
The details here should be a good starting point.
We need to know the image with complete path.
To know the complete path, the deployment tool must build know the target container registry and must build the container image using dotnet publish --os linux --arch x64 /t:PublishContainer -c Release. You can also publish to a container registry using /p:ContainerRegistry=registry (see https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container?pivots=dotnet-8-0 for more details).
This should give you the full image path.
Second we also need to know the exposed port of the application.
For projects 8080 (for http, and ignore https for now), I would start with this since .NET 8+ containers expose this port. Container resources will have a containerPort property on the binding definition.
We need to know the type of application whether it is web or console
There's no job concept so everything is a long running pod (deployment). If there are no bindings then we don't need to create a service.
We don't need to build something enterprise ready in the MVP, we're looking to build a PoC to understand the gaps in the aspire manifest generation 😄 .
Here's AZD's logic for processing the manifest (in golang 🥲 ): https://github.com/Azure/azure-dev/blob/f0e015f2e2810132d2d9b5181b99b530b38cce46/cli/azd/pkg/apphost/generate.go#L215
The details here should be a good starting point.
We need to know the image with complete path.
To know the complete path, the deployment tool must build know the target container registry and must build the container image using
dotnet publish --os linux --arch x64 /t:PublishContainer -c Release. You can also publish to a container registry using/p:ContainerRegistry=registry(see https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container?pivots=dotnet-8-0 for more details).This should give you the full image path.
Second we also need to know the exposed port of the application.
For projects 8080 (for http, and ignore https for now), I would start with this since .NET 8+ containers expose this port. Container resources will have a containerPort property on the binding definition.
We need to know the type of application whether it is web or console
There's no job concept so everything is a long running pod (deployment). If there are no bindings then we don't need to create a service.
We don't need to build something enterprise ready in the MVP, we're looking to build a PoC to understand the gaps in the aspire manifest generation 😄 .
Here's AZD's logic for processing the manifest (in golang 🥲 ): https://github.com/Azure/azure-dev/blob/main/cli/azd/pkg/apphost/generate.go#L215
For Kubernetes deployment target I suggest the following prerequisites should be specified.
- kubectl cli: "kubectl" // docker cli isn't needed since dotnet cli can publish directly into an image registry
- image registry url: "..."
- image registry username: "..."
- image registry password: "..."
- kubernetes_context: "..."
Deployment should fail fast and warn if any of these are unavailable / unreachable
Just spent a couple of hours playing around with a possible way to handle kustomize manifest generation
see prom3theu5/aspirational-manifests
What I'm doing is performing polymorphic deserialization of the aspire manifest file, and then, along with the implementation for type specific handlers for each component type, writing Kustomize simple manifests containing a deployment and service.
Handlers are mapped to resource types here
With loading of the aspire manifest here
Because the aspire manifest doesn't include port mappings directly for apps - i'm making the assumption services will be running on port 80 This could easily be expanded by passing in a custom configuration object containing resource key to port mappings, and other settings etc
Also - right now I've made no attempt to construct a db connection string, I'm simply passing all the env variables from the aspire manifest on each resource into config map generators which get mapped on the deployments
We'd likely want to expand the configuration input of the tool to include protected / sensitive values, and have those written to secrets instead.
Image name is also inferred as resource key (the name of the resource) with latest tag The idea being a simple image update can be added to the parent kustomization.yaml file to update all the image tags for instance
images:
- name: apigateway
newTag: 1.0.1
- name: catalogservice
newTag: 1.0.1
Although the path is there on the resource, so perhaps we interrogate the csproj xml directly?? I guess that is dependant on if an end user is utilising built in container build support.
We could have the templates as code, however while playing around with them was much easier to use Handlebars templating, simply passing in the data we need for transformation
Very nice starting point @prom3theu5!
Because the aspire manifest doesn't include port mappings directly for apps - i'm making the assumption services will be running on port 80
The port is 8080 for older .NET projects. We'll be doing work to put this value in the manifest so it doesn't need to be guessed or assumed by the tool.
Also - right now I've made no attempt to construct a db connection string, I'm simply passing all the env variables from the aspire manifest on each resource into config map generators which get mapped on the deployments
I think this is one of the harder things to do that isn't consistent across any of the hosting models. For container resources we plan to have define the connection string format with place holders that the tool should replace after determining what those values are.
Here's an example: https://github.com/dotnet/eShop/compare/main...davidfowl/custom-resource#diff-0480fce9a853ffae810b350c0c31097423ca45247c8fdce9d82c95407638ba45R43
We also need to make sure that service discovery works when we generate the k8s service manifest (cc @ReubenBond). eShopLite has an example of a working k8s deployment manfiest.
We'd likely want to expand the configuration input of the tool to include protected / sensitive values, and have those written to secrets instead.
Yes, this is something @mitchdenny, @ellismg and I have been discussing with the app model. How do we describe that a "secret needs to go here", so the deployment tooling could ask for or generate the secret value, store it securely and make it available to the resources that need it.
Image name is also inferred as resource key (the name of the resource) with latest tag The idea being a simple image update can be added to the parent kustomization.yaml file to update all the image tags for instance
Seems like a fine start. I think the tool can query the project for this information as part of producing the image.
Although the path is there on the resource, so perhaps we interrogate the csproj xml directly?? I guess that is dependant on if an end user is utilising built in container build support.
This would be out default. We wouldn't be parsing XML, you can resolve msbuild properties from the outside as JSON now:
https://github.com/Azure/azure-dev/blob/f00f37d70ae38a8b53d28650b0398ea4d097ec7d/cli/azd/pkg/tools/dotnet/dotnet.go#L208C42-L208C42.
Learn more here https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8#cli-based-project-evaluation
We could have the templates as code, however while playing around with them was much easier to use Handlebars templating, simply passing in the data we need for transformation
azd is using go templates 😄 (https://github.com/Azure/azure-dev/blob/f00f37d70ae38a8b53d28650b0398ea4d097ec7d/cli/azd/resources/apphost/templates/resources.bicept)
Let's see if we can get eShopLite working in a k8s environment using this prototype as a base. Thanks for kick starting this!
Thanks @davidfowl :)
The port is 8080 for older .NET projects. We'll be doing work to put this value in the manifest so it doesn't need to be guessed or assumed by the tool.
Perfect. For now i'll switch this from 80 to 8080 - should we expose 8443 as well?
I think this is one of the harder things to do that isn't consistent across any of the hosting models. For container resources we plan to have define the connection string format with place holders that the tool should replace after determining what those values are.
I agree - that would make things a lot simpler - looking at the example, from that it would be trivial to implement in the templates
We also need to make sure that service discovery works when we generate the k8s service manifest (cc @ReubenBond). eShopLite has an example of a working
Those manifests are good starting points really - next step I think we should use them as a base by turning them into common manifest types with mustache variables. We can then replace the simple deployments and services I threw together, which should get us a step closer to getting eShopLite generating and running
this would be out default. We wouldn't be parsing XML, you can resolve msbuild properties from the outside as JSON
Wow - this is pretty awesome, didn't know about this - thanks!
azd is using go templates 😄
Ahh nice - I wish there was a lib with support for them in c# - another gap in the eco system there :P I chose handlebars because the syntax is very similar - but we could use Liquid, or something else if people preferred Either way - glad that they are using templates there, easy to maintain :)
Ahh nice - I wish there was a lib with support for them in c# - another gap in the eco system there :P
I here there is this template syntax called Razor which is quite rich ;)