为什么GKE Ingress控制器会显示404错误 [英] Why GKE Ingress controller gives 404 error

查看:209
本文介绍了为什么GKE Ingress控制器会显示404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们为Ingress提供了以下代码,并且"/demo"应用与REST API的运行正常,并获得了呼叫响应.但是,"/um"没有打开,并且显示404错误. UM是内置于Angular 6中的前端应用程序,它应打开一个索引页.

We have a below code for Ingress and "/demo" app is running fine with REST API Get call response. However "/um" is not opening and its giving 404 error. UM is a front-end app built in Angular 6 and it should open an index page.

当我们将此应用程序公开为外部IP时,即键入:LoadBalancer,则该应用程序可以正常工作.在Ingress设置中尝试时,也会遇到404错误.

When we expose this application as a External IP i.e. Type:LoadBalancer, then application is working fine. The same is encountering 404 when try from Ingress setup.

不知道是什么引起了这个问题.以下是我们的示例Ingress部署文件.请通过一些见解来解决此问题.

Not sure what commits this issue. The below is our sample Ingress deployment file. Kindly through some insights to fix the issue.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myingress
spec:
  rules:
   - http:
      paths:
      - path: /um
        backend:
          serviceName: usermanager-frontend
          servicePort: 8973
      - path: /demoapp
        backend:
          serviceName: springboot-demo
          servicePort: 7070

在进行重新编写之前,我们已经阅读了一些帖子,因此已删除,因为今天GKE不支持此操作.

Before we where having re-write which we have removed after reading some post as this is not supported in GKE today.

metadata:
  name: usermanagement-ui
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /um

推荐答案

YAML文件我的意思是部署和服务配置.您始终可以使用这些信息来编辑您的问题.

YAML file I meaning Deployment and Service configuration. You can always edit your question with those information.

在GKE上,您可以运行两种类型的入口.

On GKE you can run 2 types of ingress.

Nginx入口控制器

GCP入口控制器

如果要使用GCP入口,则您的服务必须为NodePort类型.

If you want to use GCP Ingress, your Services must be NodePort type.

在服务清单中,请注意类型为NodePort.这是用于配置HTTP(S)负载平衡器的Ingress的必需类型.可以在此处找到更多信息.

如果您想使用Nginx Controller,则需要对其进行部署(最佳实践是使用HELM).您需要使用Ingress中的特殊注释来强制执行此操作,例如: annotations: kubernetes.io/ingress.class: nginx

If you would like to use Nginx Controller you will need to deploy it (best practise is to use HELM). You need to enforce it using in special annotation in your Ingress like: annotations: kubernetes.io/ingress.class: nginx

我已经在此SO答案中提到了差异以及应如何设置差异.

I already mentioned about differences and how it should be set in this SO answer.

假设,在您提到的有关GKE入口的标题中,我将为您提供工作示例.为此,我将使用2个部署,2个svc(hellov1和hellov2)和Ingress.

Assuming, that in the title you mentioned about GKE ingress I will give you working example. To do that I will use 2 deployments, 2 svc (hellov1 and hellov2) and Ingress.

此外,我不确定这是否在复制/粘贴中出错,但是在您的YAML入口定义中缺少一些空格.看起来应该像这样:

In addition, I am not sure if this is mistake in copy/paste but in your YAML ingress definition are missing some spaces. It should looks like:

spec:
  rules:
    - http:
        paths:

YAML:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-v2
spec:
  selector:
    matchLabels:
      app: hello-v2
  replicas: 1
  template:
    metadata:
      labels:
        app: hello-v2
    spec:
      containers:
      - name: hellov2
        image: "gcr.io/google-samples/hello-app:2.0"
        ports:
        - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: hello-v2-svc
  labels: 
    app: hello-v2
spec:
  type: NodePort 
  selector:
    app: hello-v2
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP
---
apiVersion: apps/v1
kind:  Deployment
metadata:
  name: hello
  labels:
    app: hello
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
        - name: nginx
          image: gcr.io/google-samples/hello-app:1.0
          ports:
          - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: hello-svc
  labels: 
    app: hello
spec:
  type: NodePort 
  selector:
    app: hello
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP  
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myingress
spec:
  rules:
    - http:
        paths:
        - path: /hello
          backend:
            serviceName: hello-svc
            servicePort: 8080
        - path: /hello-v2
          backend:
            serviceName: hello-v2-svc
            servicePort: 8080

$ kubectl get po,svc,ing
NAME                            READY   STATUS    RESTARTS   AGE
pod/hello-59c5c6ff8d-vdk8d      1/1     Running   0          8m16s
pod/hello-v2-6875bf9bc4-gtzpz   1/1     Running   0          8m16s

NAME                   TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
service/hello-svc      NodePort    10.8.9.67    <none>        8080:30232/TCP   8m15s
service/hello-v2-svc   NodePort    10.8.3.15    <none>        8080:30501/TCP   8m16s
service/kubernetes     ClusterIP   10.8.0.1     <none>        443/TCP          7h

NAME                           HOSTS   ADDRESS         PORTS   AGE
ingress.extensions/myingress   *       34.120.142.85   80      8m15s

部署Ingress后,GKE大约需要5分钟才能开始正常工作.

user@cloudshell:~ (k8s-tests-XXX)$ curl 34.120.142.85/hello
Hello, world!
Version: 1.0.0
Hostname: hello-59c5c6ff8d-vdk8d
user@cloudshell:~ (k8s-tests-XXX)$ curl 34.120.142.85/hello-v2
Hello, world!
Version: 2.0.0
Hostname: hello-v2-6875bf9bc4-gtzpz

我鼓励您阅读GKE文档配置Ingress用于外部负载平衡.您可以在此处找到更多示例和说明,以了解如何使用多个后端.可能有用的本文.

I encourage you to read GKE docs Configuring Ingress for external load balancing. You can find there more example and explanation how to use mutiple backends. Als useful could be this article.

此配置仅适用于路径/hello/nginx,但是您始终可以检查默认后端选项.

This configuration will only apply to path /hello and /nginx, however you can always check default backend option.

如果仍然有404,请验证是否设置了正确的porttargetPortservicePort.

If you will still have 404, verify if you set proper port, targetPort and servicePort.

这篇关于为什么GKE Ingress控制器会显示404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆