Deploying a Simple JavaScript Game on AWS EC2 with K3s
Introduction
In the era of cloud computing, deploying applications has become significantly easier and more efficient. One popular approach involves using containerization platforms like Kubernetes to manage application deployments across a cluster of servers. This article will guide you through the process of setting up a lightweight Kubernetes distribution (K3s) on AWS EC2 and deploying a simple JavaScript game as a demonstration.
Why K3s and AWS EC2?
- K3s: K3s is a lightweight and streamlined version of Kubernetes, ideal for small deployments or environments with limited resources. It's designed to be easy to install and manage, making it perfect for developers and smaller teams.
-
AWS EC2: Amazon Elastic Compute Cloud (EC2) provides a scalable and reliable platform for hosting your applications. EC2 offers a wide range of instance types to suit your needs, making it a flexible choice for various workloads.
Prerequisites
- AWS Account: You need an active AWS account to create EC2 instances.
- Basic Linux Knowledge: Familiarity with basic Linux commands is recommended.
- Docker: Docker is required to build and package the game as a container.
- Git: Git is used to manage the game source code.
-
Basic Kubernetes Concepts: Understanding the core Kubernetes concepts like Pods, Deployments, and Services will be helpful.
Step-by-Step Guide
- Setting Up the AWS EC2 Instance
- Launch an Instance: Log in to your AWS console and navigate to EC2. Select "Launch Instance".
- Choose an AMI: Select an Amazon Machine Image (AMI) that includes the Linux distribution of your choice (e.g., Amazon Linux 2).
- Configure Instance Details: Configure the instance details, including the instance type, storage size, and security group.
- Add Storage: Add a volume for your game files and configuration.
- Configure Security Group: Configure the security group to allow SSH access (port 22) and the port that your game will be running on (e.g., 8080).
-
Launch: Launch the instance and note the public DNS hostname.
- Installing K3s on the EC2 Instance
- Connect via SSH: Use an SSH client (e.g., PuTTY) to connect to your EC2 instance using the public DNS hostname and the security group's SSH port (22).
- Download and Install K3s: Download the K3s installation script and run it. You can find the latest version at the official K3s website: https://k3s.io/
curl -sfL https://get.k3s.io | sh -
- Verify Installation: Check that K3s is running:
kubectl version
3. Creating a Game Container
-
Create a Dockerfile: Create a file named
Dockerfile
in your game's directory:
FROM node:16-alpine
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm", "start"]
- Build the Docker Image: Build the Docker image using the following command:
docker build -t my-js-game .
4. Deploying the Game to K3s
-
Create a Deployment: Create a Kubernetes Deployment file (e.g.,
deployment.yaml
):
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-js-game
spec:
replicas: 1
selector:
matchLabels:
app: my-js-game
template:
metadata:
labels:
app: my-js-game
spec:
containers:
- name: my-js-game
image: my-js-game:latest
ports:
- containerPort: 8080
-
Create a Service: Create a Kubernetes Service file (e.g.,
service.yaml
):
apiVersion: v1
kind: Service
metadata:
name: my-js-game
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: my-js-game
- Apply the Files: Apply the deployment and service configuration files to the K3s cluster:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
5. Accessing the Game
-
Get the Public IP: Once the service is created, check the output of
kubectl get services
. You'll see a Public IP address assigned to the service. -
Access the Game: Access your game by opening the Public IP address in a web browser.
Example: A Simple JavaScript Game
Here's a simple JavaScript game that demonstrates the deployment process:
// game.js
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
let x = canvas.width / 2;
let y = canvas.height / 2;
let radius = 20;
let dx = 2;
let dy = -2;
function drawBall() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fillStyle = 'red';
ctx.fill();
ctx.closePath();
}
function update() {
x += dx;
y += dy;
// Bounce off walls
if (x + radius > canvas.width || x - radius < 0) {
dx = -dx;
}
if (y + radius > canvas.height || y - radius < 0) {
dy = -dy;
}
drawBall();
requestAnimationFrame(update);
}
update();
-
Create the Game: Save the above code in a file named
game.js
. -
Create an HTML File: Create an HTML file (e.g.,
index.html
) with the following content:
<!DOCTYPE html>
<html>
<head>
<title>
Simple Game
</title>
</head>
<body>
<canvas height="300" id="gameCanvas" width="400">
</canvas>
<script src="game.js">
</script>
</body>
</html>
-
Follow the Deployment Steps: Build the Docker image and deploy the game to K3s as described above.
Conclusion
Deploying a simple JavaScript game on AWS EC2 using K3s simplifies the development and deployment process. By using containerization and Kubernetes, you can easily scale your application, manage updates, and ensure high availability. K3s' lightweight nature makes it ideal for smaller projects or when resources are limited. This approach provides a powerful foundation for building and deploying more complex web applications.
Best Practices:
- Secure Your Cluster: Use strong passwords, configure access control policies, and enable encryption for secure communication.
- Monitoring and Logging: Implement monitoring tools to track the health of your cluster and applications, and configure logging to diagnose issues.
- Backup and Recovery: Regularly back up your data and have a disaster recovery plan in place.
- Continuous Integration/Continuous Deployment (CI/CD): Use CI/CD tools to automate the deployment process and ensure consistent updates.
By following these best practices, you can optimize your cloud-native development workflow and create robust and scalable applications.