如何在Kubernetes中更改请求路径

时间:2019-06-26 07:09:22

标签: kubernetes kubernetes-ingress

下面是我的Nodejs微服务模式:


// api.ts
import { Router } from 'express';
const router = Router();
router.get(':id', ...doSomething);
router.post(':id', ...doSomething);
export default router;

// index.ts
import * as Express from 'express';
import API from './api.js';
basePath = process.env.basePath; // usually is project name
const app = Express();

// handle external call
app.use(basePath, API) // wish to remove this line 
// handle internal call from microservices
app.use(API) // preferred to be like this
...continue

下面是我从同事那里继承的kubeDeploy文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: $CI_PROJECT_NAME
  namespace: $KUBE_NAMESPACE
spec:
  replicas: 1
  selector:
    matchLabels:
      app: $CI_PROJECT_NAME
  template:
    metadata:
      labels:
        app: $CI_PROJECT_NAME
    spec:
      imagePullSecrets:
      - name: gitlabcred
      containers:
      - image: registry.gitlab.com/$GROUP_NAME/$CI_PROJECT_NAME:$CI_BUILD_REF_NAME
        imagePullPolicy: Always
        name: $CI_PROJECT_NAME
        ports:
        - containerPort: $PORT
        env:
          - name: basePath
            value: "$URL_PATH"
        resources: $KUBE_RESOURCES
        livenessProbe: $KUBE_LIVENESS
---
apiVersion: v1
kind: Service
metadata:
  name: $CI_PROJECT_NAME
  namespace: $KUBE_NAMESPACE
spec:
  ports:
  - port: $PORT
    protocol: TCP
    name: http
  selector:
    app: $CI_PROJECT_NAME
  sessionAffinity: ClientIP
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: $CI_PROJECT_NAME
  namespace: $KUBE_NAMESPACE
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: $KUBE_DNS_NAME
    http:
      paths:
      - path: /$URL_PATH
        backend:
          serviceName: $CI_PROJECT_NAME
          servicePort: $PORT
  - http:
      paths:
      - backend:
          serviceName: $CI_PROJECT_NAME
          servicePort: $PORT

上面的代码和设置在内部和外部呼叫中都可以正常工作,如下所示:

http://publicUrl.com/projectA/someId //外部调用,微服务将request.path接收为“ / projectA / someId”

http://publicUrl.com/projectB/someId //外部调用,微服务将request.path接收为“ / projectB / someId”

http://projectA/someId //内部调用,微服务将request.path接收为“ / someId”

http://projectB/someId //内部调用,微服务将request.path接收为“ / someId”

我希望从我的微服务中删除“ app.use(basePath,API)”,使其与环境无关。

无论如何,我是否可以更改kubeDeploy以使其从外部调用中在微服务内部收到的路径更改为“ / someId”?

更新:以下是由devops更新的最新kubeDeploy

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: $CI_PROJECT_NAME
  namespace: $KUBE_NAMESPACE
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - host: $KUBE_DNS_NAME
    http:
      paths:
      - path: /$URL_PATH(/|$)(.*)
        backend:
          serviceName: $CI_PROJECT_NAME
          servicePort: $PORT
  - http:
      paths:
      - backend:
          serviceName: $CI_PROJECT_NAME
          servicePort: $PORT

我在上面尝试过,但是我不知道它会变成从浏览器端重定向。 示例:我在浏览器中打开http://publicUrl.com/projectA/help时,由于Ingress无法找到匹配路径,因此在浏览器地址栏上,URL转向变成了http://publicUrl.com/help,显示“默认后端-404”。

1 个答案:

答案 0 :(得分:1)

您可以使用Flexbox,但请记住,这些是nginx控制器的自定义扩展,不能移植到所有其他实现中。

从他们的示例:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something(/|$)(.*)