用入口nginx重写问题

时间:2019-04-05 07:26:15

标签: kubernetes

我是新手,并尝试使用minikube和ingress-nginx在本地使用kubernetes部署整个应用程序。面对以下重写问题

下面是我的ingress-service.yml配置文件

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /  
spec:
  rules:
    - http:
        paths:
          - path: /
            backend:
              serviceName: client-cluster-ip-service
              servicePort: 3000
          - path: /api/
            backend:
              serviceName: server-cluster-ip-service
              servicePort: 5000

我的快速路由代码是

app.get('/', (req, res) => {
  res.send('index');
});

app.get('/values/all', async (req, res) => {
  res.send('all values');
});

app.get('/values/current', async (req, res) => {
  res.send('current values');
});

通过我的所有路线的react应用程序,它只会进入索引路线。

我需要的是,当我的React应用程序调用路由“ / api / values / all”时,我应该从相应的路由中获取响应,即

app.get('/values/all', async (req, res) => {
  res.send('all values');
});

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您的rewrite-target一词似乎是问题所在。看看rewrite-target上的文档。尝试创建2个入口资源:

Front.yaml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /  
spec:
  rules:
    - http:
        paths:
          - path: /*
            backend:
              serviceName: client-cluster-ip-service
              servicePort: 3000

Back.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /api  
spec:
  rules:
    - http:
        paths:
          - path: /api/*
            backend:
              serviceName: server-cluster-ip-service
              servicePort: 5000