Can swagger-UI work for services inside kubernetes?

7/16/2020

I have a few microservices running in a kubernetes cluster. Each service has its own APIs and corresponding swagger.json.

I'm considering deploying a swagger-ui pod inside kubernetes to show these swagger.json and execute the APIs.

Tried it and realized that:

  1. swagger-ui can't find the services' swagger.jsons. Even though the swagger-ui pod can resolve the serives' dns name, but my browser can't. Seems the code is running in browser instead of the pod.
  2. For the same reason that it's browser running the code, the swagger-ui can't be used to execute the APIs, since the services are not reachable from outside kubernetes.

So my question is, 1. is there a way to let swagger-ui run code inside the pod? so that it can reach the services and execute their apis? 2. is there ANY way to execute the kubernetes services' apis via webui, if we don't use swagger-ui?

Thanks a lot!

-- Wei Huang
kubernetes
swagger
swagger-ui

2 Answers

8/14/2021
  1. I solved it here: https://stackoverflow.com/a/68798178/12877180

  2. Executing kubernetes services by using their names via webui can be achived by using nginx reverse proxy mechanizm.

https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

This mechanizm will redirect your API request invoked from the browser level to internal cluster service name where the nginx server is running.

An example nginx configuration

server {
  listen 80;

  location /api/ {
    proxy_pass http://<service-name>.<namespace>:<port>;
  }

  location / {
    root /usr/share/nginx/html;
    index index.html;
    add_header 'Access-Control-Allow-Origin' '*';
    try_files $uri $uri/ /index.html =404;
  }
}

In above example all your .../api/... calls will be redirect to http://<service-name>.<namespace>:<port>/api/... endpoint.

Unfortunately i dont't know how to achive the same goal with the Swagger-UI

NOTE

If the proxy_pass url would end with '/' the location url ex. '/api/' would not be added to desired url.

-- Mikolaj
Source: StackOverflow

8/14/2021

is there a way to let swagger-ui run code inside the pod? so that it can reach the services and execute their apis?

You can expose or share the file of JSON to swagger POD and swagger UI can server those files, that's one way possible. However it's not good idea to set the RWM (Read-write many) setting up the NFS inside K8s and share the pods file system across each other.

is there ANY way to execute the kubernetes services' apis via webui, if we don't use swagger-ui?

You can also try the ReDoc : https://github.com/Redocly/redoc

which is similar to swagger also in below github repo there is also example available for both.

To run the swagger inside the Kubernetes you can try

Two ways swagger on getting file either from the file system or from the URL.

For example, if you are looking forward to running the deployment of swagger UI.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: swagger-ui
  labels:
    app: swagger-ui
spec:
  replicas: 1
  selector:
    matchLabels:
      app: swagger-ui
  template:
    metadata:
      labels:
        app: swagger-ui
    spec:
      containers:
      - name: swagger-ui
        image: swaggerapi/swagger-ui #build new image for adding local swaggerfile
        ports:
        - containerPort: 8080
        env:
        - name: BASE_URL
          value: /
        - name: API_URLS
          value: >-
            [
            {url:'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml',name:'Pet Store Example'},
            {url:'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/uspto.yaml',name:'USPTO'},
            {url:'/example-swagger.yaml',name:'Local Example'} 
            ]

https://github.com/harsh4870/Central-API-Documentation-kubernetes

You can also read about the newman : https://azevedorafaela.com/2018/12/18/how-to-test-internal-microservices-in-a-kubernetes-cluster/

-- Harsh Manvar
Source: StackOverflow