'Nextjs app not running on port after deploying to VM (docker, kubernetes)
Trying to spin up Nextjs application on server by creating docker image and running it on a VM along with Kubernetes.
After deployment, I can see my nextjs pod running fine and even in logs it claimed that server has started. However I can't access the web via browser nor curl the url.
I have tried a few things
- Changing from port 3000 to 5000 to confirm port 3000 is not blocked by whatever reason
- Check result of
lsof -i :5000, I see nothing showing up. - Check result of
curl http://localhost:5000, I gotcurl: (7) Failed to connect to localhost port 5000: Connection refusedreturn. - Verified my nextjs pod's log, as below
> [email protected] dev /usr/src/app
> next dev
ready - started server on 0.0.0.0:5000, url: http://localhost:5000
wait - compiling...
event - compiled client and server successfully in 2.2s (171 modules)
Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
https://nextjs.org/telemetry
Code reference
Below is the Dockerfile and deployment.yaml use
Dockerfile
FROM node:12
ENV PORT 5000
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Installing dependencies
COPY package*.json /usr/src/app/
RUN npm install
# Copying source files
COPY . /usr/src/app
# Building app
RUN npm run build
EXPOSE 5000
# Running the app
CMD "npm" "run" "dev"
# Running the app
CMD [ "npm", "start" ]
Deployment Yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nextjs-web
spec:
selector:
matchLabels:
app: nextjs-web
replicas: 1
template:
metadata:
labels:
app: nextjs-web
spec:
containers:
- name: nextjs
image: maskedPath/dockerimages/nextjs:1234
ports:
- containerPort: 5000
imagePullSecrets:
- name: acr-secret
---
apiVersion: v1
kind: Service
metadata:
name: nextjs-web
spec:
selector:
app: nextjs-web
ports:
- protocol: TCP
port: 80
targetPort: 5000
Other details
Describe pod
Name: nextjs-web-c9cfb675d-6thbx
Namespace: default
Priority: 0
Node: maskedData
Start Time: Thu, 21 Apr 2022 15:23:56 +0000
Labels: app=nextjs-web
pod-template-hash=c9cfb675d
Annotations: <none>
Status: Running
IP: maskedData
IPs:
IP: maskedData
Controlled By: ReplicaSet/nextjs-web-c9cfb675d
Containers:
nextjs:
Container ID: containerd://c7bbad112c6915af0aea2a7a7e1a7f42a87840e7e22ca1fe49afdc0000000000
Image: maskedData/dockerimages/nextjs:1234
Image ID: maskedData/dockerimages/nextjs@sha256:000000000036f0e0728c128322f95a9585b6724332d3f853e7f240a8df2b0ad3
Port: 5000/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 21 Apr 2022 15:24:09 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-n72pn (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-n72pn:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-n72pn
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
Solution 1:[1]
Finally have it work, the catch is to expose the app in services via type load balancer. Hence this app will be accessible to the internet world.
Command below can be use to expose.
kubectl expose deployment nextjs-web --type=LoadBalancer --name=nextjs-web
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TommyLeong |
