简体   繁体   English

通过 Kubernetes 上的 nginx 入口中的服务公开容器中的基本路径

[英]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.

相关问题 如何使用 Docker 桌面为 Windows Kube.netes 和 ingress-nginx 公开 postgres tcp 端口 - How to expose postgres tcp port using Docker Desktop for Windows Kubernetes and ingress-nginx Dockerfile EXPOSE 和 Kubernetes 服务/容器端口之间的关系? - Relationship between Dockerfile EXPOSE and Kubernetes service/container ports? 如何使用Azure容器服务将Kubernetes pod公开到公共Internet - How to expose the Kubernetes pod to the public internet using Azure container service 如何使用 Ingress 公开服务? - How to expose a service using Ingress? Ingress如何决定他是否必须在http或https上公开kubernetes服务? - how ingress decides whether he has to expose kubernetes service on http or https? 如何使用 Kube.netes Ingress 暴露 Flask App? - How to expose Flask App with Kubernetes Ingress? Kubernetes Nginx 未找到入口“ingress-nginx-admission” - Kubernetes Nginx Ingress "ingress-nginx-admission" not found Kubernetes HyperV群集公开服务 - Kubernetes HyperV Cluster Expose Service Kubernetes ingress-nginx从非默认命名空间调用服务 - Kubernetes ingress-nginx call service from non-default namespace 使用 ingress-nginx 在 kube.netes 中提供 static 个资产 - Serving static assets in kubernetes with ingress-nginx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM