Unable to access Spring Boot microservice exposed via nginx ingress controller on Kubernetes cluster running on AWS

瘦欲@ 提交于 2020-12-04 07:56:53

问题


I have spun up a 3 node Kubernetes cluster (version: 1.5.8) on AWS using the kube-up.sh script following this walkthrough:

https://ryaneschinger.com/blog/building-a-kubernetes-cluster-on-aws/

I'm able to successfully access the cluster and view the UI. Output of kubectl cluster-info command:

I wrote a simple Spring Boot microservice:

@RestController
public class AddCustomerController {

    private static final String template = "Customer %s is added.";

    @RequestMapping("/addcustomer")
    public Message addcustomer(@RequestParam(value="name") String name) {

        //Retrieve the hostname of the "node"/"container"
        String hostname = null;
        try {
            hostname = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return new Message(ThreadLocalRandom.current().nextLong(),
                            String.format(template, name),
                            hostname);
    }
}

and packaged it in a Docker container after the Gradle build and am able to successfully use it locally. I have pushed the image to DockerHub.

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD build/libs/*.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Now I'm using Helm Charts to deploy this application to Kubernetes.

Deployment descriptor:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: add-customer-deployment
spec:
  replicas: 3
  template:
    metadata:
      name: add-customer-microservice
      labels:
        app: add-customer
    spec:
      containers:
      - image: {{ .Values.dockerHubUsername }}/add-customer-microservice:latest
        name: add-customer-microservice
        imagePullPolicy: Always
        ports:
        - containerPort: 8080

Service descriptor:

apiVersion: v1
kind: Service
metadata:
  name: add-customer-service
spec:
  selector:
    app: add-customer
  ports:
    - port: 1000
      protocol: TCP
      targetPort: 8080
      name: access-port
  type: NodePort

I have followed the same procedure for 3 other similar Spring Boot microservices.

Ingress descriptor:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: quantiphi-poc-ingress-dns
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - host: crud.qdatalabs.com
      http:
        paths:
        - path: /service1
          backend:
            serviceName: add-customer-service
            servicePort: 1000
        - path: /service1/*
          backend:
            serviceName: add-customer-service
            servicePort: 1000
        - path: /service2
          backend: 
            serviceName: get-customer-service
            servicePort: 2000
        - path: /service2/*
          backend:
            serviceName: get-customer-service
            servicePort: 2000
        - path: /service3
          backend: 
            serviceName: update-customer-service
            servicePort: 3000
        - path: /service3/*
          backend:
            serviceName: update-customer-service
            servicePort: 3000
        - path: /service4
          backend: 
            serviceName: delete-customer-service
            servicePort: 4000
        - path: /service4/*
          backend:
            serviceName: delete-customer-service
            servicePort: 4000

First I install the nginx controller on my cluster using the Helm Charts:

helm install --name my-release stable/nginx-ingress

Then I install my Chart using:

helm install folder-conataining-helm-chart/

Then I point the alias of crud.qdatalabs.com (Type A) from Route53 to the ELB spawned by the Ingress resource.

The URL crud.qdatalabs.com/healthz is giving 200 OK response

When I try to access the microservices using the URL crud.qdatalabs.com/service1/addcustomer?name=starman

I'm treated with the WhiteLabel Error Page:

I think I have made some configuration error, but can't put my finger on it. Please help me with any direction. I'll be happy to provide more details. Thank you.


回答1:


As I stated in Setting up a Kuberentes cluster with HTTP Load balancing ingress for RStudio and Shiny results in error pages, the most likely problem you have is that when you go with this ingress you attached your URI is different then with direct accesc ( /service1/ vs / ) so your app is lost and has no content for that uri.

With Nginx Ingress Controller you can use ingress.kubernetes.io/rewrite-target: / annotation to mitigate this and make sure that / is accessed even when there is a subfolder in the ingress path.

So, you either need to use proper rewrite annotation or support the path you use in the ingress inside your service.



来源:https://stackoverflow.com/questions/48762138/unable-to-access-spring-boot-microservice-exposed-via-nginx-ingress-controller-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!