Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

6 Docker .NET App Templates to Get You Started on Containers (Part 2)

DZone's Guide to

6 Docker .NET App Templates to Get You Started on Containers (Part 2)

You've got the templates, now see how you can put them to use. Learn how to get your hands dirty with Docker for your .NET apps.

· Cloud Zone
Free Resource

MongoDB Atlas is a database as a service that makes it easy to deploy, manage, and scale MongoDB. So you can focus on innovation, not operations. Brought to you in partnership with MongoDB.

In the last post, we got set up with our various templates. Now, it's time to get them working how you want them to.

Environment Variable Bindings Across Images

A user can create cross-image environment variable bindings by making a reference to another image’s environment variable or container values. For example, the plug-used in the Apache HTTP container is referencing the container private IP of the ASP.NET container BalancerMembers=BalancerMember http://{{aspnet-mvc | container_private_ip}}:5004 – in which the container IP of the ASP.NET Hello MVC container is automatically retrieved and injected in the Apache HTTP configuration file.

Here is a list of supported environment variable values:

  • {{alphanumeric | 8}}: creates a random 8-character alphanumeric string. This is most useful for creating random passwords.
  • {{Image Name | ip}}: allows you to enter the host IP address of a container as a value for an environment variable. This is most useful for allowing the middleware tier to establish a connection with the database.
  • {{Image Name | container_hostname}} or {{Image Name | container_ip}}: allows you to enter the name of a container as a value for an environment variable. This is most useful for allowing the middleware tier to establish a secure connection with the database (without exposing the database port).
  • {{Image Name | container_private_ip}}: allows you to enter the internal IP of a container as a value for an environment variable. This is most useful for allowing the middleware tier to establish a secure connection with the database (without exposing the database port).
  • {{Image Name | port_Port Number}}: allows you to enter the Port number of a container as a value for an environment variable. This is most useful for allowing the middleware tier to establish a connection with the database. In this case, the port number specified needs to be the internal port number – i.e. not the external port that is allocated to the container. For example, {{PostgreSQL | port_5432}} will be translated to the actual external port that will allow the middleware tier to establish a connection with the database.
  • {{Image Name | Environment Variable Name}}: allows you to enter the value an image’s environment variable into another image’s environment variable. The use cases here are endless – as most multi-tier applications will have cross-image dependencies.

Using Plug-Ins and the Host Parameter to Deploy a Highly Available Docker Java Application

When creating application templates in HyperForm, the  host parameter allows you to specify the host you would like to use for container deployments. This is possible if you have selected  Weave as the networking layer when  creating your clusters. That way you can ensure high-availability for your application server clusters across different hosts (or regions) and you can comply with affinity rules to ensure that the database runs on a separate host for example. Here are the values supported for the host parameter:

  • host1, host2, host3, etc.: selects a host randomly within a data-center (or cluster) for container deployments.
  • IP Address 1, IP Address 2, etc.: allows a user to specify the actual IP addresses to use for container deployments.
  • Hostname 1, Hostname 2, etc.: allows a user to specify the actual hostnames to use for container deployments.
  • Wildcards (e.g. “db-”, or “app-srv-”): to specify the wildcards to use within a hostname.

Using Plug-In Lifecycle Stages for Docker Service Discovery

The service discovery framework in HyperForm provides event-driven life-cycle stages that executes custom scripts to re-configure application components. This is critical when scaling out clusters for which a load balancer may need to be re-configured or a replica set may need to be re-balanced.

You will notice that the Nginx and Apache HTTP plug-in are getting executed during these different stages or events:

  • When the Nginx or the Apache HTTP container is created: In this case, the container IP's of the application servers are injected into the default configuration file to facilitate the load balancing to the right services.
  • When the ASP.NET application server cluster is scaled in or scale out: In this case, the updated container IP's of the application servers are injected into the default configuration file to facilitate the load balancing to the right services.
  • When the ASP.NET application servers are stopped or started: In this case, the updated container IP's of the application servers are injected into the default configuration file to facilitate the load balancing to the right services.

So the service discovery framework here is doing both service registration (by keeping track of the container IP's and environment variable values) and service discovery (by executing the right scripts during certain events or stages).

The lifecycle parameter in plug-ins allows you to specify the exact stage or event to execute the plug-in. If no lifecycle is specified, then by default, the plug-in will be executed on_create. Here are the supported lifecycle stages:

  • on_create: executes the plug-in when creating the container.
  • on_start: executes the plug-in after a container starts.
  • on_stop: executes the plug-in before a container stops.
  • on_destroy: executes the plug-in before destroying a container.
  • pre_create: executes the plug-in before creating the container.
  • post_create: executes the plug-in after the container is created and running.
  • post_start[:Node]: executes the plug-in after another container starts.
  • post_stop[:Node]: executes the plug-in after another container stops.
  • post_destroy[:Node]: executes the plug-in after another container is destroyed.
  • post_scale_out[:Node]: executes the plug-in after another cluster of containers is scaled out.
  • post_scale_in[:Node]: executes the plug-in after another cluster of containers is scaled in.
  • cron(0 1 1 * * ?): schedules the plug-in based on a specified cron expression. Here are some examples for cron expressions.
  • exec_on_machine: executes the plug-in on the underlying machine. This lifecycle can be used with other container life cycles. For example, exec_on_machine pre_create will execute the plug-in on the machine before creating the container.

Provisioning the Underlying Infrastructure on Any Cloud

Once an application is saved, a user can deploy the application to a cluster (or pool of servers) that is already available or provision infrastructure on 18 different cloud endpoints including HyperGrid, VMware vSphere, OpenStack, CloudStack, Amazon Web Services, Rackspace, Microsoft Azure, DigitalOcean, IBM SoftLayer, and many others.

For detailed instructions on automating infrastructure provisioning, please refer to the detailed documentation here.

The recorded demo in this blog uses a cluster of provisioned VM’s on Microsoft Azure Resource Manager. For instructions on automating the provisioning of VM’s on ARM using reusable YAML templates, please refer to the detailed documentation here.

Deploying the ASP.NET Application on the HyperGrid Cluster

A user can deploy the ASP.NET applications on any of the available clusters. On the HyperForm SaaS Platform (http://dchq.io), a public cluster set up on HyperGrid infrastructure is available for free application deployment (up to 2GB of Memory). This cluster is meant for DEV/TEST deployments only and is refreshed every two to three months.

To deploy the application on the HyperGrid cluster, a user can navigate to the Library page, click on Customize and then select the available HyperGrid cluster:

Picture


Accessing the In-Browser Terminal For The Running Containers

A command prompt icon should be available next to the containers’ names on the Apps page. This allows users to enter the container using a secure communication protocol through the agent message queue. A white list of commands can be defined by the Tenant Admin to ensure that users do not make any harmful changes on the running containers.

For the ASP.NET deployment for example, we used the command prompt to make sure that the application code is indeed under the /app directory.

Picture


Monitoring The CPU, Memory & I/O Utilization Of The Running Containers

Once the application is up and running, users can monitor the CPU, Memory, & I/O of the running containers to get alerts when these metrics exceed a pre-defined threshold. This is especially when performing functional & load testing.

A user can perform historical monitoring analysis and correlate issues to container updates or build deployments. This can be done by clicking on the Stats link. A custom date range can be selected to view CPU, Memory, and I/O historically.

Picture


Replacing (Redeploying) Containers When New Images Are Pushed into a Registry

For developers wishing to follow the “immutable” containers model by rebuilding Docker images containing the application code and spinning up new containers with every application update, HyperForm provides an automated container replacement workflow that pulls that latest image in a registry and replaces the running container with the latest image.

To set up a container redeployment (or replacement) profile, a user can select Container Redeploy Profile from the Action menu.

Picture


A user can then select one of the stored Docker registries and enter the name of the image that should be used when replacing the running container. To register a Docker Hub, Nexus, Artifactory or Quay account, a user should navigate to Cloud Providers and clicking on the + to select Docker Registries.

Picture


Scaling Out The ASP.NET Application

If the running application becomes resource constrained, a user can scale out the application to meet the increasing load. Moreover, a user can schedule the scale out during business hours and the scale in during weekends for example.

To scale out the cluster of the ASP.NET containers from one to two, a user can click on the Actions menu of the running application and then select Scale Out. A user can then specify the new size for the cluster and then click on Run Now.

The service discovery plug-in configured for the load balancer (i.e. Nginx or Apache HTTP) will be automatically triggered to inject the container IP's of the new application servers into the default configuration file to facilitate the load balancing to the right services.

Picture


An application timeline is available to track every change made to the application for auditing and diagnostics. This can be accessed from the expandable menu at the bottom of the page of a running application.

Picture


Alerts and notifications are available for when containers or hosts are down or when the CPU and Memory Utilization of either hosts or containers exceed a defined threshold.

Executing Plug-Ins Post-Provision to Update an Application

If a user would like to make simple changes to the ASP.NET application by leveraging ASP.NET Dynamic Compilation (https://msdn.microsoft.com/en-us/library/ms366723.aspx), then a custom script plug-in can be invoked to make such updates.

The Plug-ins framework, which relies on custom scripts that can be written in BASH, PowerShell, Perl, Ruby or Python, enables advanced application deployment and facilitates quick integration with any external service – including storage, networking or monitoring solutions.

To do this, a user must first create a plug-in by navigating to Blueprints > Plug-ins.

Then from the application’s page, a user can select Plug-ins Run from the Actions menu.

Picture


A user can then search for the plug-in and override the available arguments if needed (e.g. URL to fetch the latest ASP.NET files). Because ASP.NET Dynamic Compilation is used in this example, then a container restart is not needed. However, for other updates that require a quick restart of the container, then the Restart toggle can be selected.

Picture


Conclusion

Containerizing enterprise applications is still a challenge mostly because existing application composition frameworks do not address complex dependencies, external integrations or application life-cycle management tasks post-provision.

HyperForm, available in hosted and on-premise versions, addresses all of these challenges and simplifies the containerization of enterprise applications through an advanced application composition framework that extends Docker Compose supporting:

  • advanced plug-ins that can be invoked at more than 20 different lifecycle stages to enable service discovery, on-the-fly containerization and application storage automation, and
  • data injection to support complex application dependencies.

To run & manage the simple Docker ASP.NET "Hello MVC" and “Hello Web” applications on 18 different clouds and virtualization platforms (including HyperGrid, vSphere, OpenStack, AWS, Rackspace, Microsoft Azure, Google Compute Engine, DigitalOcean, IBM SoftLayer, etc.), make sure that you either:

Related Refcard:

MongoDB Atlas is the best way to run MongoDB on AWS — highly secure by default, highly available, and fully elastic. Get started free. Brought to you in partnership with MongoDB.

Topics:
image ,docker hub ,.net ,docker compose

Published at DZone with permission of Amjad Afanah, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}