spring boot cloud kubernetes config not working for multiple pods

1/19/2020

I am using spring-cloud-starter-kubernetes-all dependency for reading config map from my spring boot microservices and its working fine.

After modifying the config map i am using refresh endpoint

minikube servie list # to get the servive url 
curl http://192.168.99.100:30824/actuator/refresh -d {} -H "Content-Type: application/json"

it working as expected and application loads configmap changes.

Issue The above working fine if i have only 1 pod of my application but when i do use more that 1 pods only 1 pods picks the changes not all.

In below example only i pod picks the changes

[message-producer-5dc4b8b456-tbbjn message-producer] Say Hello to the World12431
[message-producer-5dc4b8b456-qzmgb message-producer] Say Hello to the World

minkube deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: message-producer
  labels:
    app: message-producer
spec:
  replicas: 2
  selector:
    matchLabels:
      app: message-producer
  template:
    metadata:
      labels:
        app: message-producer
    spec:
      containers:
        - name: message-producer
          image: sandeepbhardwaj/message-producer
          ports:
            - containerPort: 8080

---
apiVersion: v1
kind: Service
metadata:
  name: message-producer
spec:
  selector:
    app: message-producer
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

configmap.yml

kind: ConfigMap
apiVersion: v1
metadata:
  name: message-producer
data:
  application.yml: |-
    message: Say Hello to the World

bootstrap.yml

spring:
  cloud:
    kubernetes:
      config:
        enabled: true
        name: message-producer
        namespace: default
      reload:
        enabled: true
        mode: EVENT
        strategy: REFRESH
        period: 3000

configuration

@ConfigurationProperties(prefix = "")
@Configuration
@Getter
@Setter
public class MessageConfiguration {
    private String message = "Default message";
}

rbac

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  namespace: default # "namespace" can be omitted since ClusterRoles are not namespaced
  name: service-reader
rules:
- apiGroups: [""] # "" indicates the core API group
  resources: ["services"]
  verbs: ["get", "watch", "list"]

---
apiVersion: rbac.authorization.k8s.io/v1
# This cluster role binding allows anyone in the "manager" group to read secrets in any namespace.
kind: ClusterRoleBinding
metadata:
  name: service-reader
subjects:
- kind: User
  name: default # Name is case sensitive
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: service-reader
  apiGroup: rbac.authorization.k8s.io
-- Sandeep Bhardwaj
kubernetes
minikube
spring
spring-boot
spring-cloud-kubernetes

1 Answer

1/19/2020

This is happening because when you hit curl http://192.168.99.100:30824/actuator/refresh -d {} -H "Content-Type: application/json" kubernetes will send that request to one of the pods behind the service via round robin load balancing.

You should use the property source reload feature by setting spring.cloud.kubernetes.reload.enabled=true. This will reload the property whenever there is a change in the config map hence you don't need to use the refresh endpoint.

-- Arghya Sadhu
Source: StackOverflow