Why GKE Ingress controller gives 404 error

后端 未结 1 1226
别那么骄傲
别那么骄傲 2021-01-15 21:20

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-en

相关标签:
1条回答
  • 2021-01-15 21:48

    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.

    0 讨论(0)
提交回复
热议问题