Kubernetes 根据 url 中的参数路由到特定 pod

Kubernetes routing to specific pod in function of a param in url

我的需求看起来像这样的东西:

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: http
spec:
  serviceName: "nginx-set"
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: gcr.io/google_containers/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: http
----
apiVersion: v1
kind: Service
metadata:
  name: nginx-set
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: http
  clusterIP: None
  selector:
    app: nginx

这是有趣的部分:

apiVersion: voyager.appscode.com/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: default
spec:
  rules:
  - host: appscode.example.com
    http:
      paths:
      - path: '/testPath'
        backend:
          hostNames:
          - web-0
          serviceName: nginx-set #! There is no extra service. This
          servicePort: '80'      # is the Statefulset's Headless Service

由于在 url.

函数中设置了主机名,因此我能够定位到特定的 pod

现在我想知道是否可以在 kubernetes 中创建这样的规则

apiVersion: voyager.appscode.com/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: default
spec:
  rules:
  - host: appscode.example.com
    http:
      paths:
      - path: '/connect/(\d+)'
        backend:
          hostNames:
          - web-(result of regex match with \d+)
          serviceName: nginx-set #! There is no extra service. This
          servicePort: '80'      # is the Statefulset's Headless Service

或者如果我必须为每个 pod 编写规则?

抱歉,这不可能,最好的解决方案是创建多个路径,每个路径引用一个 pod:

apiVersion: voyager.appscode.com/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: default
spec:
  rules:
  - host: appscode.example.com
    http:
      paths:
      - path: '/connect/0'
        backend:
          hostNames:
          - web-0
          serviceName: nginx-set
          servicePort: '80'
      - path: '/connect/1'
        backend:
          hostNames:
          - web-1
          serviceName: nginx-set
          servicePort: '80'