Deploying a Currency Converter Application on Kubernetes on AWS
Yixin Cao
Posted on April 6, 2023
This process is fully powered by ChatGPT 3.5. Not tested.
The Currency Converter application is a simple web application that allows users to convert US dollars to Chinese RMB or Chinese RMB to US dollars. In this tutorial, we'll show you how to deploy the application using Kubernetes on AWS.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- A working React application that you want to deploy
- A basic understanding of Kubernetes and Docker
- An AWS account
- A local development environment with the following tools installed:
- Docker
- kubectl command-line tool
- AWS CLI
Step 1: Containerize the Application
Before deploying the application, you'll need to containerize it using Docker. Here are the steps to containerize the application:
- Create a Dockerfile in the root directory of your project with the following content:
FROM node:latest
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
This Dockerfile defines a base image of the latest version of Node.js, sets the working directory to /app, and copies the package.json and package-lock.json files to the container. Then, it installs the required dependencies, copies the source code to the container, exposes port 3000, and starts the application using the npm start command.
- Use the Dockerfile to build a Docker image of the application:
docker build -t currency-converter .
This command builds a Docker image with the name currency-converter.
- Test the Docker image locally by running it with Docker:
docker run -p 3000:3000 currency-converter
This command starts a container with the currency-converter image and maps port 3000 on the container to port 3000 on your local machine. You can access the application in your web browser at http://localhost:3000.
Step 2: Set Up an Amazon EKS Cluster
Amazon Elastic Kubernetes Service (EKS) is a fully-managed Kubernetes service provided by AWS. Here's how to set up an EKS cluster in your AWS account:
- Open the Amazon EKS console at https://console.aws.amazon.com/eks/home.
- Click the Create cluster button.
- On the Cluster configuration page, provide a unique name for your cluster, and select the Kubernetes version you want to use. Leave the rest of the settings at their default values.
- Click the Next button.
- On the Node group configuration page, choose the instance types and the number of nodes you want to use for your cluster. You can leave the rest of the settings at their default values.
- Click the Next button.
- On the Networking configuration page, choose the VPC and subnets that you want to use for your cluster. Leave the rest of the settings at their default values.
- Click the Create button to create your EKS cluster.
Step 3: Set Up the kubectl Command-Line Tool
kubectl is the Kubernetes command-line tool that you'll use to manage your cluster. Here's how to install and configure kubectl:
- Install kubectl by following the instructions for your operating system on the Kubernetes documentation: https://kubernetes.io/docs/tasks/tools/install-kubectl/.
- Configure kubectl to use your EKS cluster by running the following command:
aws eks update-kubeconfig --name <your-cluster-name>
Replace with the name of your EKS cluster.
Step 4: Deploy the Application to the EKS Cluster
Now that you have containerized the application and set up an EKS cluster, it's time to deploy the application to the cluster. Here's how:
- Create a Kubernetes deployment for the application:
Create a file named deployment.yaml
with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: currency-converter
spec:
replicas: 1
selector:
matchLabels:
app: currency-converter
template:
metadata:
labels:
app: currency-converter
spec:
containers:
- name: currency-converter
image: <your-aws-account-id>.dkr.ecr.<your-aws-region>.amazonaws.com/currency-converter:latest
ports:
- containerPort: 3000
This YAML file defines a Kubernetes deployment with one replica and one container. The container is based on the Docker image you built earlier, which is stored in the Amazon Elastic Container Registry (ECR). Replace <your-aws-account-id>
and <your-aws-region>
with your AWS account ID and region, respectively.
- Apply the deployment to the EKS cluster by running the following command:
kubectl apply -f deployment.yaml
This command creates a deployment named currency-converter and starts one replica of the application.
- Expose the application using a Kubernetes service:
Create a file named
service.yaml
with the following content:
apiVersion: v1
kind: Service
metadata:
name: currency-converter
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 3000
selector:
app: currency-converter
This YAML file defines a Kubernetes service that exposes the application on port 80 using a load balancer. Replace currency-converter
with the name of your deployment.
- Apply the service to the EKS cluster by running the following command:
kubectl apply -f service.yaml
This command creates a service named currency-converter and exposes the application to the internet.
- Verify that the application is running by running the following command:
kubectl get service currency-converter
This command displays information about the service, including the external IP address that you can use to access the application in your web browser.
Conclusion
In this tutorial, we showed you how to deploy a Currency Converter application using Kubernetes on AWS. We containerized the application using Docker, set up an EKS cluster, deployed the application to the cluster, and exposed it to the internet using a Kubernetes service. With these steps, you can deploy any web application using Kubernetes on AWS.
Posted on April 6, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.