For internal purpose, I'm building a dashboard application. In this dashboard I need to display some info about Kubernetes (running pods, clusters, etc).
I'm trying to call my Kubernetes API from my web app (from browser). Url of API is http://localhost:8001/api/v1/
I'm getting error on fetch data (CORS origin not allowed).
I've search on internet for hours trying to find solution but nothing is working. I know there is other stack post giving some solution but I'm not sure how to apply it. For ex. : Enabling CORS in Kubernetes API
Any of you know how to allow CORS origin on Kubernetes API (Docker for windows)?
Note: I'm using kubectl proxy
You can edit kubernetes API server yaml file, to get CORS working.
Add line --cors-allowed-origins=["http://*"\] argument to /etc/default/kube-apiserver or /etc/kubernetes/manifests/kube-apiserver.yaml file, it depends where your kube-apiserver configuration file is located.
spec:
containers:
- command:
- kube-apiserver
- --cors-allowed-origins=["http://*"]
Restart kube-apiserver.
Then add annotation to service configuration to dns.alpha.kubernetes.io/external: "http://localhost:8001/api/v1/"
in your service configuration file and apply changes.
Create a server sided middle layer that can proxy your requests to api without needing CORS on the api itself.
For example, with nginx:
server {
listen 80;
add_header "Access-Control-Allow-Origin" *;
location / {
proxy_pass https://my-api-that-needs-cors;
}
}
Take care to only expose what you need to expose.