This is a sample project that demonstrates how to containerize a Node.js application using Docker and deploy it to Kubernetes using Minikube.
Before you begin, make sure you have the following installed:
-
Clone this repository:
git clone https://github.com/your-username/your-project.git
-
Navigate to the project directory:
cd your-project
-
Install the dependencies:
npm install
Running the application locally
To run the application locally, use the following command:
npm start
This will start the server on http://localhost:3000.
Running the application locally
To containerize the application with Docker, follow these steps:
-
Build the Docker image:
docker build -t my-express-app .
-
Run the Docker container:
docker run -p 3000:3000 my-express-app
Pushing the Docker Image to Docker Hub
-
Log in to Docker Hub using the command:
docker login
-
Tag your Docker image with your Docker Hub username and repository name using the command:
docker tag <image ID> <username>/<repository name>:<tag>
For example:
docker tag my-express-app parthrupapara/my-express-app:latest
-
Push the Docker image to Docker Hub using the command:
docker push <username>/<repository name>:<tag>
For example:
docker push parthrupapara/my-express-app:latest
Updating the Deployment YAML File
-
Open the deployment YAML file (deployment.yaml) in a text editor.
-
Update the image field under the spec.template.spec.containers section to reflect the new Docker image you pushed to Docker Hub.
For example:
template: spec: containers: - name: my-express-app image: your-username/your-repository-name:latest ports: - containerPort: 3000```
-
Save the changes to the deployment.yaml file.
Setting up a Kubernetes Cluster with Minikube
-
Start Minikube:
minikube start
-
Verify that the cluster is running:
kubectl cluster-info
Deploying the Application to Kubernetes
-
Deploy the application to Kubernetes:
kubectl apply -f deployment.yaml
kubectl apply -f kubernetes/service.yaml
kubectl get deployments
-
Get the external IP address of the service:
kubectl get services
-
Get the service URL:
minikube service my-express-app --url
-
Open the URL in a web browser or use curl to make a request:
curl http://<service-url>
The response should be "Hello, World!"
To delete the deployment and service from Kubernetes, run:
kubectl delete deployment my-express-app
kubectl delete service my-express-app
To stop Minikube, run:
minikube stop
Congratulations! You have successfully built and deployed a Node.js and Express API using Docker, Kubernetes, and Minikube. You can now use this project as a starting point for building your own APIs.