ingress ip is providing expected result but host returns 404 http not found
Ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: helloworld-ing
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
defaultBackend:
service:
name: helloworld-svc
port:
number: 8080
ingressClassName: nginx
tls:
- hosts:
- helloworld.dev.com
secretName: ingress-tls-csi
rules:
- host: helloworld.dev.com
http:
paths:
- path: /helloworld
pathType: Prefix
backend:
service:
name: helloworld-svc
port:
number: 8080
Ingress Ip was not working earlier but adding default backend resolved that issue. I believe this can be the issue that its not going past backend and not even reaching rules.
I do not see warning/errors in ingress logs but If I remove default backend I can not even access app using ingress IP.
I am not sure what I am missing in my ingress configuration.
I am trying same path for url and Ip -
Curl http://10.110.45.61/helloworld/service/result
Curl http://helloworld.dev.com/helloworld/service/result
I am happy to provide more information if required.
CodePudding user response:
Hello, Hope you'are enjoying your Kubernetes journey !
So This is what I have tested for a first try (i havent tested tls now): First I have setup a kind cluster locally with this configuration (info here: https://kind.sigs.k8s.io/docs/user/quick-start/):
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: so-cluster-1
nodes:
- role: control-plane
image: kindest/node:v1.23.5
- role: control-plane
image: kindest/node:v1.23.5
- role: control-plane
image: kindest/node:v1.23.5
- role: worker
image: kindest/node:v1.23.5
- role: worker
image: kindest/node:v1.23.5
- role: worker
image: kindest/node:v1.23.5
after this I create my cluster with this command:
kind create cluster --config=config.yaml
Next, i have created a test namespace (manifest obtained with: kubectl create ns so-tests -o yaml --dry-run):
apiVersion: v1
kind: Namespace
metadata:
name: so-tests
then I created this vanilla nginx deployment and exposed it with a service, here is the config (manifest from here https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#creating-a-deployment):
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
same with the pod (manifest from obtained with: k expose deployment nginx-deployment --dry-run -o yaml ):
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: nginx
name: nginx-deployment
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
after applying every manifests to my cluster and checked if my pods were running, I made sure that i could access my nginx pod web homepage by running:
kubectl port-forward pod/nginx-deployment-74d589986c-c85r9 8080:80 #checked on localhost:8080 and it was succesful
I've done the same against the service to make sure that it was correctly redirecting the traffic to the pod:
k port-forward service/nginx-deployment 8080:80 #checked on localhost:8080 and it was succesful
When I was sure that my workload was correctly running and accessible, I installed nginx ingress controller (from here: https://kubernetes.github.io/ingress-nginx/deploy/) with this command:
helm upgrade --install ingress-nginx ingress-nginx \
--repo https://kubernetes.github.io/ingress-nginx \
--namespace ingress-nginx --create-namespace
Then I created the ingress k8s resource, here is the config (obtained by running: k create ingress demo-localhost --class=nginx --rule="demo.localdev.me/*=demo:80" and by replacing the service.name by the name of my service.):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
creationTimestamp: null
name: demo-localhost
spec:
ingressClassName: nginx
rules:
- host: demo.localdev.me
http:
paths:
- backend:
service:
name: nginx-deployment
port:
number: 80
path: /
pathType: Prefix
Then to check if my ingress was corretly redirecting the traffic i ran:
kubectl port-forward --namespace=ingress-nginx service/ingress-nginx-controller 8080:80 #I also tested this on my browser localhost:8080
And guess what? -> 404 not found.
so, I decided to replace the name "demo.localdev.me" by "localhost" and it worked, here is the conf:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demo-localhost
spec:
ingressClassName: nginx
rules:
- host: localhost #demo.localdev.me
http:
paths:
- backend:
service:
name: nginx-deployment
port:
number: 80
path: /
pathType: Prefix
I went to my C:\Windows\System32\drivers\etc\hosts file on windows (equivalent to /etc/hosts on linux, or equivalent to your DNS serveur in enterprise) to check if it was a dns issue, i added this line:
127.0.0.1 demo.localdev.me
and it worked as expected. (make sure to clear you browser cache when playing with /etc/hosts file or to use private navigation to get accurate resulsts)
So, you can do some testing: make sure that:
- your path is correctly reachable (you can do the testing with kubectl port-forward, as i have done before)
- you do have the correct entry in dns that redirects the traffic to your nginx service (not your application pod service)
- you do not have a firewall that is blocking your ports
- the tls configuration is working properly (try with and without)
If you have more information to share with us, feel free, i'll be happy to help you !
Have a nice day man !