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

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

问题描述

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

我打算为 api-servicebook-service 保留单独的用户界面,公共静态文件从单独的 pod 提供,可能是 nginx:alpine 图像.

我能够创建一个前端来提供来自 nginx:alpine 的静态文件,参考这个

解决方案

看起来您很困惑这样一个事实,即用户在线浏览会触发标准请求以下载"您的静态内容, 使用您的 2 个 API(书籍和 API).不是 NGINX 服务为访问您的 API 的静态内容提供服务,而是用户浏览器/应用程序,他们对静态内容和 API 执行的操作完全相同(前者具有更多/特定的标头和数据,例如身份验证...).

在您的图表上,您需要将新的 static-service 放在与 book-serviceapi-service,即在入口后面.但是您的 static-service 不会像其他 2 一样与 db-service 建立链接.然后只需完成您的入口规则,最后使用静态服务如本例所示:

apiVersion: extensions/v1beta1种类:入口元数据:名称:您的全球入口注释:nginx.ingress.kubernetes.io/rewrite-target:/规格:规则:- 主机:foo.bar.com网址:路径:- 路径:/book-service后端:服务名称:预订服务服务端口:80- 路径:/api-service后端:服务名称:api-service服务端口:80- 小路:/后端:服务名称:静态服务服务端口:80

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

  • foo.bar.com/book-service 用于您的图书服务
  • foo.bar.com/api-service 用于 api-service
  • foo.bar.com/ 即所有其他内容都将进入静态服务

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.

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.

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

The below diagram crudely shows where I am now.

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.

解决方案

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...).

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

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 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天全站免登陆