Deploy nodeJS app to kubernetes cluster with load balancer service

  1. Create a Dockerfile
FROM node:16.13.0 AS builder
WORKDIR /dist
COPY ./package.json ./
COPY ./package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:16.13-alpine
WORKDIR /dist
EXPOSE 4002
COPY --chown=node:node --from=builder /dist ./
USER node
CMD [“npm”, “run”, “start”]
aws eks --region us-west-2 update-kubeconfig --name my-cluster
gcloud container clusters get-credentials my-cluster --zone us-east1 --project <proj_name>
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: myprog
name: myprog
spec:
replicas: 2
selector:
matchLabels:
app: myprog
template:
metadata:
labels:
app: myprog
spec:
containers:
- image: myusername/myprog:latest
name: myprog
ports:
- containerPort: 4002
kubectl apply -f deployment.yaml
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
myprog-5dbb5d4dd5–5cs9w 1/1 Running 0 73s
myprog-5dbb5d4dd5-fx8kx 1/1 Running 0 73s
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
myprog-5dbb5d4dd5 2 2 2 57m
apiVersion: v1
kind: Service
metadata:
name: myprog-service
spec:
type: LoadBalancer
selector:
app: myprog
ports:
- protocol: TCP
port: 4002
targetPort: 4002
kubectl apply -f service.yaml
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myprog-service LoadBalancer 172.20.141.130 a971e8a5–158085.us-west-2.elb.amazonaws.com 4002:30125/TCP 68m
$ curl -X GET http://a971e8a5–158085.us-west-2.elb.amazonaws.com:4002/api/v1/hello
helloworld
kubectl delete deployment myprogkubectl delete service myprog-service

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store