I have a spring boot application which has some end points, ex: product/version
I deployed then the application to the K8s network and expose it out using Ingress as below:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
rules:
- http:
paths:
- path: /productApp
backend:
serviceName: productappservice
servicePort: 80
I added a path to the ingress to have my application running under my-host:8080/productApp/
In the Deployment
configuration, I need to add the extra context-path
to my application using ConfigMap
to map with the URL that the ingress is exposing:
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: product-app
spec:
replicas: 1
template:
metadata:
labels:
app: product-app
spec:
containers:
- name: product-app
image: my-docer-registry/product-app:latest
imagePullPolicy: Always
ports:
- containerPort: 8080
env:
- name: server.servlet.context-path
valueFrom:
configMapKeyRef:
name: product-app-values
key: server.servlet.context-path
---
apiVersion: v1
kind: ConfigMap
metadata:
name: product-app-values
data:
server.servlet.context-path: '/productApp'
So now my application is running under my-host:8080/productApp/
and when I tried to call the API ex: my-host:8080/productApp/product/version
, it worked fine.
The Problem came when I work with Swagger. I have swagger under: my-host:8080/productApp/swagger-ui.html
and when I tried to call the product/version
API, it returned TypeError: Failed to fetch
. Then I realized that the host URL that swagger is sending request to is no longer my-host:8080
, it was only my-host
:
curl -X GET "http://my-host/productApp/product/version" -H "accept: application/json;charset=UTF-8"
and here is the example json:
{
"swagger": "2.0",
"info": {
"version": "0.0.1-SNAPSHOT",
"title": "Product App REST API"
},
"host": "my-host",
"basePath": "/productApp",
"tags": [{
"name": "Product App API",
"description": "REST API of Product App"
},
{
"name": "version API",
"description": "Version information of application and REST API"
}],
"paths": {
.....
}
}
What I expect here is the host URL should be my-host:8080
, instead of only my-host
, that's why the request in swagger is failed to sent.
Does anyone have an idea how to fix this problem. Thank you in advanced!