Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kubernetes 如何在k8s yaml文件中引用标签值_Kubernetes_Yaml_Istio - Fatal编程技术网

Kubernetes 如何在k8s yaml文件中引用标签值

Kubernetes 如何在k8s yaml文件中引用标签值,kubernetes,yaml,istio,Kubernetes,Yaml,Istio,我想引用k8s yaml文件中VirtualService规范部分中的标签值。我使用${metadata.labels[component]}来表示下面的位置。有没有办法实现我的想法 apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: istio-ingress-version namespace: netops labels: component: version spe

我想引用k8s yaml文件中VirtualService规范部分中的标签值。我使用${metadata.labels[component]}来表示下面的位置。有没有办法实现我的想法

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: istio-ingress-version
  namespace: netops
  labels:
    component: version
spec:
  hosts:
  - "service.api.com" 
  gateways:
  - public-inbound-gateway 
  http:
  - match: 
    - uri:
        prefix: /${metadata.labels[component]}/
      headers: 
        referer:
          regex: ^https://[^\s/]*a.api.com[^\s]* 
    rewrite:
      uri: "/"
    route:
    - destination:
        host: ${metadata.labels[component]}.3da.svc.cluster.local  
  - match: 
    - uri:
        prefix: /${metadata.labels[component]}/
      headers: 
        referer:
          regex: ^https://[^\s/]*b.api.com[^\s]* 
    rewrite:
      uri: "/"
    route:
    - destination:
        host: ${metadata.labels[component]}.3db.svc.cluster.local  
  - match: 
    - uri:
        prefix: /${metadata.labels[component]}/
    rewrite:
      uri: "/"
    route:
    - destination:
        host: ${metadata.labels[component]}.3db.svc.cluster.local

这不是Kubernetes本身的功能,但是还有其他工具可以帮助您解决这个问题

其中最主要的是。它允许您创建可以跨多个不同YAML文件共享的变量,允许您共享值,甚至完全参数化部署。

查看downwardAPI,在运行时将pod元数据(如标签和注释)注入pod。
你能举例说明如何在VirtualService规范中使用向下API吗?当资源类型为VirtualService时如何使用它?
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: gcr.io/google_containers/busybox
      command: ["sh", "-c", "while true; do if [[ -e /etc/labels ]]; then cat /etc/labels; fi; if [[ -e /etc/annotations ]]; then cat /etc/annotations; fi; sleep 5; done"]
      volumeMounts:
        - name: podinfo
          mountPath: /etc
          readOnly: false
  volumes:
    - name: podinfo
      downwardAPI:
        items:
          - path: "labels"
            fieldRef:
              fieldPath: metadata.labels
          - path: "annotations"
            fieldRef:
              fieldPath: metadata.annotations