Kubernetes ingress rules for external service

后端 未结 1 1375
说谎
说谎 2021-01-26 23:43

This question is similar to the question but this is more around the path in the rule that can be configured.

The ingress should be able to handle both the internal serv

相关标签:
1条回答
  • 2021-01-27 00:35

    There is a service that can echo my request back to me: https://postman-echo.com/, it will come useful later. Here is its ip and it will simulate your external service:

    $ dig postman-echo.com +short
    107.23.20.188
    

    It works as following:

    $ curl 107.23.20.188/get | jq
    {
      "args": {},
      "headers": {
        "x-forwarded-proto": "http",
        "x-forwarded-port": "80",
        "host": "107.23.20.188",
        "x-amzn-trace-id": "Root=1-5ebced9c-941e363cc28bf3529b8e7246",
        "user-agent": "curl/7.52.1",
        "accept": "*/*"
      },
      "url": "http://107.23.20.188/get"
    }
    

    So as you can see it sends me a json with all headers that I sent to it and most importantly - url with path it receives.

    Here is the ingress yaml I used:

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: external-service
      annotations:
        #kubernetes.io/ingress.class: "nginx"
        nginx.ingress.kubernetes.io/ingress.class: "nginx"
        nginx.ingress.kubernetes.io/ssl-redirect: "false"
        nginx.ingress.kubernetes.io/rewrite-target: /$1
    spec:
      rules:
      - host:
        http:
          paths:
          - backend:
              serviceName: external-ip
              servicePort: 80
            path: /es/(.*)
    

    Service and Endpoint definition stays the same as yours with exception for endpoint IP. Here I used 107.23.20.188 (the postman-echo IP).

    Now lets try to send some requests through nginx but first lets check whats ingress ip:

    $ kubectl get ingress
    NAME               HOSTS   ADDRESS         PORTS   AGE
    external-service   *       192.168.39.96   80      20h
    

    The ip is 192.168.39.96 and its private IP because I am running it on minikube but it should not matter.

    $ curl -s 192.168.39.96/es/get
    {
      "args": {},
      "headers": {
        "x-forwarded-proto": "http",
        "x-forwarded-port": "80",
        "host": "192.168.39.96",
        "x-amzn-trace-id": "Root=1-5ebcf259-6331e8c709656623f1a94ed4",
        "x-request-id": "d1545d1e8764da3cf57abb143faac4fb",
        "x-forwarded-host": "192.168.39.96",
        "x-original-uri": "/es/get",
        "x-scheme": "http",
        "user-agent": "curl/7.52.1",
        "accept": "*/*"
      },
      "url": "http://192.168.39.96/get"
    }
    

    so as you see I am sending request for path /es/get and echo server is receiving /get.


    One thing I have noticed while writing this answer is that (maybe its just copy-paste error but) your quotes in annotations are different than " and this may be causing that nginx is not processing annotations as it should. Im my case for some reason when I was copy-pasting your yaml it it was working but so it did without your annotations so that may be the reason I haven't noticed it earlier.

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