简体   繁体   English

Nginx 入口找不到服务

[英]Nginx ingress can't find the service

I created a nginx ingress using this link using docker-desktop.我使用 docker-desktop 使用 此链接创建了一个 nginx 入口。

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.0/deploy/static/provider/cloud/deploy.yaml

My service:我的服务:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: be-assinaturas
  namespace: apps-space
  labels:
    tier: backend
    app: assinaturas
spec:
  selector:
    matchLabels:
      name: be-assinaturas
      app: assinaturas
  strategy:
    type: Recreate
  replicas: 2
  template:
    metadata:
      name: be-assinaturas
      labels:
        app: assinaturas
        name: be-assinaturas
    spec:
      containers:
        - name: be-assinaturas
          image: jedi31/assinaturas:latest
          imagePullPolicy: Always

---
kind: Service
apiVersion: v1
metadata:
  name: svc-assinaturas
  namespace: apps-space
spec:
  selector:
    name: be-assinaturas
    app: assinaturas
  type: ClusterIP
  ports:
    - name: be-assinaturas-http
      port: 80
      targetPort: 80

My ingress resource is defined as:我的入口资源定义为:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata: 
  name: ingress-be-assinaturas
  namespace: apps-space
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
    - http:
        paths: 
        - path: /assinaturas
          pathType: Prefix
          backend:
            service:              
              name: svc-assinaturas
              port:
                number: 80

Running a kubectl get services --all-namespaces I get运行kubectl get services --all-namespaces我得到

NAMESPACE       NAME                                 TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
apps-space      svc-assinaturas                      ClusterIP      10.107.188.28    <none>        80/TCP                       12m
default         kubernetes                           ClusterIP      10.96.0.1        <none>        443/TCP                      66d
ingress-nginx   ingress-nginx-controller             LoadBalancer   10.102.238.173   localhost     80:32028/TCP,443:30397/TCP   5h45m
ingress-nginx   ingress-nginx-controller-admission   ClusterIP      10.98.148.190    <none>        443/TCP                      5h45m
kube-system     kube-dns                             ClusterIP      10.96.0.10       <none>        53/UDP,53/TCP,9153/TCP       66d

If I do a port forward on service, like this: kubectl port-forward -n apps-space service/svc-assinaturas 5274:80如果我对服务进行端口转发,如下所示: kubectl port-forward -n apps-space service/svc-assinaturas 5274:80

I can acess my app using curl, like this: curl -v http://localhost:5274/hc/alive我可以使用 curl 访问我的应用程序,如下所示: curl -v http://localhost:5274/hc/alive

and the response is:回应是:

*   Trying 127.0.0.1:5274...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 5274 (#0)
> GET /hc/alive HTTP/1.1
> Host: localhost:5274
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Date: Mon, 06 Dec 2021 23:22:40 GMT
< Server: Kestrel
< Transfer-Encoding: chunked
<
* Connection #0 to host localhost left intact
{"service":"Catalogo","status":"Alive","version":"bab4653"}

But if I try access the service using Ingress, it returns a 404. curl -v http://localhost/assinaturas/hc/alive但是,如果我尝试使用curl -v http://localhost/assinaturas/hc/alive访问该服务,它会返回 404。

*   Trying 127.0.0.1:80...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /assinaturas/hc/alive HTTP/1.1
> Host: localhost
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Date: Mon, 06 Dec 2021 23:22:51 GMT
< Content-Length: 0
< Connection: keep-alive
<
* Connection #0 to host localhost left intact

What I'm doing wrong here?我在这里做错了什么? Why I can acess the service, but the ingress do not find it?为什么我可以访问该服务,但入口找不到它?

this is because the prefix /assinaturas need to be omitted by an Nginx rewrite .. And that's explain why you got 404 (not found):这是因为前缀/assinaturas需要被 Nginx重写省略。这就是为什么你得到 404(未找到)的原因:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata: 
  name: ingress-be-assinaturas
  namespace: apps-space
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /$1 # <- 🔴 rewrite here
spec:
  rules:
    - http:
        paths: 
        - path: /assinaturas/(.*) # <--  🔴 Match the path to be rewritten
          pathType: Prefix
          backend:
            service:              
              name: svc-assinaturas
              port:
                number: 80

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM