I am new to k8s and nginx. I am currently implementing an nginx ingress on my microk8s kubernetes via yaml since it’s an nginx plus. Here is the result:
myubuntu$ kubectl get all -n nginx-ingress
NAME READY STATUS RESTARTS AGE
pod/nginx-ingress-8dd4c95f5-b5qzs 1/1 Running 0 3d23h
pod/nginx-ingress-8dd4c95f5-vbvc6 1/1 Running 0 3d23h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-ingress NodePort 10.152.183.119 <none> 80:31656/TCP,443:30406/TCP 4d3h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-ingress 2/2 2 2 4d3h
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-ingress-8dd4c95f5 2 2 2 3d23h
replicaset.apps/nginx-ingress-c4d985f57 0 0 0 4d3h
I have also deploy a testing app with the following yaml values:
apiVersion: v1
kind: Namespace
metadata:
name: web
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-server
namespace: web
labels:
app: web
annotations:
deployment.kubernetes.io/revision: '1'
sensor.crowdstrike.com/imageanalyzer: "enable"
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: test
image: my docker image
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: web-server-service
namespace: web
spec:
selector:
app: web
type: ClusterIP
ports:
- port: 3000
targetPort: http
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-server-ingress
namespace: web
annotations:
kubernetes.io/ingress.class: nginx-ingress
spec:
ingressClassName: nginx
rules:
- host: web.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web-server-service
port:
number: 3000
I have check and confirm that both services work good independently, but when I tried to configure the port in the web ingress file, the nginx plus ingress always return 404? Is it a problem with my config file or namespace? Thanks in advance.
Deploy an app on microk8s with custom nginx plus ingress.
2
Answers
You can set up your services on another namespace and have your ingress controllers in another namespace.
For nginx, most times, when there is a 404 error, the hostname is either invalid or not correctly represented. Or the path is wrong.
I set up a local nginx ingress and applied your configuration using nginx pod and 80 as my container port. I changed the hostname of the
web-server-ingress
to localhost. below is a sample of what I ran:In your case, I would advise you to grab the name of the ingress-controller pod:
The name for me is
ingress-nginx-controller-77cfd766fc-2h4k8
Then try to view the logs:
You will notice no request log if the hostname does not match.
If there are logs, you will get information about the request made to the upstream service:
Below is a sample 200 response:
Notice the part after
[web-web-server-service-80]
records the pod that was reached and the port, the request length, time and response, and an ID for the request.Here is a log for a 503 response:
Notice there is no log for the upstream service request/response
When the hostname does not match, the controller records no log and returns a 404 to the client.
I believe that ports are configured wrong in your yaml-files. I’m assuming this is a Node app given port 3000, and that you want to expose your app on port 80 (http).
In deployment.yaml, use this
In service.yaml, use this
In ingress.yaml, use this
For accepting requests from localhost, replace
with
or remove
host
.