[英]Expose basepath in container through service in nginx ingress on kubernetes
My ASP.NET Core web application uses basepath in startup like,我的 ASP.NET Core Web 应用程序在启动时使用 basepath,例如,
app.UsePathBase("/app1");
So the application will run on basepath.所以应用程序将在 basepath 上运行。 For example if the application is running on localhost:5000, then the app1 will be accessible on ' localhost:5000/app1 '.
例如,如果应用程序在 localhost:5000 上运行,那么app1将可以在“ localhost:5000/app1 ”上访问。
So in nginx ingress or any ingress we can expose the entire container through service to outside of the kubernetes cluster.所以在 nginx ingress 或任何 ingress 中,我们可以通过 service 将整个容器暴露给 kubernetes 集群之外。
My kubernetes deployment YAML file looks like below,我的 kubernetes 部署 YAML 文件如下所示,
apiVersion: apps/v1
kind: Deployment
metadata:
name: app1-deployment
spec:
selector:
matchLabels:
app: app1
replicas: 1
template:
metadata:
labels:
app: app1
spec:
containers:
- name: app1-container
image: app1:latest
ports:
- containerPort: 5001
---
apiVersion: v1
kind: Service
metadata:
name: app1-service
labels:
app: app1
spec:
type: NodePort
ports:
- name: app1-port
port: 5001
targetPort: 5001
protocol: TCP
selector:
app: app1
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: app1-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- http:
paths:
- path: /app1(/|$)(.*)
backend:
serviceName: server-service
servicePort: 5001
The above ingress will expose the entire container in ' localhost/app1 ' URL.上述入口将在“ localhost/app1 ” URL 中公开整个容器。 So the application will run on '/app1' virtual path.
所以应用程序将在“/app1”虚拟路径上运行。 But the app1 application will be accessible on ' localhost/app1/app1 '.
但是app1应用程序将可以在“ localhost/app1/app1 ”上访问。
So I want to know if there is any way to route ' localhost/app1 ' request to basepath in container application ' localhost:5001/app1 ' in ingress.所以我想知道是否有任何方法可以将 ' localhost/app1 ' 请求路由到容器应用程序 ' localhost:5001/app1 ' ingress 中的 basepath。
If I understand correctly, now the app is accessible on /app1/app1
and you would like it to be accessible on /app1
如果我理解正确,现在可以在
/app1/app1
上访问该应用程序,并且您希望它可以在/app1
上访问
To do this, don't use rewrite:为此,请不要使用重写:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: app1-ingress
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- http:
paths:
- path: /app1
backend:
serviceName: server-service
servicePort: 5001
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.