Spring boot 基于Kubernetes入口路径的路由未按预期工作
我安装在kubernetes集群中。当我试图通过入口控制器访问微服务端时,它没有按预期工作 我已经部署了两个spring boot应用程序 入口规则 路径1->/customer 路径2->/prac 当我试图访问某个服务时,例如。 ,它不起作用 但当我尝试在没有入口路径的情况下访问时,它是有效的 我无法理解为什么入口路径不起作用,而其他服务也是如此 微服务1端口9090Spring boot 基于Kubernetes入口路径的路由未按预期工作,spring-boot,kubernetes,kubernetes-ingress,nginx-ingress,Spring Boot,Kubernetes,Kubernetes Ingress,Nginx Ingress,我安装在kubernetes集群中。当我试图通过入口控制器访问微服务端时,它没有按预期工作 我已经部署了两个spring boot应用程序 入口规则 路径1->/customer 路径2->/prac 当我试图访问某个服务时,例如。 ,它不起作用 但当我尝试在没有入口路径的情况下访问时,它是有效的 我无法理解为什么入口路径不起作用,而其他服务也是如此 微服务1端口9090 apiVersion: apps/v1 kind: Deployment metadata: name: custome
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
服务客户服务和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
你已经安装了这个
nginx.ingres.kubernetes.io/rewrite-target:/annotation要正常工作,您需要安装此
解决此问题的另一种方法是在spring应用程序中将contextPath配置为/prac我花了一天时间来解决此问题。问题只是安装了错误的nginx。我使用helm found安装nginx入口 安装它,请使用helm版本3:
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install ingress-nginx ingress-nginx/ingress-nginx
一旦运行,您将在日志中看到一个片段,说明您的入口应该是什么样子。如果您想执行上述操作,您可以使用上面建议的注释,此后,您可以按照教程完成更多操作,如重写
我的集群使用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
如您所见,我正在使用kubernetesgke部署spring微服务。
与内置的gke入口相比,使用nginx入口有很多好处,而且它比更受欢迎。在讨论中,我注意到一件事。我们不应该把它们混为一谈
apiVersion: networking.k8s.io/v1
kind: Ingress
及
首先确保我们使用的入口控制器以及基于该版本的入口控制器。我使用的是nginx而不是nginx。这一个支持apiVersion:networking.k8s.io/v1beta1,并根据Arsene的评论工作
此Ingress yaml文件与Ingress nginx Ingress controller配合使用
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(/|$)(.*)
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
但是,此Ingress yaml文件不适用于Ingress nginx Ingress controller
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(/|$)(.*)
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引导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
App-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控制器,将不胜感激。非常感谢。抱歉,内容太长了删除注释后,其行为也相同。因此,spring boot应用程序具有/practice/getprac的rest控制器?请在spring boot应用程序中将contextPath设置为/prac。在它正常工作的情况下,我没有收到任何指定上下文路径应与入口路径匹配的示例。想了解重写目标注释您安装了nginx plus Ingres而不是kubernetes nginx Ingres…这就是注释不工作的原因nginx.ingress.kubernetes.io/rewrite-target:/$2解决了我的问题,在配置之前,我得到了一个错误:白标错误页面。我为这个问题花了三天时间。非常感谢你,阿瑟
http://ex03.k8.sb.two.app.ingress.com/two/message/James%20Bond
Result: App Two- Hi James Bond API