1

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

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

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

它按预期工作,应用程序加载 configmap 更改。

问题 如果我的应用程序只有 1 个 pod,但当我使用超过 1 个 pod 时,上述工作正常,但只有 1 个 pod 不会选择全部更改。

在下面的示例中,只有我 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

引导程序.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
4

1 回答 1

1

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

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

于 2020-01-19T06:15:47.667 回答