本文介绍了无法访问通过AWS上运行的Kubernetes集群上的Nginx入口控制器公开的Spring Boot微服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此演练之后,我已使用kube-up.sh脚本在AWS上启动了一个3节点的Kubernetes集群(版本:1.5.8):

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/

我能够成功访问集群并查看UI. kubectl cluster-info命令的输出:

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

我写了一个简单的Spring Boot微服务:

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);
    }
}

并在Gradle构建之后将其打包在Docker容器中,并能够在本地成功使用它.我已将映像推送到DockerHub.

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"]

现在,我正在使用Helm Charts将此应用程序部署到Kubernetes.

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

部署描述符:

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

服务描述符:

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

对于其他3个类似的Spring Boot微服务,我也遵循相同的步骤.

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

入口描述符:

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

首先,我使用Helm Charts在我的集群上安装nginx控制器:

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/

然后,我将Crud.qdatalabs.com(类型A)的别名从Route53指向由Ingress资源生成的ELB.

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

URL crud.qdatalabs.com/healthz 正在给出200 OK响应

The URL crud.qdatalabs.com/healthzis giving 200 OK response

当我尝试使用URL crud.qdatalabs.com/service1访问微服务时/addcustomer?name = starman

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

我收到了WhiteLabel错误页面的提示:

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.

推荐答案

正如我在,您最可能遇到的问题是当您进行此入口操作时,您附加的URI与直接访问权限(/service1/vs/)不同,因此您的应用程序丢失了,并且没有该uri的内容.

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.

使用Nginx Ingress Controller,您可以使用ingress.kubernetes.io/rewrite-target:/注释来减轻这种情况,并确保即使入口路径中有子文件夹,也可以访问/.

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.

这篇关于无法访问通过AWS上运行的Kubernetes集群上的Nginx入口控制器公开的Spring Boot微服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:04