Create a web application using Python. Containerize the application using Docker and deploy it on a Kubernetes cluster (Minikube). A cloud provider for hosting is optional.
We'll use a simple Node.js application as an example. You can choose any language or framework you are comfortable with.
Create a directory for your project:
mkdir simple-web-app
cd simple-web-app
Initialize a new Node.js project:
npm init -y
Install Express.js:
npm install express
Create an app.js
file with the following content:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello, world!');
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
Create a Dockerfile
in your project directory with the following content:
# Use the official Node.js image as the base image
FROM node:14
# Set the working directory
WORKDIR /app
# Copy the package.json and package-lock.json files
COPY package*.json ./
# Install the dependencies
RUN npm install
# Copy the rest of the application files
COPY . .
# Expose the port the app runs on
EXPOSE 3000
# Command to run the application
CMD ["node", "app.js"]
Build the Docker image:
docker build -t simple-web-app .
Run the Docker container:
docker run -p 3000:3000 simple-web-app
Install Minikube: Follow the instructions here.
Start Minikube:
minikube start
Create a Kubernetes deployment file deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: simple-web-app
spec:
replicas: 2
selector:
matchLabels:
app: simple-web-app
template:
metadata:
labels:
app: simple-web-app
spec:
containers:
- name: simple-web-app
image: simple-web-app:latest
ports:
- containerPort: 3000
Create a Kubernetes service file service.yaml
:
apiVersion: v1
kind: Service
metadata:
name: simple-web-app
spec:
type: NodePort
ports:
- port: 3000
targetPort: 3000
nodePort: 30001
selector:
app: simple-web-app
Apply the Kubernetes configurations:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Get the Minikube IP:
minikube ip
Access your application:
Open your browser and go to http://<Minikube_IP>:30001
.