简体   繁体   English

Kubernetes 外部服务入口规则

[英]Kubernetes ingress rules for external service

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 .外部服务的 Url 应该类似于http://host_name:80/es When the user hits this url, this should be redirected to the external service.当用户点击此 url 时,应将其重定向到外部服务。

The service definition and the ingress rule are configured as below but it leads to 404. Where am i going wrong?服务定义和入口规则配置如下,但它导致 404。我哪里出错了?

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.当我尝试使用 URL http://host_name:80和以下入口规则时,它可以工作。 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: /

There is a service that can echo my request back to me: https://postman-echo.com/ , it will come useful later.有一项服务可以将我的请求回显给我: https://postman-echo.com/ ,稍后会派上用场。 Here is its ip and it will simulate your external service:这是它的 ip 它将模拟您的外部服务:

$ 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.如您所见,它向我发送了一个 json,其中包含我发送给它的所有标头,最重要的是 - url 及其接收的路径。

Here is the ingress yaml I used:这是我使用的入口 yaml:

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.服务和端点定义与您的相同,但端点 IP 除外。 Here I used 107.23.20.188 (the postman-echo IP).这里我使用了 107.23.20.188(postman-echo IP)。

Now lets try to send some requests through nginx but first lets check whats ingress ip:现在让我们尝试通过 nginx 发送一些请求,但首先让我们检查一下入口 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. ip 是192.168.39.96和它的私有 IP 因为我在 minikube 上运行它,但这应该没关系。

$ 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 .如您所见,我正在发送对路径/es/get的请求,而 echo 服务器正在接收/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.在写这个答案时我注意到的一件事是(可能只是复制粘贴错误,但是)注释中的引号不同于" ,这可能导致 nginx 没有按应有的方式处理注释。我的一些情况当我复制粘贴您的 yaml 时它的原因是它正在工作,但它没有您的注释,所以这可能是我之前没有注意到它的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM