Simplifying ASP.NET applications on OpenShift with the ASP.NET Core S2I Builder
With recent changes the to the.NET ecosystem, developers of popular languages such as C# now have the ability to develop and deploy .NET applications across multiple platforms including OSX and Linux. This is made possible thanks to the .NET Core, a modular implementation of the .NET framework capable of supporting both web and console applications.
Aside from opening up opportunities to a new pool of potential developers, .NET Core also enabled these applications to take advantage of certain technologies they were previously restricted from. One of these technologies in particular is Linux Containers. Containerized .NET applications can now benefit from a wide range of features built into containerization technologies such as Docker. This includes a rapid application deployment cycle and portability across machines whether they are located on physical, virtual or hosted in a cloud environment.
In addition, since these applications are running in containers, they are also eligible to be run in OpenShift, Red Hat’s Platform as a Service product. OpenShift provides the flexibility to manage and run containerized applications while also providing the tools necessary for developers to be productive. It is this ecosystem that gives developers the flexibility and freedom to easily build and deploy applications, without needing to be concerned about the underlying infrastructure.
OpenShift Source to Image and .NET
To abstract developers from the underlying Docker image build process, OpenShift provides a feature called Source to Image (S2I). S2I takes an existing image in the docker format, packages an application on top of that existing image, and creates a new container image (also in the Docker format) for deployment onto OpenShift. This originating image is called an S2I builder image and contains the logic to build, deploy, and eventually run the layered application. There are two primary scripts that is used by S2I.
- assemble – Describes the steps necessary to prepare the source code for deployment. This process typically includes dependency resolution and packaging
- run – Instructions on how to run the container image to support deployed applications
These scripts are included in an S2I builder that is available for ASP.NET developers to leverage for deploying their applications into OpenShift. The source code for the builder is located on GitHub at the following location:
The S2I scripts in this builder utilize the dotnet command line interface (CLI) to prepare the application for deployment and to support the running application. Inside the S2I run script, the dotnet run command is used in conjunction with the application’s project.json file to start the application from the previously assembled components. The application itself is deployed on a kestrel based web server that listens on port 5000 from all network interfaces. This allows the application to be invoked by other applications within the OpenShift cluster or connected to a route for external ingress.
That’s it! The ASP.NET Core framework does the rest to launch the application and make it available for invocation.
Deploying a Sample Application
With an understanding of how applications are assembled and run using the ASP.NET S2I builder, let’s walk through how a sample application can utilize the builder to simplify the deployment of ASP.NET applications into an OpenShift environment. The sample application is also located on GitHub and contains both the application source code and an OpenShift template that can be used to set up the necessary project components.
Clone the repository onto your local machine to have assets available to add to OpenShift
git clone https://github.com/openshift-s2i/s2i-aspnet-example
The first step when interacting with OpenShift is to login to the environment and create a new project for the sample application. Using the OpenShift command line interface (CLI), create a project called ose-dotnet
oc new-project ose-dotnet
Next, add the template to the project so it can be used as the baseline for creating .NET applications. The template is located in a file called aspnet-s2i.json in the templates directory. Execute the following command from the root of the sample application repository to add the aspnet-s2i template to the project:
oc create –f templates/aspnet-s2i-template.json
Now, create an application from the template. The GIT_URI template parameter provides a generic variable that promotes template reuse by multiple applications. Execute the following command specifying the aspnet-s2i template and the location of the sample application’s repository to create the application in OpenShift.
oc new-app --template=aspnet-s2i –p GIT_URI=https://github.com/openshift-s2i/s2i-aspnet-example
A new S2I build will be initiated to clone the source code from the git repository into the builder which will produce the new image. Once complete, a new deployment of the image will occur. The template also included a route so that the application can be invoked from outside the OpenShift environment. The hostname of the route can be discovered by running the oc get routes command. A result similar to the following will be returned.
NAME HOST/PORT PATH SERVICE LABELS INSECURE POLICY TLS TERMINATION aspnet-app aspnet-app-ose-dotnet.rhel-cdk.10.1.2.2.xip.io aspnet-app app=aspnet-app,template=aspnet-s2i
The particular deployment of OpenShift is running in the Red Hat Container Development Kit (CDK) and provides a localized environment for working with OpenShift to accelerate the development process.
Copy the hostname into a browser to verify the application is accessible
Congratulations! You just deployed your first ASP.NET application on OpenShift. To allow additional teams to take advantage of this simplified workflow for deploying .NET applications of the own, the template from the sample application can be added to the global openshift project which will make the template accessible by all authenticated users. To do so, execute the following command from the root of the sample application repository:
oc create –f templates/aspnet-s2i-template.json -n openshift
The .NET Core framework has made it easier for ASP.NET developers to expand the reach of their applications. Through the use of the ASP.NET Core S2I builder, a simplified process is available for developers to quickly build and deploy their applications to OpenShift and to take advantage of all of the features available within the platform.
For additional information and articles on .NET Core visit our .NET Core web page for more on this topic.
For more information about .NET Core on Red Hat platforms, visit RedHatLoves.NET
Join the Red Hat Developer Program (it’s free) and get access to related cheat sheets, books, and product downloads.