Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve static contents in a kubernetes application

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.

enter image description here

like image 575
aswin prabhakar Avatar asked Dec 03 '18 13:12

aswin prabhakar


People also ask

How do I set up ingress to serve static content on Kubernetes?

You can't configure your Ingress to serve static data (from your host files for example) alone because the Ingress resource is just a way to configure a way of understanding for Kubernetes on how to access a specific internal resource from outside the cluster. You must configure a web server (NGINX, Apache, etc.)

Why does Kubernetes need static pods?

Static pods are usually used by software bootstrapping kubernetes itself. For example, kubeadm uses static pods to bringup kubernetes control plane components like api-server, controller-manager as static pods.


2 Answers

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
like image 155
Clorichel Avatar answered Oct 02 '22 04:10

Clorichel


You should have, 3 distinct pods I guess : - static - book-service - api-service

The static pod will most likely not scale at the same speed than the two other.

Creating the services for each of your deployment. Then use the ingres to route the traffic on the proper endpoint.

Is it something like that you are trying to achieve?

like image 32
Axel Avatar answered Oct 02 '22 05:10

Axel