用作参数的 kubernetes pod 名称

Son*_*nam 1 kubernetes

我想使用 Kubernetes pod 名称作为容器中的标识符作为参数。

我已使用以下配置在 Kubernetes 集群上部署了 echo 容器:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo1
spec:
  selector:
    matchLabels:
      app: echo1
  replicas: 2
  template:
    metadata:
      labels:
        app: echo1
    spec:
      containers:
      - name: echo1
        image: hashicorp/http-echo
        args:
        - "-text=echo1"
        ports:
        - containerPort: 5678
Run Code Online (Sandbox Code Playgroud)

当我执行“kubectl get pods”时:

NAME                                     READY   STATUS    RESTARTS   AGE
echo1-76c689b7d-48h4v                    1/1     Running   0          19h
echo1-76c689b7d-4gq2v                    1/1     Running   0          19h
Run Code Online (Sandbox Code Playgroud)

我想通过在上面的配置中传递 pod 名称来回显 pod 名称:

args:
            - "-text=echo1"
Run Code Online (Sandbox Code Playgroud)

如何访问要在参数中使用的 pod 名称?

cod*_*ger 5

所以有几件事。首先,您将使用 fieldRef 语法作为环境变量,如https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/中所示。然后您将在参数 ( "-text=$(PODNAME)") 中使用环境变量。但是,这将为您提供实际的 Pod 名称,例如echo1-76c689b7d-48h4v. 您想要的是部署名称或标签的值app,后者更容易代替metadata.name字段路径,使用类似的东西metadata.labels['app'](需要 Kubernetes 1.9+)。