在 kubernetes 中统一服务端点

Unify service endpoints in kubernetes

是否可以在 kubernetes 中 "unify" 单个服务的端点 运行?例如我有两个服务:

  1. 用户
  2. Post

两种服务都使用它自己的端口公开。例如:http://localhost:30888/api/userhttp://localhost:30884/api/post.

现在我想要一个这样的界面:

http://localhost:8080/api/userhttp://localhost:8080/api/post。我该如何配置?

服务是使用这个公开的:

apiVersion: v1
kind: Service
metadata:
  name: post-service
  labels:
    app: post-service
spec:
  type: NodePort
  ports:
    - port: 44884
      nodePort: 30884
  selector:
    app: post

实际上,使用 Kubernetes 默认资源是不可能的:您必须使用像 Istio, Maesh or Linkerd 这样的可用服务网格,或者根据您喜欢的风格使用简单的代理(NGINX、HAproxy 或使用简单的 GoLang 应用程序) http/net 标准库)。

我不是 Istio 的专家(它实际上是可用的最常用的服务网格之一)但可能 VirtualService 可以解决问题,以及 DestinationRule资源。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews-route
spec:
  hosts:
  - you-service.your-namespace.svc.cluster.local
  http:
  - name: user-v1-routes
    match:
    - uri:
        prefix: "/api/user"
    route:
    - destination:
        host: user-service.your-namespace.svc.cluster.local
        subset: v1
  - name: post-v1-route
    route:
    - destination:
        host: post-service.your-namespace.svc.cluster.local
        subset: v1
    match:
    - uri:
        prefix: "/api/post"
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: user-destination
spec:
  host: user-service.yout-namespace.svc.cluster.local
  subsets:
  - name: v1
    labels:
      version: v1
      app: user
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: post-destination
spec:
  host: post-service.yout-namespace.svc.cluster.local
  subsets:
  - name: v1
    labels:
      version: v1
      app: post

YMMV 和 YAML 只是解释性的。

听起来您想要在您的服务之上添加一个 Ingress。入口可以做很多事情,包括根据路径将流量发送到不同的服务。

让您入门的 Ingress 可能类似于:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test-ingress
spec:
  rules:
  - http:
      paths:
      - path: /api/user
        backend:
          serviceName: post-service
          servicePort: 44884
      - path: /api/post
        backend:
          serviceName: user-service
          servicePort: 33883 # Or whatever port your user-service is using

在幕后,使 Ingress 工作的因素因您 运行 Kubernetes 集群的方式而异。例如,如果您使用的是 GKE,Ingress 实际上是作为 HTTP load balancer. If you're using EKS, it's implemented as an Application load balancer 实现的。每个负载均衡器都有很多功能和选项,但在开始任何事情之前您应该了解的最重要的一点是,它们会在 运行 您的集群之上花费额外的钱。

编辑:

正如评论中提到的 prometherion,入口允许来自集群外部的流量。如果您 运行 是某种类型的公开网站,这可能就是您想要的。但是,如果这些是不应向外界公开的内部端点,您可能需要调查