首页 文章

Kubernetes StatefulSet - 在配置中的其他位置获取spec.replicas元数据和引用

提问于
浏览
1

我正在配置一个StatefulSet,我希望副本的数量(如下所示 spec.replicas )以某种方式作为参数传递到应用程序实例中 . 我的应用程序需要 spec.replicas 来确定副本的数量,以便它知道从MySQL表加载哪些行 . 我不想对 spec.replicas 和应用程序参数中的副本数量进行硬编码,因为在向上或向下缩放副本数量时这将不起作用,因为应用程序参数需要在缩放时进行调整 .

这是我的StatefulSet配置:

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  labels:
    run: my-app
  name: my-app
  namespace: my-ns
spec:
  replicas: 3
  selector:
    matchLabels:
      run: my-app
  serviceName: my-app
  podManagementPolicy: Parallel
  template:
    metadata:
      labels:
        run: my-app
    spec:
      containers:
      - name: my-app
        image: my-app:latest
        command:
          - /bin/sh
          - /bin/start.sh
          - dev
          - 2000m
          - "0"
          - "3" **Needs to be replaced with # replicas**
          - 127.0.0.1
          - "32990"
        imagePullPolicy: Always
        livenessProbe:
          httpGet:
            path: /health
            port: 8081
          initialDelaySeconds: 180
          periodSeconds: 10
          timeoutSeconds: 3
        readinessProbe:
          failureThreshold: 10
          httpGet:
            path: /ready
            port: 8081
            scheme: HTTP
          initialDelaySeconds: 30
          periodSeconds: 15
          successThreshold: 1
          timeoutSeconds: 3
        ports:
        - containerPort: 8080
          protocol: TCP
        resources:
          limits:
            memory: 2500Mi
      imagePullSecrets:
      - name: snapshot-pull
      restartPolicy: Always

我已经阅读了Kubernetes文档, spec.replicas 字段的范围是pod或容器级别,而不是StatefulSet,至少就我所见 .

提前致谢 .

2 回答

  • 0

    您可以使用yaml锚点执行此操作:

    退房:https://github.com/kubernetes/helm/blob/master/docs/chart_template_guide/yaml_techniques.md

    apiVersion: apps/v1beta1 kind: StatefulSet metadata: labels: run: my-app name: my-app namespace: my-ns spec: replicas: &numReplicas 3 selector: matchLabels: run: my-app serviceName: my-app podManagementPolicy: Parallel template: metadata: labels: run: my-app spec: containers: - name: my-app image: my-app:latest command: - /bin/sh - /bin/start.sh - dev - 2000m - "0" - *numReplicas - 127.0.0.1 - "32990" imagePullPolicy: Always livenessProbe: httpGet: path: /health port: 8081 initialDelaySeconds: 180 periodSeconds: 10 timeoutSeconds: 3 readinessProbe: failureThreshold: 10 httpGet: path: /ready port: 8081 scheme: HTTP initialDelaySeconds: 30 periodSeconds: 15 successThreshold: 1 timeoutSeconds: 3 ports: - containerPort: 8080 protocol: TCP resources: limits: memory: 2500Mi imagePullSecrets: - name: snapshot-pull restartPolicy: Always

  • 1

    通常你会使用向下api来做这种事情 . https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/

    但是,kubernetes目前不可能将部署/有状态规范数据传播到具有向下api的pod规范中,也不应该如此 . 如果您对此软件负责,我会设置一些内部功能,以便它可以找到它的同行并定期确定它们的计数 .

相关问题