After successfully deployed the application (pod looks OK, logs are ok), I created ClusterIP service, then created NGINX Ingress and added host to hosts file. When I try to access posts.com/posts
, I get 502 bad gateway error.
ingress-srv.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-srv
annotations:
nginx.ingress.kubernetes.io/service-upstream: "true"
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: posts.com
http:
paths:
- path: /posts
pathType: Prefix
backend:
service:
name: posts-clusterip-srv
port:
number: 4000
posts-depl.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: posts-depl
spec:
replicas: 1
selector:
matchLabels:
app: posts
template:
metadata:
labels:
app: posts
spec:
containers:
- name: posts
image: myUser/posts
resources:
limits:
memory: 512Mi
cpu: "1"
requests:
memory: 256Mi
cpu: "0.2"
ports:
- containerPort: 4000
---
apiVersion: v1
kind: Service
metadata:
name: posts-clusterip-srv
spec:
selector:
app: posts
ports:
- name: posts
protocol: TCP
port: 4000
targetPort: 4000
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 posts.com
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
Dockerfile
FROM node:alpine
WORKDIR /app
COPY package.json ./
RUN npm install
COPY ./ ./
CMD ["npm", "start"]
What can be the problem and where at least I should look for?
I see two problems - try these and let me know if it helps or not:
pathType
in your Ingress manifest. It is mandatory to provide this otherwise Kubernetes won't understand whether it is a exact match or a prefix match. Read more over here. (I see that @Sahadat pointed it out rightly but somehow deleted his answer).spec:
containers:
- name: posts
image: myuser/posts
ports:
- containerPort: 4000
UPDATE 1:
One more potential problem could be this entry in your host file - 127.0.0.1 posts.com
, delete it and try.