Why GKE Ingress controller gives 404 error

爱⌒轻易说出口 提交于 2020-06-17 09:42:34

问题


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.

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.

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

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

回答1:


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

On GKE you can run 2 types of ingress.

Nginx Ingress Controller

GCP Ingress Controller

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

In the Service manifest, notice that the type is NodePort. This is the required type for an Ingress that is used to configure an HTTP(S) load balancer. More detailed information can be found here.

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

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

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.

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

After you deploy Ingress, GKE need about 5 minutes before it will start working properly.

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

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.

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

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



来源:https://stackoverflow.com/questions/62296942/why-gke-ingress-controller-gives-404-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!