簡體   English   中英

Nginx Ingress 給出 404 未找到任何入口資源

[英]Nginx Ingress giving 404 not found for any ingress resource

Nginx 控制器在本地設置任何入口資源時都會拋出 404 錯誤。

nginx-ingress 控制器設置是根據以下文檔中詳述的步驟進行的,並且入口控制器在本地設置中作為 NodePort 服務公開

https://github.com/nginxinc/kubernetes-ingress/blob/master/docs/installation.md#5-access-the-live-activity-monitoring-dashboard--stub_status-page

以下示例入口資源用於測試入口香蕉.yaml

kind: Pod
apiVersion: v1
metadata:
  name: banana-app
  namespace: nginx-ingress
  labels:
    app: banana
spec:
  containers:
    - name: banana-app
      image: hashicorp/http-echo
      args:
        - "-text=banana"

---

kind: Service
apiVersion: v1
metadata:
  name: banana-service
  namespace: nginx-ingress
spec:
  selector:
    app: banana
  ports:
    - port: 5678 

蘋果.yaml

kind: Pod
apiVersion: v1
metadata:
  name: apple-app
  namespace: nginx-ingress
  labels:
    app: apple
spec:
  containers:
    - name: apple-app
      image: hashicorp/http-echo
      args:
        - "-text=apple"

---

kind: Service
apiVersion: v1
metadata:
  name: apple-service
  namespace: nginx-ingress
spec:
  selector:
    app: apple
  ports:
    - port: 5678 # 

入口文件.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example-ingress
  namespace: nginx-ingress
  annotations:
    ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - http:
      paths:
        - path: /apple
          backend:
            serviceName: apple-service
            servicePort: 5678
        - path: /banana
          backend:
            serviceName: banana-service
            servicePort: 5678

以下是服務列表

NAMESPACE       NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
nginx-ingress   apple-service          ClusterIP   10.111.230.109   <none>        5678/TCP                     95m
nginx-ingress   banana-service         ClusterIP   10.102.139.127   <none>        5678/TCP                     95m
nginx-ingress   nginx-ingress          NodePort    10.97.65.187     <none>        80:30207/TCP,443:31031/TCP   6d23h
[root@kube01 ingress]# kubectl get ing
NAME              HOSTS              ADDRESS   PORTS     AGE
example-ingress   *                            80        131m
[root@kube01 ingress]# kubectl describe ing example-ingress
Name:             example-ingress
Namespace:        default
Address:
Default backend:  default-http-backend:80 (<none>)
Rules:
  Host  Path  Backends
  ----  ----  --------
  *
        /apple    apple-service:5678 (10.244.2.7:5678)
        /banana   banana-service:5678 (10.244.1.11:5678)
Annotations:
  ingress.kubernetes.io/rewrite-target:  /
Events:                                  <none>

如果我按如下方式直接卷曲豆莢,它會按預期工作

[root@kube01 ingress]# curl http://10.244.2.7:5678/apple
apple
[root@kube01 ingress]# curl http://10.244.1.11:5678/banana
banana
[root@kube01 ingress]#

但是,如果我嘗試通過 Nodeport 入口控制器訪問它,我總是找不到如下

[root@kube01 ingress]# curl http://xx.xx.xx.193:30207/apple
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.2</center>
</body>
</html>
[root@kube01 ingress]# curl http://xx.xx.xx.193:30207/banana
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.2</center>
</body>
</html>
[root@kube01 ingress]#

curl http://xx.xx.xx.193:30207/apple curl http://xx.xx.xx.193:30207/banana從外部或瀏覽器訪問時,上述語句應分別顯示apple和banana

更新:我進入運行入口控制器的 pod 並檢查其配置文件,發現入口資源未應用於控制器。 我嘗試重新啟動容器但沒有運氣。 我也嘗試重新應用入口資源,但 pod 中的配置文件沒有改變。 進入控制器的所有內容都在配置文件中的位置標記中拋出 404。 任何有關資源未應用於控制器的幫助表示贊賞

root@nginx-ingress-685d7964cd-djvgf:/etc/nginx# cat nginx.conf

user  nginx;
worker_processes  auto;
daemon off;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


    access_log  /var/log/nginx/access.log  main;


    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout 65s;
    keepalive_requests 100;

    #gzip  on;

    server_names_hash_max_size 512;


    variables_hash_bucket_size 256;
    variables_hash_max_size 1024;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }





    server {
        listen 80 default_server;
        listen 443 ssl default_server;

        ssl_certificate /etc/nginx/secrets/default;
        ssl_certificate_key /etc/nginx/secrets/default;

        server_name _;
        server_tokens "on";
        access_log off;



        location / {
           return 404;
        }
    }
    # stub_status
    server {
        listen 8080;

        allow 127.0.0.1;
        deny all;
        location /stub_status {
            stub_status;
        }
    }

    include /etc/nginx/config-version.conf;
    include /etc/nginx/conf.d/*.conf;

    server {
        listen unix:/var/run/nginx-502-server.sock;
        access_log off;

        location / {
            return 502;
        }
    }
}

stream {
    log_format  stream-main  '$remote_addr [$time_local] '
                      '$protocol $status $bytes_sent $bytes_received '
                      '$session_time';

    access_log  /var/log/nginx/stream-access.log  stream-main;


}
root@nginx-ingress-685d7964cd-djvgf:/etc/nginx#

我的評論很少(我已經注意到):

1 ,您的入口正在默認名稱空間中工作:

  Namespace:        default
  instead of nginx-ingress namespace

2 ,在您的kubectl describe ing example-ingress中,沒有像這樣的注釋:

kubernetes.io/ingress.class:  nginx

3 ,我遇到了同樣的問題,但是我無法到達服務,但是當您使用ssl-redirect: "false"選項更改入口控制器的configmap時,它將起作用。 然后它將在入口資源中不帶spec.rules.host parameter情況下工作。

希望有幫助

我在我的情況下修復了它。

我的入口看起來像這樣( ingress.yaml ):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: app-ingress
  namespace: sample
spec:
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web-service
                port:
                  number: 80

我為路徑添加了一個通配符,如下:

          - path: /*

並重新應用此入口資源: kubectl apply -f ingress.yaml

然后它起作用了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM