如何在kubernetes应用程序中提供静态内容 [英] How to serve static contents in a kubernetes application

查看:120
本文介绍了如何在kubernetes应用程序中提供静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型的 java 网络应用程序,其中包含三个微服务- api-服务 book-service db-service 所有这些都使用minikube在本地部署在kubernetes集群上.

I have a small java webapp comprising of three microservices - api-service,book-service and db-service all of which are deployed on a kubernetes cluster locally using minikube.

我计划为 api-service book-service 保留单独的UI,并通过单独的pod提供常见的静态文件,可能是nginx:alpine图像

I am planning to keep separate UIs for api-service and book-service , with the common static files served from a separate pod, probably an nginx:alpine image.

我能够创建一个前端,以引用

I was able to create a front end that serves the static files from nginx:alpine referring to this tutorial.

我想使用 ingress-nginx 控制器将请求路由到这两个服务.

I would like to use ingress-nginx controller for routing requests to the two services.

下图粗略显示了我现在所在的位置.

The below diagram crudely shows where I am now.

对于将静态内容服务的容器放置在何处以及如何将其连接到入口资源感到困惑,我猜想在入口之前保留前端容器会破坏ingress-nginx控制器的目的.提供静态文件的最佳做法是什么.感谢任何帮助.谢谢.

I am confused as to where I should place the pod that serves the static content, and how to connect it to the ingress resource.I guess that keeping a front end pod before ingress defeats the purpose of ingress-nginx controller. What is the best practice to serve static files. Appreciate any help. Thanks.

推荐答案

您似乎对以下事实感到困惑:在线浏览的用户会触发标准请求,以下载"您的静态内容,使用您的2个API(book和api).访问您的API的不是提供静态内容的NGINX服务,而是用户的浏览器/应用程序,并且它们对静态内容和API都执行完全相同的操作(前者具有更多/特定的标头和数据,例如auth ... ).

Looks like you are confusing the fact that users, browsing online, will trigger standard requests to both "download" your static content, and use your 2 APIs (book and api). It's not the NGINX service serving the static content that is accessing your APIs, but the users browsers/applications, and they do that exactly the same for both static content and APIs (former has more/specific headers and data, like auth...).

在图表上,您希望将新的static-service放置在与book-serviceapi-service完全相同的级别,即后面.但是您的static-service不会像其他2那样与db-service链接.然后只需完成您的入口规则,并在本例中最后添加static-service

On your diagram you'll want to put your new static-service at the exact same level as your book-service and api-service, ie behind the ingress. But your static-service won't have a link with the db-service like the other 2. Then just complete your ingress rules, with the static-service at the end as in this example:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: your-global-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /book-service
        backend:
          serviceName: book-service
          servicePort: 80
      - path: /api-service
        backend:
          serviceName: api-service
          servicePort: 80
      - path: /
        backend:
          serviceName: static-service
          servicePort: 80

在上面的示例中,您必须调整服务名称和端口,并选择希望用户访问API的路径:

You'll have to adjust your services names and ports, and pick the paths you want your users to access your APIs, in the example above you'd have:

  • foo.bar.com/book-service用于您的图书服务
  • foo.bar.com/api-service用于api服务
  • foo.bar.com/,即所有其他要进入静态服务的地方
  • foo.bar.com/book-service for your book-service
  • foo.bar.com/api-service for the api-service
  • foo.bar.com/ ie everything else going to the static-service

这篇关于如何在kubernetes应用程序中提供静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆