🟢
Kubernetes

Node.js Microservices on K8s

Node API gateway + worker Deployments with ConfigMap env and internal service DNS

📋Configuration Files

configmap.yml

yaml
1apiVersion: v1
2kind: ConfigMap
3metadata:
4 name: node-api-config
5data:
6 NODE_ENV: "production"
7 WORKER_URL: "http://worker-svc:4000"

gateway-deployment.yml

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: gateway
5spec:
6 replicas: 2
7 selector:
8 matchLabels:
9 app: gateway
10 template:
11 metadata:
12 labels:
13 app: gateway
14 spec:
15 containers:
16 - name: gateway
17 image: node-gateway:local
18 imagePullPolicy: IfNotPresent
19 ports:
20 - containerPort: 3000
21 envFrom:
22 - configMapRef:
23 name: node-api-config
24 resources:
25 requests:
26 memory: "128Mi"
27 cpu: "100m"
28 limits:
29 memory: "256Mi"
30 cpu: "250m"

gateway-svc.yml

yaml
1apiVersion: v1
2kind: Service
3metadata:
4 name: gateway-svc
5spec:
6 type: NodePort
7 selector:
8 app: gateway
9 ports:
10 - port: 3000
11 targetPort: 3000
12 nodePort: 32300

worker-deployment.yml

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: worker
5spec:
6 replicas: 1
7 selector:
8 matchLabels:
9 app: worker
10 template:
11 metadata:
12 labels:
13 app: worker
14 spec:
15 containers:
16 - name: worker
17 image: node-worker:local
18 imagePullPolicy: IfNotPresent
19 ports:
20 - containerPort: 4000
21 resources:
22 requests:
23 memory: "128Mi"
24 cpu: "100m"

worker-svc.yml

yaml
1apiVersion: v1
2kind: Service
3metadata:
4 name: worker-svc
5spec:
6 type: ClusterIP
7 selector:
8 app: worker
9 ports:
10 - port: 4000
11 targetPort: 4000
📄

Step 01

Deploy Node Microservices

(01)Build and load images

Linux
1docker build -t node-gateway:local ./gateway
2docker build -t node-worker:local ./worker
3minikube image load node-gateway:local
4minikube image load node-worker:local

(02)Apply manifests

Linux
1kubectl apply -f configmap.yml
2kubectl apply -f gateway-deployment.yml
3kubectl apply -f gateway-svc.yml
4kubectl apply -f worker-deployment.yml
5kubectl apply -f worker-svc.yml
6kubectl get pods,svc