kubernetes 使用索引设置 env 变量值

问题描述 投票:0回答:1

我有这些豆荚:

NAME                  READY   STATUS    RESTARTS   AGE    IP            NODE                  NOMINATED NODE   READINESS GATES
pod/zabbix-server-0   1/1     Running   0          67m    10.244.1.12   i-0155b0a4f7376aea5   <none>           <none>
pod/zabbix-server-1   1/1     Running   0          73m    10.244.0.13   i-0af3ff4d3cc1a4a4a   <none>           <none>
pod/zabbix-server-2   1/1     Running   0          73m    10.244.1.11   i-0155b0a4f7376aea5   <none>           <none>

现在我尝试使用此代码部署

zabbix-agent

apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: mon-zabbix
  name: zabbix-agent
spec:
  serviceName: zabbix-agent
  replicas: 3
  selector:
    matchLabels:
     app: zabbix-agent
  template:
    metadata:
      labels:
        app: zabbix-agent
    spec:   
      containers:
      - name: zabbix-agent
        image: zabbix/zabbix-agent:ubuntu-6.0-latest
        ports:
        - containerPort: 10050
        env:
        - name: ZBX_SERVER_HOST
          value: zabbix-server-N <- how can refer a zabbix-server-0 for zabbix-agent-0
        - name: ZBX_HOSTNAME
          value: zabbix-server-N <- how can refer a zabbix-server-0 for zabbix-agent-0
        volumeMounts:
        - name: pvc
          mountPath: /etc/zabbix/zabbix_agentd.d
        securityContext:
          allowPrivilegeEscalation: false
  volumeClaimTemplates:
  - metadata:
      name: pvc
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 100Mi

是否可以使用zabbix-server-0为zabbix-agent-0设置变量ZBX_SERVER_HOST和ZBX_HOSTNAME,为zabbix-agent-1设置zabbix-server-1等...?

kubernetes zabbix
1个回答
0
投票

您可以使用

apps.kubernetes.io/pod-index
标签和变量插值来做到这一点。

        env:
        - name: POD_NUMBER
          valueFrom:
            fieldRef:
              fieldPath: metadata.labels['apps.kubernetes.io/pod-index']
        - name: ZBX_SERVER_HOST
          value: zabbix-server-$(POD_NUMBER)
© www.soinside.com 2019 - 2024. All rights reserved.