kubernetes 我想要1 nginx入口资源为我的所有10个服务

jc3wubiy  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(1)|浏览(80)

我在node中有10个服务,每个服务都有多个get和post请求。他们在GCP应用程序引擎中运行良好。yaml

runtime: nodejs14
service: feedservice
instance_class: F4
automatic_scaling:
  max_instances: 7

runtime: nodejs14
service: walletservice
instance_class: F4
automatic_scaling:
  max_instances: 4

字符串
现在我已经把它们部署到aws eks了。我已经用Nginx ingress控制器暴露了它们,这是我的ingress.yaml

apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: my-ingress
      annotations:
        kubernetes.io/ingress.class: nginx
    spec:
      ingressClassName: nginx
      rules:
        - host: myapp.example.com
          http:
            paths:
              - path: /feedservice
                pathType: Prefix
                backend:
                  service:
                    name: feedservice
                    port:
                      number: 8081
              - path: /walletservice
                pathType: Prefix
                backend:
                  service:
                    name: walletservice
                    port:
                      number: 8081
.
.
and so on


问题是appengine会自动将servicename放在基url中,然后我可以像这样获取get和post请求:https://myappengineurl/servicename/getrequest1,https://myappengineurl/servicename/getrequest2,https://myappengineurl/servicename2/getrequest1等等。
我已经像这样公开了我的节点服务:

app.get('/getrequest1', jsonParser, async (request, response) => {
    //my code
     )}
    app.get('/getrequest2', jsonParser, async (request, response) => {
    //my code
     )}

.....so on
    
    
        const port = process.env.PORT || 8081;
        app.listen(port, () => {
            console.log(`helloworld: listening on port ${port}`);
        });


但我无法访问这些服务,出现此错误:Cannot GET /feedservice,when I can change ingress to this:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress-feedservice
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  ingressClassName: nginx
  rules:
    - host: myapp.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: feedservice
                port:
                  number: 8081


和/或

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress-walletservice
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  ingressClassName: nginx
  rules:
    - host: myapp.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: walletservice
                port:
                  number: 8081


只有1服务将工作它说/已用于默认路径,我不想改变主机名。我也希望我所有的10个服务1入口。非常感谢你的帮助

7lrncoxx

7lrncoxx1#

在您的情况下,您必须添加此注解nginx.ingress.kubernetes.io/rewrite-target,它可以帮助您“Target URI where the traffic must be redirected”。Source
所以你的新yaml文件应该看起来像这样:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /    
spec:
  ingressClassName: nginx
  rules:
    - host: myapp.example.com
      http:
        paths:
          - path: /feedservice
            pathType: Prefix
            backend:
              service:
                name: feedservice
                port:
                  number: 8081
          - path: /walletservice
            pathType: Prefix
            backend:
              service:
                name: walletservice
                port:
                  number: 8081

字符串
而且最好在不同的端口号上有多个服务,它更适合重定向。

相关问题