Kubernetes ingress rules for external service

一个人想着一个人 提交于 2020-05-17 05:49:44

问题


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 services and an external service. The Url for the external service should be something like http://host_name:80/es. When the user hits this url, this should be redirected to the external service.

The service definition and the ingress rule are configured as below but it leads to 404. Where am i going wrong?

Ingress rules

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”
spec:
  rules:
  - host:
    http:
      paths:
      - backend:
          serviceName: external-ip
          servicePort: 80
        path: /es

Service and End Point definitions

apiVersion: v1
kind: Service
metadata:
  name: external-ip
spec:
  ports:
  - name: app
    port: 80
    protocol: TCP
    targetPort: 80
---
apiVersion: v1
kind: Endpoints
metadata:
  name: external-ip
subsets:
- addresses:
  - ip: <ip to external service>
  ports:
  - name: app
    port: 80
    protocol: TCP

It works when i try with the URL http://host_name:80 and the following ingress rule. Please note the difference in the path in the ingress rule.

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”
    spec:
      rules:
      - host:
        http:
          paths:
          - backend:
              serviceName: external-ip
              servicePort: 80
            path: /

回答1:


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.



来源:https://stackoverflow.com/questions/61750709/kubernetes-ingress-rules-for-external-service

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