Kubernetes外部服务的入口规则 [英] Kubernetes ingress rules for external service

查看:135
本文介绍了Kubernetes外部服务的入口规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题类似于问题,但这更多地涉及了可以配置的规则.

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

入口应能够处理内部服务和外部服务.外部服务的网址应类似于 http://host_name:80/es .当用户点击此网址时,应将其重定向到外部服务.

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.

服务定义和入口规则配置如下,但导致404. 我要去哪里错了?

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

进入规则

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

服务和端点定义

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

当我尝试使用URL http://host_name:80 和以下入口规则时,它可以工作.请注意入口规则中路径的不同.

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: /

推荐答案

有一项服务可以将我的请求回显给我:

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

它的工作原理如下:

$ 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"
}

因此,如您所见,它向我发送了一个json,其中包含我发送给它的所有标头,最重要的是-包含接收到的路径的url.

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.

这是我使用的入口Yaml:

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/(.*)

服务和端点的定义与您相同,端点IP除外.在这里,我使用了107.23.20.188(邮递员回声IP).

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).

现在让我们尝试通过nginx发送一些请求,但首先让我们检查什么是入口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

该IP是192.168.39.96及其专用IP,因为我在minikube上运行它,但这没关系.

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"
}

如您所见,我正在发送路径/es/get的请求,而回显服务器正在接收/get.

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

我在写此答案时注意到的一件事是(也许只是复制粘贴错误,但是)注释中的引号与"不同,这可能导致nginx不在处理注释应该.在我的情况下,由于某种原因,当我复制粘贴您的yaml时,它可以正常工作,但是没有您的注释就可以了,所以这可能就是我较早没有注意到它的原因.

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.

这篇关于Kubernetes外部服务的入口规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆