如何使用特定端口定义一些 Kubernetes Ingress 路由

How to define some Kubernetes Ingress routes with specific ports

我正在尝试为我的 Kubernetes 集群创建一些入口规则(目前,在使用 Docker Desktop 的本地主机上)但它们不起作用。

我想做什么

- App #1 : Some database (e.g. mongodb or RavenDb or Postgres, etc).
- App #2 : Some queue (rabbitmq, etc)
- App #3 : Some web site api #1 
- App #4 : Some web site api #2

访问每个应用程序的路径

- App #1 : <anything>:port 5200
- App #2 : <anything>:port 5300
- App #3 : /account/*:80, /accounts:80
- App #4 : /order/*:80, /orders/*:80

[注意 -> 我还没有包括 ssl/443 端口,因为我还没有处理它,等等]

这是我为第一个应用程序(不起作用)得到的示例:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: data-ravendb-ingress
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: data-ravendb-service
          servicePort: dashboard

---
apiVersion: v1
kind: Service
metadata:
  name: data-ravendb-service
  labels:
    app: hornet
    tier: backend
    component: data-ravendb
spec:
  ports:
  - name: dashboard
    port: 5200
    targetPort: 8080
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: data-ravendb-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: hornet
        tier: backend
        component: data-ravendb
    spec:
      containers:
      - name: data-ravendb-container
        image: ravendb/ravendb
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
          name: dashboard

如何设置入口以允许这 4 个应用程序正确访问后端服务?

目前不完全支持在 Kubernetes Ingress 中路由 HTTP/HTTPS 协议以外的任何其他流量。您使用的 Traefik 也是 HTTP 反向代理,因此使用入口控制器很难或不可能做到这一点。

参见

如@Jakub 所述,入口仅支持 http/https 个端口。

您可以为您的网站 API 以及 App1 和 App2 创建具有不同后端路径的入口。但是如果你需要暴露端口,那么考虑使用 LoadBalancer 服务。

根据您的第一个应用示例,为什么它不起作用,因为您没有在服务 yaml 中指定 selectors。尝试像下面这样更改它,然后它会起作用。

apiVersion: v1
kind: Service
metadata:
  name: data-ravendb-service
  labels:
    app: hornet
    tier: backend
    component: data-ravendb
spec:
  selector: # Mandatory to find backends
    app: hornet # Should match to pod label
    tier: backend # Should match to pod label
  ports:
  - name: dashboard
    port: 5200
    targetPort: 8080

希望对您有所帮助!