Clean deploy of Spring boot microservices with Config Server

3/18/2019

We had configured a kubernetes cluster where we deploy various services using spring boot and we have one service that is Spring Cloud Config Server.

Our trouble is that when we start the cluster all the services try to connect to the config server to download the configuration, and since the Config Server has not yet started all the services fail, causing kubernetes to retry the initialization and consuming many resources so that config server it self can not start.

We are wondering if there is a way to initialize all services in such a way that do not over load the cluster or so that they pacefully wait until the config server starts. As of now, all services start and we have to wait for like 20 minutes until the cluster works its way out.

Thanks in advance

-- jaxkodex
configserver
kubernetes
spring
spring-boot

2 Answers

3/18/2019

You can use Init Containers to ping for the server until it is online. An example would be:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 1
  template:
    metadata:
      labels:
        app: web
    spec:
      initContainers:
      - name: wait-config-server
        image: busybox
        command: ["sh", "-c", "for i in $(seq 1 300); do nc -zvw1 config-server 8080 && exit 0 || sleep 3; done; exit 1"]
      containers:
      - name: web
        image: my-mage
        ports:
        - containerPort: 80
          ...

In this example I am using an nc command for pinging the server but you can also use wget, curl or whatever is suited best for you.

-- Urosh T.
Source: StackOverflow

3/18/2019

Their are various options to do the same. Choose the one that best suits you:

  1. You can as well try to apply liveliness probe or readiness probe to the config server. In this manner, all containers can wait till the config server is up and running and then try to connect with the config server.
  2. You can use consul service running as a quorum of 3 or 5 services, and design the clients to connect to the consul and wait till the config server is up and running.
  3. You can write a startup script will will trigger the connection establishment with the config server and post which it can start the containers.
-- Vinod Kumar
Source: StackOverflow