使用Spring Cloud Kubernetes服务发现

时间:2019-07-07 06:52:57

标签: kubernetes spring-cloud spring-cloud-kubernetes

我正在使用典型的Spring云堆栈在简单的微服务架构上进行POC,但不是使用Eureka服务器,而是使用不起作用的spring-cloud-kubernetes进行服务发现。

整个POC在这里-https://github.com/dhananjay12/spring-microservices-using-spring-kubernetes

网关作为边缘服务器和2个下游服务-用户服务和联系我们服务。

k8设置位于k8s文件夹中。

下游服务具有以下依赖性:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-kubernetes</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

application.yml

server:
  port: 8100

management:
  endpoints:
    web:
      exposure:
        include: '*'


spring:
  cloud:
    kubernetes:
      enabled: true
      reload:
        enabled: true
eureka:
  client:
    enabled: false

bootstrap.yml:

spring:
  application:
    name: user-service

和主类中的@EnableDiscoveryClient注释。

网关服务与kubernetes的依赖关系过于紧密:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-kubernetes</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-kubernetes-ribbon</artifactId>
        </dependency>

application.yml

server:
  port: 8050

spring:
  application:
    name: gateway
  cloud:
    kubernetes:
      enabled: true
      reload:
        enabled: true
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true

eureka:
  client:
    enabled: false


logging:
  level:
    root: DEBUG
    org.springframework.gateway: TRACE
    org.springframework.cloud.gateway: TRACE
    org.springframework.cloud.loadbalancer: TRACE


management:
  endpoints:
    web:
      exposure:
        include: '*'


bootstrap.yml

spring:
  application:
    name: gateway

和主类中的@EnableDiscoveryClient注释。

请在此处查看部署和服务Yaml-https://github.com/dhananjay12/spring-microservices-using-spring-kubernetes/tree/master/k8s

我能够到达网关,但不能路由到诸如用户服务之类的下游服务:

例如-/ user-service / users / getPublicMailingAddress

提供Whitable错误页面

enter image description here

,网关中的日志显示:


2019-07-07 06:40:30.017 TRACE 1 --- [or-http-epoll-2] o.s.c.g.h.p.RoutePredicateFactory : Pattern "[/my-nginx-nginx-ingress-controller/**]" does not match against value "/user-service/users/getPublicMailingAddress"

2 个答案:

答案 0 :(得分:0)

您的集群是否以RBAC模式运行?如果是这样,您可能必须创建一个服务帐户,为其指定适当的群集角色,并配置您的部署以使用该特定服务帐户。

如果您不想将所有权限公开给Pod,则应遵循以下访问权限/资源:

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: custom-role
rules:
- apiGroups: [""]
  resources:
  - endpoints
  - namespaces
  - pods
  - services
  verbs:
  - get
  - watch
  - list

答案 1 :(得分:0)

Spring Cloud Kubernetes要求访问Kubernetes API,以便能够检索为单个服务运行的Pod的地址列表。如果您使用Kubernetes,则应该执行以下命令:

kubectl create clusterrolebinding admin --clusterrole=cluster-admin --serviceaccount=default:default