Spring Boot 应用中的 Kubernetes pod 级配置外化

我需要社区的一些帮助,我还是 K8 和 Spring Boot 的新手。提前谢谢大家。
我需要的是在 K8 环境中运行 4 个 K8 pod,并且每个 pod 的配置略有不同,例如,我的 Java 类之一中有一个名为区域的属性,它从 Application.yml 中提取它的值,例如

@Value("${regions}")
私有字符串区域;

现在在将其部署到 K8 env 后,我想要运行 4 个 pod(我可以在 helm 文件中配置它),并且在每个 pod 中,regions 字段应该具有不同的值。 这是可以实现的吗?任何人都可以提供任何建议吗?

smartbeth1 回答:Spring Boot 应用中的 Kubernetes pod 级配置外化

如果您想以不同的配置运行 4 个不同的 Pod,您必须在 kubernetes 中部署 4 个不同的部署。

您可以根据需要创建不同的 configmap,存储整个 Application.yaml 文件或环境变量,并将其注入不同部署

如何将整个 application.yaml 存储在 config map

apiVersion: v1
kind: ConfigMap
metadata:
  name: yaml-region-first
data:
  application.yaml: |-
    data: test,region: first-region

与您可以使用第二个部署创建配置映射的方式相同。

 apiVersion: v1
    kind: ConfigMap
    metadata:
      name: yaml-region-second
    data:
      application.yaml: |-
        data: test,region: second-region

您可以将此 configmap 注入到每个部署中

示例:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: hello-app
  name: hello-app
  namespace: default
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: hello-app
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: hello-app
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
        volumeMounts:
          - mountPath: /etc/nginx/app.yaml
            name: yaml-file
            readOnly: true
      volumes:
      - configMap:
          name: yaml-region-second
          optional: false
        name: yaml-file

相应地,您也可以创建舵图。

如果您只是传递单个环境而不是将整个文件存储在 configmap 中,您可以直接为部署添加价值。

示例:

apiVersion: v1
kind: Pod
metadata:
  name: print-greeting
spec:
  containers:
  - name: env-print-demo
    image: bash
    env:
    - name: REGION
      value: "one"
    - name: HONORIFIC
      value: "The Most Honorable"
    - name: NAME
      value: "Kubernetes"
    command: ["echo"]
    args: ["$(GREETING) $(HONORIFIC) $(NAME)"]

https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/

对于每个部署,您的环境都会有所不同,并且在掌舵中,您可以使用 CLI 命令dynamicallyupdate 它。

本文链接:https://www.f2er.com/930.html

大家都在问