问题
So, I wrote an API that is listening on the the path /api/v1/books
and is deployed as a deployment on my k8s cluster, created service (restapi-service
) so that we can call that from another pods.
Now I created another deployment (restapi-ui-deployment
) that just has a .html
page and and its being deployed on nginx, which eventually calls the the service that we have created earlier to get the response.
Now, the issue is when I exec into the pods of restapi-ui-deployment
I am successfully able to curl http://restapi-service:8081/api/v1/books
. But if we are trying to do the same thing from .html page that is deployed I get
GET http://restapi-service:8081/api/v1/books net::ERR_NAME_NOT_RESOLVED
Below is the code that is being deployed as restapi-ui-deployment
if (xmlObj != null){
xmlObj.open("GET", "http://restapi-service:8081/api/v1/books", true)
xmlObj.onreadystatechange = processResponse;
xmlObj.send(null)
}
else{
console.log("There was an error getting the object.")
}
function processResponse(){
if (xmlObj.status == 200 && xmlObj.readyState == 4){
console.log("Got the response successfully")
response = xmlObj.responseText
}
else{
console.log("There was an issue getting the response.")
}
}
回答1:
I am afraid that you are confused about the way your application works. The XmlHttpRequest is originating in a webbrowser, therefore outside of the kubernetes cluster, not from nginx inside your cluster. (nginx serves the html page) The kubernetes dns is not available outside of kubernetes, nor would a connection to a ClusterIP work from the outside. Solution: Create an appropriate Ingress and call that from your frontend or provide a proxy on your nginx where you got the frontend delivered. (That would lead to really getting the request origin as your nginx)
来源:https://stackoverflow.com/questions/57348029/unable-to-access-a-service-from-another-pod-using-xmlhttprequest-object