Kubernetes基于入口路径的路由无法按预期工作

时间:2020-03-15 10:33:33

标签: spring-boot kubernetes kubernetes-ingress nginx-ingress

我在kubernetes集群中安装了NGINX Ingress。当我尝试通过Ingress Controller访问微服务端时,它无法按预期运行

我已经部署了两个spring boot应用程序

进入规则

  • 路径1-> / customer
  • 路径2-> / prac

当我尝试访问其中一项服务时。 http://test.practice.com/prac/practice/getprac ,它不起作用

enter image description here

但是当我尝试在没有入口路径 http://test.practice.com/practice/getprac 的情况下访问时,

enter image description here

我无法理解为什么Ingress路径无法正常工作,而其他服务也是如此

微服务1(端口9090)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: customer
  namespace: practice
  labels: 
    app: customer
spec:
  replicas: 5
  selector:
    matchLabels: 
      app: customer
  template: 
    metadata:
      labels: 
        app: customer
    spec: 
      imagePullSecrets:
      - name: testkuldeepsecret
      containers:
      - name: customer
        image: kuldeep99/customer:v1
        ports:
        - containerPort: 9090
          hostPort: 9090

---
apiVersion: v1
kind: Service
metadata:                     
  name: customer-service
  namespace: practice
  labels: 
spec:                         
  ports: 
  - port: 9090 
    targetPort: 9090
    protocol: TCP
    name: http
  selector:
    app: customer

微服务2(端口8000)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prac
  namespace: practice
  labels: 
    app: prac
spec:
  replicas: 4
  selector:
    matchLabels: 
      app: prac
  template: 
    metadata:
      labels: 
        app: prac
    spec: 
      imagePullSecrets:
      - name: testkuldeepsecret
      containers:
      - name: prac
        image: kuldeep99/practice:v1
        ports:
        - containerPort: 8000
          hostPort: 8000

---
apiVersion: v1
kind: Service
metadata:                     
  name: prac-service
  namespace: practice
  labels: 
spec:                         
  ports: 
  - port: 8000 
    targetPort: 8000
    protocol: TCP
    name: http
  selector:
    app: prac

服务(客户服务和实践服务)

NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
customer-service   ClusterIP   10.97.203.19    <none>        9090/TCP   39m
ngtest             ClusterIP   10.98.74.149    <none>        80/TCP     21h
prac-service       ClusterIP   10.96.164.210   <none>        8000/TCP   15m
some-mysql         ClusterIP   None            <none>        3306/TCP   2d16h

入口

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: practice-ingress
  namespace: practice
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec: 
  rules:
  - host: practice.example.com
    http: 
      paths:
      - backend:
          serviceName: customer-service
          servicePort: 9090
        path: /customer
      - backend:
          serviceName: prac-service
          servicePort: 8000
        path: /prac

3 个答案:

答案 0 :(得分:3)

您已经安装了此nginx ingress

nginx.ingress.kubernetes.io/rewrite-target: /注释才能正常工作,您需要安装此nginx ingress

解决此问题的另一种方法是在spring应用程序中将contextPath配置为/prac

答案 1 :(得分:1)

我花了一天的时间来解决这个问题。问题只是安装了错误的 nginx。我使用 helm found here 来安装 nginx-ingress

安装它,请使用 helm 版本 3:

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update

helm install ingress-nginx ingress-nginx/ingress-nginx

一旦运行,您将在日志中看到一个片段,说明您的入口应该是什么样子。如果你想做上面的,你可以按照上面建议的注解,以后你可以按照教程here来实现更多,比如重写。

我的集群使用 GKE 部署在 GCP 上

完成后,这是输出日志:

NAME: ingress-nginx
LAST DEPLOYED: Sat Apr 24 07:56:11 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
The ingress-nginx controller has been installed.
It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status by running 'kubectl --namespace default get services -o wide -w ingress-nginx-controller'

An example Ingress that makes use of the controller:

  apiVersion: networking.k8s.io/v1beta1
  kind: Ingress
  metadata:
    annotations:
      kubernetes.io/ingress.class: nginx
    name: example
    namespace: foo
  spec:
    rules:
      - host: www.example.com
        http:
          paths:
            - backend:
                serviceName: exampleService
                servicePort: 80
              path: /
    # This section is only required if TLS is to be enabled for the Ingress
    tls:
        - hosts:
            - www.example.com
          secretName: example-tls

If TLS is enabled for the Ingress, a Secret containing the certificate and key must also be provided:

  apiVersion: v1
  kind: Secret
  metadata:
    name: example-tls
    namespace: foo
  data:
    tls.crt: <base64 encoded cert>
    tls.key: <base64 encoded key>
  type: kubernetes.io/tls

这是安装后的样子:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2

  name: example
  # namespace: foo
spec:
  rules:
    - host: [your ip address].sslip.io
      http:
        paths:
          - backend:
              serviceName: registry-app-server
              servicePort: 8761
            path: /eureka/(.*)
          - backend:
              serviceName: api-gateway-server
              servicePort: 7000
            path: /api(/|$)(.*)

如您所见,我正在使用 kubernetes(gke) 部署 Spring 微服务。 使用nginx-ingress相比内置gke ingress有很多好处,而且比its counterparts

更受欢迎

答案 2 :(得分:0)

在讨论中,我观察到一件事。我们不应该混淆

apiVersion: networking.k8s.io/v1
kind: Ingress

apiVersion: networking.k8s.io/v1beta1
kind: Ingress

首先确保我们使用的是哪个 Ingress 控制器,并根据该控制器决定 apiVersion。我正在使用“ingress-nginx”(不是“nginx-ingress”)。这个支持“apiVersion:networking.k8s.io/v1beta1”,并且按照“Arsene”的评论工作。

此 Ingress yaml 文件可与“ingress-nginx”Ingress 控制器配合使用

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2

  name: k8-exercise-03-two-app-ingress
spec:
  rules:
    - host: ex03.k8.sb.two.app.ingress.com
      http:
        paths:
          - backend:
              serviceName: k8-excercise-01-app-service
              servicePort: 8080
            path: /one(/|$)(.*)
          - backend:
              serviceName: k8-exercise-03-ms-service
              servicePort: 8081
            path: /two(/|$)(.*)

但是,这个 Ingress yaml 文件不适用于“ingress-nginx”Ingress 控制器

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
    name: k8-exercise-03-two-app-ingress
    annotations:
       kubernetes.io/ingress.class: "nginx"
#       nginx.ingress.kubernetes.io/use-regex: "true"
       ingress.kubernetes.io/rewrite-target: /$2
spec:
#  ingressClassName: nginx
  rules:
 #192.168.1.5 ex03.k8.sb.com is mapped in host file. 192.168.1.5 is Host machine IP
  - host: ex03.k8.sb.two.app.ingress.com
    http:
        paths:
        - backend:
            service:
                name: k8-excercise-01-app-service
                port:
                    number: 8080  
          path: /one(/|$)(.*)
          pathType: Prefix
          
        - pathType: Prefix
          path: /two(/|$)(.*)
          backend:
            service:
                name: k8-exercise-03-ms-service
                port:
                    number: 8081

我可以像这样访问 Spring Boot API 调用:

对于 App-1:

http://ex03.k8.sb.two.app.ingress.com/one/
Result: App One - Root

http://ex03.k8.sb.two.app.ingress.com/one/one
Result: App One - One API

http://ex03.k8.sb.two.app.ingress.com/one/api/v1/hello
Result: App One - Hello API

应用程序 2:

http://ex03.k8.sb.two.app.ingress.com/two/message/James%20Bond
Result: App Two- Hi James Bond API

最后,如果有人知道如何更改“apiVersion:networking.k8s.io/v1”yaml 以支持“ingress-nginx”控制器,将不胜感激。谢谢你。抱歉内容太长