Service Discover using Spring cloud kubernetes

7/7/2019

I am doing a POC on simple microservices architecture using typical Spring cloud stack but instead of Eureka server, service discovery is to be made using spring-cloud-kubernetes which is not working.

The whole POC is here - https://github.com/dhananjay12/spring-microservices-using-spring-kubernetes

Gateway as a edge server and 2 downstream services- user-service and contact-us-service.

The k8 setup is in k8s folder.

The downstream services have following dependencies:

<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

and annotation of @EnableDiscoveryClient in the main class.

The gateway service has too relevant kubernetes dependencies:

<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

and annotation of @EnableDiscoveryClient in the main class.

Please see the deployment and service yaml here - https://github.com/dhananjay12/spring-microservices-using-spring-kubernetes/tree/master/k8s

I am able to get to gateway but it is not routing to downstream service like user-service:

For example - /user-service/users/getPublicMailingAddress

gives Whitable error page

enter image description here

and the logs in gateway shows:

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"
-- Dhananjay
kubernetes
spring-cloud
spring-cloud-kubernetes

2 Answers

7/8/2019

Is your cluster running in RBAC mode? If so you'll probably have to create a service account, give it the proper cluster role and configure your deployments to use that specific service account.

In case you don't want to expose every privilege to the pods, following access rights/resources should suffice:

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

7/17/2019

Spring Cloud Kubernetes requires access to the Kubernetes API in order to be able to retrieve a list of addresses for pods running for a single service. If you use Kubernetes, you should just execute the following command:

kubectl create clusterrolebinding admin --clusterrole=cluster-admin --serviceaccount=default:default
-- Dhananjay
Source: StackOverflow