Deploying a web application using containers can enhance scalability and manageability, especially in cloud environments. Amazon Elastic Kubernetes Service (EKS) allows you to run Kubernetes applications in a highly available and scalable manner. In this blog post, we will walk through the steps to deploy a traditional ASP.NET Framework web application on Amazon EKS.
Table of Contents
- Introduction
- Prerequisites
- Setting Up AWS EKS
- Containerizing the ASP.NET Framework Application
- Pushing the Docker Image to Amazon ECR
- Deploying the Application on EKS
- Configuring Database Access
- Testing the Application
- Monitoring and Maintenance
- Conclusion
- References
Introduction
ASP.NET Framework applications are traditionally hosted on Windows Servers, but by leveraging containers, we can run these applications in a Kubernetes environment. This guide will provide a step-by-step approach to deploying an ASP.NET Framework web application using Amazon EKS, making it easier to manage and scale.
Prerequisites
Before you start, make sure you have the following:
- AWS Account: Sign up for an AWS account if you don’t have one.
- AWS CLI: Install the AWS Command Line Interface on your local machine.
-
kubectl: Install
kubectl
to interact with your EKS cluster. - Docker: Install Docker to build and manage containers.
- Familiarity with Kubernetes: Basic understanding of Kubernetes concepts will be helpful.
Setting Up AWS EKS
Creating an EKS Cluster
- Log in to the AWS Management Console: Go to the AWS Management Console.
- Navigate to EKS: Search for EKS in the services and select it.
-
Create a New Cluster:
- Click on Add cluster and select Create.
- Provide a name for your cluster and select the appropriate Kubernetes version.
- Configure the VPC settings. You can create a new VPC or select an existing one.
- Review and create the cluster. This process can take several minutes.
Configuring kubectl
- Update kubeconfig: After the cluster is created, run the following command to update your kubeconfig file:
aws eks --region <region> update-kubeconfig --name <cluster_name>
- Verify the connection: Run the following command to verify that you can connect to the EKS cluster:
kubectl get svc
Setting Up the Node Group
-
Create a Node Group:
- Go back to the EKS dashboard and select your cluster.
- Under the Compute tab, click Add Node Group.
- Configure the node group settings, including instance types and scaling options.
- Review and create the node group. Wait for the nodes to become active.
Containerizing the ASP.NET Framework Application
Creating a Dockerfile
-
Create a Dockerfile: In the root of your ASP.NET Framework project, create a file named
Dockerfile
with the following content:
# Use the official ASP.NET Framework base image
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8
# Set the working directory
WORKDIR /inetpub/wwwroot
# Copy the application files
COPY ./bin/Release/ .
# Expose the port that the app runs on
EXPOSE 80
Building the Docker Image
-
Build the Docker Image: Open a terminal in the directory containing the
Dockerfile
and run the following command to build the image:
docker build -t my-aspnet-app .
- Verify the Image: Check the images on your local machine to ensure it was built successfully:
docker images
Pushing the Docker Image to Amazon ECR
-
Create an ECR Repository:
- Navigate to the ECR service in the AWS Management Console.
- Click on Create repository.
- Provide a name for your repository (e.g.,
my-aspnet-app
), and create it.
Authenticate Docker to ECR:
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<region>.amazonaws.com
- Tag the Docker Image:
docker tag my-aspnet-app:latest <aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-aspnet-app:latest
- Push the Docker Image to ECR:
docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-aspnet-app:latest
Deploying the Application on EKS
Creating Kubernetes Deployment
-
Create a Deployment YAML file: Create a file named
deployment.yaml
with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-aspnet-app
spec:
replicas: 2
selector:
matchLabels:
app: my-aspnet-app
template:
metadata:
labels:
app: my-aspnet-app
spec:
containers:
- name: my-aspnet-app
image: <aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-aspnet-app:latest
ports:
- containerPort: 80
- Apply the Deployment:
kubectl apply -f deployment.yaml
Creating Kubernetes Service
-
Create a Service YAML file: Create a file named
service.yaml
with the following content:
apiVersion: v1
kind: Service
metadata:
name: my-aspnet-app-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 80
selector:
app: my-aspnet-app
- Apply the Service:
kubectl apply -f service.yaml
- Get the Load Balancer's URL:
After a few minutes, run the following command to retrieve the external URL for your application:
kubectl get services
Look for the EXTERNAL-IP
of the my-aspnet-app-service
. This is the URL you will use to access your application.
Configuring Database Access
If your ASP.NET application requires a database, ensure it is accessible from your EKS cluster.
Setting Up SQL Server
- Use Amazon RDS: For production-grade applications, consider using Amazon RDS to set up a SQL Server database instance.
-
Connect to the Database: Update your application’s
web.config
file to include the database connection string. For example:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Server=my-rds-instance.abcdefg123456.us-east-1.rds.amazonaws.com;Database=YourDatabaseName;User Id=YourUsername;Password=YourPassword;"
providerName="System.Data.SqlClient" />
</connectionStrings>
Testing the Application
-
Access the Application: Open a web browser and navigate to the external URL obtained from the
kubectl get services
command. - Check Functionality: Test various functionalities of your application to ensure everything works as expected.
Monitoring and Maintenance
- Monitoring: Use Amazon CloudWatch to monitor the performance of your EKS cluster and applications.
- Logs: Implement logging within your ASP.NET application to capture errors and critical information.
- Auto-scaling: Configure horizontal pod autoscaling based on resource usage to handle traffic spikes.
Conclusion
Deploying an ASP.NET Framework web application on Amazon EKS allows you to leverage the benefits of containerization and Kubernetes orchestration. By following this guide, you can easily set up a scalable and manageable web application infrastructure in the cloud.
As you continue to explore AWS and EKS, consider utilizing advanced features like CI/CD pipelines, automated backups, and security best practices to
enhance your deployment further.