Kubernetes StatefulSet-获取spec.replicas元数据并在配置中的其他地方引用 [英] Kubernetes StatefulSet - obtain spec.replicas metadata and reference elsewhere in configuration

查看:543
本文介绍了Kubernetes StatefulSet-获取spec.replicas元数据并在配置中的其他地方引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am configuring a StatefulSet where I want the number of replicas (spec.replicas as shown below) available to somehow pass as a parameter into the application instance. My application needs spec.replicas to determine the numer of replicas so it knows what rows to load from a MySQL table. I don't want to hard-code the number of replicas in both spec.replicas and the application parameter as that will not work when scaling the number of replicas up or down, since the application parameter needs to adjust when scaling.

这是我的StatefulSet配置:

Here is my StatefulSet config:


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 字段的作用域是容器级别或容器级别,从未涉及StatefulSet,至少据我所知.

I have read the Kubernetes docs and the spec.replicas field is scoped at the pod or container level, never the StatefulSet, at least as far as I have seen.

谢谢.

推荐答案

您可以使用Yaml锚来执行此操作:

You could use a yaml anchor to do this:

签出: 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

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

这篇关于Kubernetes StatefulSet-获取spec.replicas元数据并在配置中的其他地方引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆