Sign inSign up

paulopatto/multiport-nodejs

By paulopatto

•Updated about 1 year ago

A sample application to run in GKE

Image
Languages & frameworks
Developer tools
Web servers
0

903

paulopatto/multiport-nodejs repository overview

⁠Multi-Port Node.js Application

⁠Overview

This is a multi-port Node.js application designed to demonstrate a microservice-like architecture running on Kubernetes, with support for local development and deployment.

⁠Pre-requisites

  • Node.js (v18.x or later)
  • npm (v9.x or later)
  • Docker
  • Kubernetes CLI (kubectl)
  • Minikube (for local development, optional)
  • Google Cloud SDK (for GKE deployment, optional)

⁠Local Development Setup

1. Clone the Repository

git clone https://github.com/paulopatto/multiport-nodejs-app.git
cd multiport-nodejs-app

2. Install Dependencies

# Install project dependencies
npm install

3. Run Locally

# Run the application
node app.js # or via nodemon

# Application will be available on ports:
# - 8080 (Main service)
# - 8000 (Metrics service)
# - 9000 (Admin service)

⁠Docker Build

# Build Docker image
docker build -t multiport-nodejs:v1.0.0 .

# Run Docker container
docker run -p 8080:8080 -p 8000:8000 -p 9000:9000 multiport-nodejs:v1.0.0

⁠Kubernetes Deployment

⁠Local Minikube Deployment

1. Start Minikube

minikube start

2. Build and Load Docker Image

# Build image
docker build -t multiport-nodejs:v1.0.0 .

# Load image to Minikube
minikube image load multiport-nodejs:v1.0.0

3. Deploy to Kubernetes

# Apply Kubernetes manifests
kubectl apply -f k8s-deployment.yaml
kubectl apply -f k8s-ingress.yaml
⁠Using Ingress NGINX Controller

Given our architecture's need to manage multiple services independently, a reverse proxy⁠ is an ideal choice. Essentially, a reverse proxy acts as a routing server. NGINX is a widely used tool for this purpose. To streamline our setup, we'll utilize a pre-built service called nginx-ingress-controller (found at https://github.com/kubernetes/ingress-nginx/⁠). For detailed installation steps, refer to https://kubernetes.github.io/ingress-nginx/deploy/#quick-start⁠.

⁠Enable ingress nginx controller via minikube addon
minikube addons enable ingress

or via manifest

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.12.0-beta.0/deploy/static/provider/cloud/deploy.yaml
⁠Local testing (with nginx ingress controller)

Create an ingress resource. The following example uses a host that maps to localhost. Apply nginx-ingress.yaml with:

kubectl apply -f nginx-ingress.yaml

Also config forward a local port to the ingress controller:

# Port-forward for port 8080
kubectl port-forward -n multiport service/multiport-nodejs-service 8080:8080 &

# Port-forward for port 8000
kubectl port-forward -n multiport service/multiport-nodejs-service 8000:8000 &

# Port-forward for port 9000
kubectl port-forward -n multiport service/multiport-nodejs-service 9000:9000 &

At this point, you can access your deployment using

curl

curl --resolve multiport.local.dev:8080:127.0.0.1 http://multiport.local.dev:8080
curl --resolve multiport.local.dev:8000:127.0.0.1 http://multiport.local.dev:8000
curl --resolve multiport.local.dev:9000:127.0.0.1 http://multiport.local.dev:9000

browser

Additional Step(optional): Update the /etc/hosts file to resolve the local domain:

127.0.0.1 multiport.local.dev

or if the hosts configured the service can be accessed in:

To stop the port-forward processes, you can use:

# List background jobs
jobs

# List jobs filterde by port-forward
ps aux | grep port-forward


# Kill specific job by job number
kill %1
kill %2
kill %3

⁠GKE Deployment

  1. Authenticate and Set Project
# Login to GCP
gcloud auth login

# Set your project
gcloud config set project $PROJECT_ID

# (Optional)
gcloud containers clusters create multiport-cluster
  1. Build and push docker image to registry
docker build -t paulopatto/multiport-nodejs:$VERSION .
docker push paulopatto/multiport-nodejs:$VERSION
  1. Deploy to GKE
kubectl apply -f manifest.k8s.yaml

Can check resources with:

kubectl get -n multiport deployments
kubectl get -n multiport services
kubectl get -n multiport pods

# OR just

kubectls get all -n multiport

The LoadBalancer service will provide external IP addresses use kubectl get services to find the external IPs

⁠Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Make your changes
  4. Commit your changes (git commit -m 'Add some AmazingFeature')
  5. Push to the branch (git push origin feature/AmazingFeature)
  6. Open a Pull Request

Tag summary

Content type

Image

Digest

sha256:e1ffa4e11…

Size

26.1 MB

Last updated

about 1 year ago

docker pull paulopatto/multiport-nodejs