1.元数据或自定义数据 传入环境变量官网参考链接
# 参考yaml
#root@node1:~$ cat deploy-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      name: test-deploy
      annotations:
        jarfile: test.jar
      labels:
        app: myapp
        jarfile: test2.jar
        release: canary
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v2
        env:
        - name: test
          valueFrom:
            fieldRef:
              fieldPath: metadata.annotations['jarfile']
        - name: test2
          valueFrom:
            fieldRef:
              fieldPath: metadata.labels['jarfile']
        - name: test3
          valueFrom:
            fieldRef:
              fieldPath: metadata.name

结果图

root@node1:~$ kb apply -f deploy-demo.yaml
deployment.apps/myapp-deploy configured
root@node1:~$ kb get pod |grep deploy-demo
root@node1:~$ kb get pod |grep deploy
myapp-deploy-6b7899c7d4-9mgzq   1/1       Running   0          38s
myapp-deploy-6b7899c7d4-j8n2g   1/1       Running   0          34s
root@node1:~$ kb exec -i myapp-deploy-6b7899c7d4-j8n2g -- printenv|grep test
test2=test2.jar
test3=myapp-deploy-6b7899c7d4-j8n2g
test=test.jar
2. Downward API(DownwardAPIVolumeFiles:挂载目录的方式)

参考未验证

apiVersion: v1
kind: Pod
metadata:
  name: kubernetes-downwardapi-volume-example
  labels:
    zone: us-est-coast
    cluster: test-cluster1
    rack: rack-22
  annotations:
    build: two
    builder: john-doe
spec:
  containers:
    - name: client-container
      image: k8s.gcr.io/busybox
      command: ["sh", "-c"]
      args:
      - while true; do
          if [[ -e /etc/podinfo/labels ]]; then
            echo -en '\n\n'; cat /etc/podinfo/labels; fi;
          if [[ -e /etc/podinfo/annotations ]]; then
            echo -en '\n\n'; cat /etc/podinfo/annotations; fi;
          sleep 5;
        done;
      volumeMounts:
        - name: podinfo
          mountPath: /etc/podinfo
          readOnly: false
  volumes:
    - name: podinfo
      downwardAPI:
        items:
          - path: "labels"
            fieldRef:
              fieldPath: metadata.labels
          - path: "annotations"
            fieldRef:
              fieldPath: metadata.annotations
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐