spring 启动云 kubernetes 配置不适用于多个 pods

spring boot cloud kubernetes config not working for multiple pods

我正在使用 spring-cloud-starter-kubernetes-all 依赖项从我的 spring 启动微服务读取配置映射,它工作正常。

修改配置映射后我正在使用刷新端点

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

它按预期工作并且应用程序加载配置映射更改。

问题 如果我的应用程序只有 1 个 pod,上面的工作正常,但是当我使用更多的 1 pods 只有 1 pods 选择不是全部的更改。

在下面的示例中,只有 i pod 选择了更改

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

minkube 部署

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

配置

@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

发生这种情况是因为当您点击 curl http://192.168.99.100:30824/actuator/refresh -d {} -H "Content-Type: application/json" 时,kubernetes 会通过循环负载平衡将该请求发送到服务后面的 pods 之一。

您应该通过设置 spring.cloud.kubernetes.reload.enabled=true 来使用 property source reload feature。这将在配置映射发生更改时重新加载 属性,因此您不需要使用刷新端点。